I am new using Swing, that is why am asking a lot; actually, I am searching a lot about this, but I could not found a good answer. I have to do a dynamic table; it has to be filled with the name of the sheets from an XML file, and there is another column with a check box which will be clicked if the sheet has to be validated.
When I create the table it is empty. After an other action it will be filled with the data.
I need to know how to use the check box listener in this case.
This is the main code, the button open performs the filling.
public class CreateScenarioUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
String filePath = null;
String[] sheetNames = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateScenarioUI frame = new CreateScenarioUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CreateScenarioUI() {
setTitle("Scenario creation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final MainTable table = new MainTable(contentPane);
/** Button Open */
final JButton btnOpen = new JButton("Open");
btnOpen.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
performBtnOpen();
}
private void performBtnOpen() {
JFileChooser choose = new JFileChooser();
choose.addChoosableFileFilter(new ExcelFilter());
int returnVal = choose.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
textField.setText(choose.getSelectedFile().getName());
}
FileReader fileReader = new FileReader();
System.out.println(choose.getSelectedFile());
filePath = choose.getSelectedFile().toString();
sheetNames = fileReader.getSheetNames(choose.getSelectedFile());
table.getModel().addColumn("Sheet name", sheetNames);
Boolean[] selectorColumn = new Boolean[sheetNames.length];
for (int i = 0; i < sheetNames.length; i++) {
selectorColumn[i] = new Boolean(false);
}
table.getModel().addColumn("Selector", selectorColumn);
}
});
btnOpen.setBounds(320, 50, 100, 20);
contentPane.add(btnOpen);
}}
And here is how I create the MainTable
:
public class MainTable extends JFrame implements TableModelListener, ItemListener {
DefaultTableModel model = new DefaultTableModel();
@Override
public void tableChanged(TableModelEvent arg0) {
// TODO Auto-generated method stub
}
public MainTable(JPanel contentPane){
String[] setColumnIdentifiers = {};
JTable table = new JTable(new Object[][] {}, new Object[] {
"Sheet name", "Create" });
JScrollPane scrollPane = new JScrollPane(table);
/** Main Table */
model = new DefaultTableModel(setColumnIdentifiers, 0) {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int columnIndex) {
if (columnIndex == 1) {
return Boolean.class;
} else {
return super.getColumnClass(columnIndex);
}
}
@Override
public boolean isCellEditable(int row, int column) {
boolean editable = true;
if (column == 0) {
editable = false;
/**Object value = getValueAt(row, column);
if (value instanceof Integer) {
editable = ((int)value) != 0;
}*/
}
return editable;
}
};
table.setModel(model);
table.getModel().addTableModelListener(this);
table.setFillsViewportHeight(true);
table.setColumnSelectionAllowed(true);
table.setBorder(new LineBorder(new Color(0, 0, 0)));
table.setCellSelectionEnabled(true);
table.setBounds(40, 100, 380, 190);
scrollPane.setBounds(40, 100, 380, 190);
scrollPane.setBorder(new LineBorder(new Color(0, 0, 0)));
contentPane.add(scrollPane);
}
public DefaultTableModel getModel(){
return model;
}
@Override
public void itemStateChanged(ItemEvent event) {
//Object source = event.getItemSelectable();
// TODO Auto-generated method stub
}
}
Any help will be useful, thank you everyone!