JTable 中的多行选择
当遇到包含不可编辑文本和布尔复选框的 JTable 时,最好取消选中所有框当其中任何一个未选中时,就会处于选择状态,反之亦然。
为了促进此行为,考虑下面的示例:
import java.awt...; import javax.swing...; public class CheckABunch extends JPanel { // Constants and variables for data model and JTable setup private static final int CHECK_COL = 1; private static final Object[][] DATA = {...}; private static final String[] COLUMNS = {...}; private DataModel dataModel = ...; private JTable table = ...; private DefaultListSelectionModel selectionModel; // Constructor initializes the interface and selection model public CheckABunch() { super(new BorderLayout()); ... table.setPreferredScrollableViewportSize(...); selectionModel = ...; } // DataModel provides custom column classes and editability private class DataModel extends DefaultTableModel { public DataModel(Object[][] data, Object[] columnNames) {...} public Class<?> getColumnClass(int columnIndex) {...} public boolean isCellEditable(int row, int column) {...} } // ControlPanel contains buttons for selecting and clearing private class ControlPanel extends JPanel { public ControlPanel() {...} } private class SelectionAction extends AbstractAction { boolean value; public SelectionAction(String name, boolean value) {...} public void actionPerformed(ActionEvent e) {...} } // Main method for creating and displaying the interface public static void main(String[] args) {...} }
在此示例中,SelectionAction 处理按钮单击以根据用户的选择更新复选框。 dataModel 确保复选框列可编辑且类型正确。
通过使用这种方法,您可以轻松在 JTable 中实现多行复选框选择和同步。
以上是如何同步 JTable 中的多行复选框选择?的详细内容。更多信息请关注PHP中文网其他相关文章!