我的应用程序中有两个ComboBox,用于每个JTable的行和列。如下所示:
我想动态添加和删除每个表中的行和列。我知道我会做table.add()
之类的事情,但删除怎么办?例如,如果用户从Row组合框中选择3,它应该向其添加另一行,但如果用户选择2返回怎么办?它应该删除插入的行。那我该怎么做呢?
我知道这可能是一个愚蠢的问题,但我是Swing的新手,所以人们对我的期望非常高:(
答案 0 :(得分:3)
您似乎想要使用组合框控制表的维度。 JTable渲染其底层数据模型。阅读How to Use Tables中有关表格的更多信息。添加和删除行实际上是对数据模型的操纵。例如,DefaultTableModel有许多方法可用于您的任务:addRow()
,removeRow()
,getRowCount()
等。
这一切都取决于所需的数据和任务。看看这个使用包装Apache RealMatrix的自定义表模型的简单示例。您可以选择所需的任何数据结构。 DefaultTableModel
也很好,可能就足够了。该示例有两个用于调整表大小的组合框。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
public class TestTableDims extends JPanel{
private MyTableModel model;
private JTable table;
private JComboBox rowsCombo;
private JComboBox columnsCombo;
public TestTableDims() {
setLayout(new BorderLayout(5, 5));
model = new MyTableModel(2, 2);
JPanel buttonsPanel = new JPanel();
Integer[] test = {1, 2, 3, 4, 5};
rowsCombo = new JComboBox(test);
rowsCombo.setSelectedItem(2);
rowsCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.setRows(((Integer)rowsCombo.getSelectedItem()));
}
});
buttonsPanel.add(new JLabel("rows"));
buttonsPanel.add(rowsCombo);
columnsCombo = new JComboBox(test);
columnsCombo.setSelectedItem(2);
columnsCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.setColumns(((Integer)columnsCombo.getSelectedItem()));
}
});
buttonsPanel.add(new JLabel("columns"));
buttonsPanel.add(columnsCombo);
add(buttonsPanel, BorderLayout.NORTH);
JTable table = new JTable();
table.setModel(model);
add(new JScrollPane(table));
}
class MyTableModel extends AbstractTableModel {
private RealMatrix matrix;
public MyTableModel(int rows, int columns) {
matrix = new Array2DRowRealMatrix(rows, columns);
}
public void setRows(int rows) {
matrix = new Array2DRowRealMatrix(rows, matrix.getColumnDimension());
fireTableStructureChanged();
}
public void setColumns(int columns) {
matrix = new Array2DRowRealMatrix(matrix.getRowDimension(), columns);
fireTableStructureChanged();
}
@Override
public int getColumnCount() {
return matrix.getColumnDimension();
}
@Override
public int getRowCount() {
return matrix.getRowDimension();
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return Double.class;
}
@Override
public void setValueAt(Object value, int row, int column) {
matrix.setEntry(row, column, (double)value);
}
@Override
public Object getValueAt(int row, int column) {
return matrix.getEntry(row, column);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
TestTableDims panel = new TestTableDims();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}