[英]How to set rows and columns of a JTable Dynamic
my question is how to set dynamic, the number of rows and columns in a JTable? 我的问题是如何设置动态,JTable中的行数和列数? I mean if the user wants to create a table of 2 rows and 2 columns, he just type down the number.
我的意思是如果用户想要创建一个包含2行和2列的表,他只需键入数字即可。 How can I do that, I've tried with the DefaultModel with no success.
我怎么能这样做,我尝试使用DefaultModel但没有成功。
I will appreciate any help. 我将不胜感激任何帮助。
Thanks 谢谢
DefaultTableModel
has two means by which you can define the number of rows/columns at runtime. DefaultTableModel
有两种方法可用于在运行时定义行/列数。
Simply create a new DefaultTableModel
, passing it the rows and columns you want... 只需创建一个新的
DefaultTableModel
,将它传递给你想要的行和列......
DefaultTableModel model = new DefaultTableModel(rows, cols);
and then apply this to the JTable
. 然后将其应用于
JTable
。 This will, obviously, replace the existing table model, meaning you will lose all of it's data. 显然,这将取代现有的表模型,这意味着您将丢失所有数据。
Create an master DefaultTableModel
and apply it to the JTable
and simply use 创建一个主
DefaultTableModel
并将其应用于JTable
并简单地使用
model.setRowCount(rows);
model.setColumnCount(cols);
to dynamically update the number of rows and columns as required. 根据需要动态更新行数和列数。 This will allow you to preserve the data within the table model (expect when you remove rows or columns, then it's lost)
这将允许您保留表模型中的数据(期望当您删除行或列时,它会丢失)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private DefaultTableModel model;
private JSpinner fldRows;
private JSpinner fldColumns;
public TestPane() {
setLayout(new BorderLayout());
fldRows = new JSpinner(new SpinnerNumberModel(1, 1, 999999, 1));
fldColumns = new JSpinner(new SpinnerNumberModel(1, 1, 999999, 1));
JPanel options = new JPanel(new GridBagLayout());
options.add(new JLabel("Rows: "));
options.add(fldRows);
options.add(new JLabel("Columns: "));
options.add(fldColumns);
JButton update = new JButton("Update");
options.add(update);
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rows = (int) fldRows.getValue();
int cols = (int) fldColumns.getValue();
// Dynamic master model...
// model.setRowCount(rows);
// model.setColumnCount(cols);
// Replace model
table.setModel(new DefaultTableModel(rows, cols));
}
});
model = new DefaultTableModel();
table = new JTable();
add(new JScrollPane(table));
add(options, BorderLayout.NORTH);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.