我只是想知道如何update specific cell in JTable
,如何在JTable中設置特定單元格的值?
一樣,如果我想設置單元格(1,1)有Test Value
值。
我的代碼是這樣的,但對我不起作用:
String s = "Test Value";
tableName.setValueAt((Object)s, 1, 1);
我只是想知道如何update specific cell in JTable
,如何在JTable中設置特定單元格的值?
一樣,如果我想設置單元格(1,1)有Test Value
值。
我的代碼是這樣的,但對我不起作用:
String s = "Test Value";
tableName.setValueAt((Object)s, 1, 1);
您可能希望使用DefaultTableModel#setValueAt()
DefaultTableModel model = (DefaultTableModel)tableName.getModel();
model.setValueAt(s, 1, 1);
首先,您需要與DefaultTableModel
DefaultTableModel model = new DefaultTableModel(data, cols);
JTable table = new JTable(model);
您可以運行這個例子,看看
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable {
String[] cols = {"Col 1", "Col2"};
String[][] data = {{"Hello", "World"},{"Hello", "World"}};
DefaultTableModel model = new DefaultTableModel(data, cols);
JTable table = new JTable(model);
JButton button = new JButton("Set Value at 1, 1");
JTextField text = new JTextField(20);
public TestTable() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(table, BorderLayout.NORTH);
panel.add(text, BorderLayout.CENTER);
panel.add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String value = text.getText();
model.setValueAt(value, 1, 1);
}
});
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestTable();
}
});
}
}
編輯使用產品類。
「我創建了一個類產品有8個字段,我想用循環做的就是在填充ArrayList中的數據,以我的表什麼」
你想使用.addRow
動態添加行。您需要從每個Product
中獲取每個字段並將其設置爲一行。然後像這樣添加那一行。注意:您應該使用getters
但爲了簡潔起見,我沒有。
for (Product p : list) {
String data1 = p.field1;
int data2 = p.field2;
int data3 = p.field3;
int data4 = p.field4;
int data5 = p.field5;
int data6 = p.field6;
int data7 = p.field7;
int data8 = p.field8;
Object[] row = {data1, data2, data3, data4, data5, data6, data7, data8};
model.addRow(row);
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable {
String[] cols = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6", "COl 7", "Col 8"};
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable table = new JTable(model);
JButton button = new JButton("Set Table");
List<Product> list;
public TestTable() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
panel.add(button, BorderLayout.SOUTH);
list = getOneRow();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for (Product p : list) {
String data1 = p.field1;
int data2 = p.field2;
int data3 = p.field3;
int data4 = p.field4;
int data5 = p.field5;
int data6 = p.field6;
int data7 = p.field7;
int data8 = p.field8;
Object[] row = {data1, data2, data3, data4, data5, data6, data7, data8};
model.addRow(row);
}
}
});
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public List<Product> getOneRow() {
List<Product> list = new ArrayList<>();
list.add(new Product("Product 1", 1, 2, 3, 4, 5, 6, 7));
list.add(new Product("Product 2", 1, 2, 3, 4, 5, 6, 7));
list.add(new Product("Product 3", 1, 2, 3, 4, 5, 6, 7));
list.add(new Product("Product 4", 1, 2, 3, 4, 5, 6, 7));
return list;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestTable();
}
});
}
}
class Product {
String field1;
int field2;
int field3;
int field4;
int field5;
int field6;
int field7;
int field8;
public Product(String s, int f2, int f3, int f4, int f5, int f6, int f7, int f8) {
field1 = s;
field2 = f2;
field3 = f3;
field4 = f4;
field5 = f5;
field6 = f6;
field7 = f7;
field8 = f8;
}
}
什麼問題,你用上面的代碼中得到些什麼? – eatSleepCode
什麼表模型正在使用? – KrzyH