I have managed to create a table in eclipse. The table has rows that are numbered. Here is the code:
public class JTableRowHeader {
private JFrame frame = new JFrame("JTable RowHeader");
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel model;
private TableRowSorter<TableModel> sorter;
private JTable headerTable;
private JCheckBox chckbxNewCheckBox; // declared check box
private JCheckBox all;
//private List<JCheckBox> checkBoxes;
public JTableRowHeader() {
int NoOfRows = 60;
table = new JTable(NoOfRows, 2);
for (int i = 0; i < table.getRowCount(); i++) {
table.setValueAt(i, i, 0);
}
sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
model = new DefaultTableModel() {
};
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
model.fireTableRowsUpdated(0, model.getRowCount() - 1);
}
});
scrollPane = new JScrollPane(table);
scrollPane.setRowHeaderView(headerTable);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(scrollPane);
chckbxNewCheckBox = new JCheckBox("New check box");
scrollPane.setColumnHeaderView(chckbxNewCheckBox);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (info.getName().equals("Nimbus")) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
//e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JTableRowHeader TestTableRowHeader = new JTableRowHeader();
frame.add(new CheckBoxGroup(); //trying to add the check box
}
});
}
}
Now that I have created the table I wish to add a checkbox for all the rows in the table. I also want the checkboxes to cahnge when I change the number of rows in the table for example if I have 4 rows I want 4 checkboxes and if I increase it to 60 rows I want 60 checkboxes. I have tried a few attempts but have failed. I am fairly new to this so any help using my code will be appreciated.
Aucun commentaire:
Enregistrer un commentaire