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中文網其他相關文章!