[英]Update JTable content based on ActionPerformed
I seem to be having trouble getting to grips with implementing a simple JTable that when a button is pressed on a GUI it simply updates the contents of the table 我似乎很难掌握实现一个简单的JTable的问题,即当在GUI上按下按钮时,它只是更新表的内容
So far I seem to be getting mixed ideas of what type of table to implement to do this but from searching around it seems to be DefaultTable model. 到目前为止,我似乎对实现哪种类型的表的想法不一,但是从周围搜索似乎是DefaultTable模型。 So far I have declared the table
到目前为止,我已经声明了桌子
private JTable table;
static Object[] columnNames = new Object[]{"Column 1","Column 2"};
static Object[][] rowData = { {"1", "2"} };
public TestTables() {
DefaultTableModel tableModel;
tableModel = new DefaultTableModel(rowData, columnNames);
}
I'm not even sure if I have declared this correctly, but when I run the GUI it shows the table data correctly. 我什至不确定我是否正确声明了此信息,但是在运行GUI时它正确显示了表数据。 I just dont understand how I can update it using an action performed
我只是不明白如何使用执行的操作来更新它
public void actionPerformed(ActionEvent e) {
if (e.getSource() == X1btn) {
// not sure how to set table to be {"3", "4"} instead
}
In your actionPerformed method, get a reference to the JTable's model (the details of this will depend on your program's structure -- something we don't know at present) which will be a DefaultTableModel, and then you can easily do whatever needs to be done with this object including adding rows, removing rows, changing values held by cells... 在您的actionPerformed方法中,获取一个JTable模型的引用(具体细节取决于您程序的结构-我们目前尚不知道),这将是一个DefaultTableModel,然后您可以轻松地执行所需的任何操作完成此对象的操作,包括添加行,删除行,更改单元格保存的值...
If you're still stuck, consider creating and posting a minimal compilable and runnable example that demonstrates your problem, an sscce . 如果仍然遇到问题,请考虑创建并发布一个最小的可编译且可运行的示例,该示例演示了您的问题sscce 。
For example: 例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTables {
static Object[] columnNames = new Object[] { "Column 1", "Column 2" };
static Integer[][] rowData = { {1, 2}, {3, 4} };
private JPanel mainPanel = new JPanel();
private DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);
private JTable table = new JTable(tableModel);
public TestTables() {
JButton timesTwoBtn = new JButton("Multiply By 2");
timesTwoBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < tableModel.getRowCount(); row++) {
for (int col = 0; col < tableModel.getColumnCount(); col++) {
Integer value = (Integer) tableModel.getValueAt(row, col);
value *= 2;
tableModel.setValueAt(value, row, col);
}
}
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(timesTwoBtn);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
}
public JPanel getMainPanel() {
return mainPanel;
}
private static void createAndShowGui() {
TestTables testTables = new TestTables();
JFrame frame = new JFrame("TableFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testTables.getMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
make an actionperformed method then get your Jtable model. 制作一个可操作的方法,然后获取您的Jtable模型。 then do something with that model.
然后对该模型进行处理。
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel)mytable.getModel();
//do something with the model say add a new row or data to the table
model.addRow(new Object[]{"new Column1 Data","new Column2 Data"});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.