I've removed most of my GUI to keep the code short.
I have a buttongroup of 3 JRadioButtons to select the table schema i want to display in my JTable, which is contained in a JScrollPane
I have tried to use fireTableStructureChanged()
andfireTableDataChanged()
as well as JTable.repaint()
to no avail. Can anyone help me?
Here is a simple example that runs a window with my configuration but does not update the table.
public class test1 implements ActionListener {
private boolean payrollActive = false;
private JPanel mainPanel = new JPanel();
private JTable dataTable;
private Vector<String> courseColumns = new Vector<String>();
private Vector<String> courseColumnsPay = new Vector<String>();
private Vector<String> profsColumns = new Vector<String>();
private Vector<String> offSpaceColumns = new Vector<String>();
public test1() {
//Add columns for tables
String[] courseColsPay = {"Year", "Program", "Course", "Code", "CCCode",
"Weight", "Session", "Section", "Day", "STime", "FTime",
"BookedRM", "EnrolCap", "Description", "ProfFName",
"ProfLName", "ProfEmail", "Notes", "Syllabus", "Exam",
"CrossList", "PreReqs", "EnrolCtrls", "Shared",
"TrackChanges", "Address", "WageType", "BasePay",
"BenefitRate", "Budgeted", "PayAmount",
"MthAmount", "Term", "AccNumber", "PayAdmin", "PayableTo"};
for (String col : courseColsPay) {
courseColumnsPay.add(col);
}
for (int i = 0; i < 25; i++) {
courseColumns.add(courseColsPay[i]);
}
String[] profCols = {"FName", "LName", "Email", "UTEmail", "Birthdate",
"OfficeBC", "OfficeRM", "Department", "Status",
"Fellowship", "OfficeStat", "PhoneNum", "HomeAddr",
"HomePhoneNum", "Notes"};
for (String col : profCols) {
profsColumns.add(col);
}
String[] offSpaceCols = {"Building", "DeptID", "DivisionName", "BldgID", "RoomID",
"Category", "Description", "ShareType", "DeptName",
"Status", "SharePerc", "ShareOccupancy", "Area",
"Fellow", "Commments", "Name", "Position",
"Dept", "FTE", "CrossApp", "CrossPos", "CrossDept",
"CrossFTE", "OtherOffice"};
for (String col : offSpaceCols) {
offSpaceColumns.add(col);
}
mainPanel.setSize(1260, 630);
mainPanel.setLayout(null);
JRadioButton coursesBtn = new JRadioButton("Courses");
coursesBtn.setMnemonic(KeyEvent.VK_C);
coursesBtn.setActionCommand("Course");
coursesBtn.setSelected(true);
coursesBtn.addActionListener(this);
JRadioButton profsBtn = new JRadioButton("Professors");
profsBtn.setMnemonic(KeyEvent.VK_P);
profsBtn.setActionCommand("Professors");
coursesBtn.addActionListener(this);
JRadioButton officeSpBtn = new JRadioButton("Office Spaces");
officeSpBtn.setMnemonic(KeyEvent.VK_O);
officeSpBtn.setActionCommand("Office Spaces");
coursesBtn.addActionListener(this);
ButtonGroup tablesBtns = new ButtonGroup();
tablesBtns.add(coursesBtn);
tablesBtns.add(profsBtn);
tablesBtns.add(officeSpBtn);
JPanel tableRadioPanel = new JPanel(new GridLayout(0, 1));
tableRadioPanel.setOpaque(true);
tableRadioPanel.setBounds(0, 0, 150, 70);
tableRadioPanel.add(coursesBtn);
tableRadioPanel.add(profsBtn);
tableRadioPanel.add(officeSpBtn);
//table start
DefaultTableModel coursesModel = new DefaultTableModel(courseColumns, 200);
dataTable = new JTable(coursesModel);
dataTable.setFillsViewportHeight(true);
dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(dataTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(160, 0, 1016, 558);
//table code end
mainPanel.add(tableRadioPanel);
mainPanel.add(scrollPane);
}
public JComponent getMainPanel() {
return mainPanel;
}
public JTable getDataTable() {
return dataTable;
}
/**
* Returns the list of columns for the given table
* @param identifier the name of the table
* @return a Vector<String> of column names
*/
public Vector<String> getColumns(String identifier) {
switch (identifier) {
case "Courses":
if (payrollActive) {
return courseColumnsPay;
} else {
return courseColumns;
}
case "Professors":
return profsColumns;
case "Office Spaces":
return offSpaceColumns;
default:
return null;
}
}
public static void createAndShowGui() {
test1 vicu = new test1();
JFrame frame = new JFrame("Victoria University Database Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1260, 630);
frame.setLocationRelativeTo(null);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.getContentPane().add(vicu.getMainPanel());
frame.getContentPane().setLayout(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton targetBtn = (JRadioButton) e.getSource();
((DefaultTableModel) dataTable.getModel()).
setColumnIdentifiers(getColumns(targetBtn.getText()));
}
}
In your example, you are not registering an ActionListener
to profsBtn
or officeSpBtn
, you keep registering to coursesBtn
JRadioButton coursesBtn = new JRadioButton("Courses");
//...
coursesBtn.addActionListener(this);
JRadioButton profsBtn = new JRadioButton("Professors");
//...
coursesBtn.addActionListener(this);
JRadioButton officeSpBtn = new JRadioButton("Office Spaces");
//...
coursesBtn.addActionListener(this);
Once I register the ActionListener
to the correct buttons, it works fine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With