I am a beginner at Java GUIs.
I have a JTable with a few columns and one of those columns has values for some rows and for some other rows there are no values.
I would like to make a JCheckBox listener see if there are values inside that column of the JTable, and if the box is selected to resort the table showing those values. If I uncheck the box, I want the table to be shown with the no values in that column. How could the above be done? Thank you. Here is my code. I also have some two JComboBox that do what I want them to do.
public class myGui extends JPanel implements ActionListener{
JPanel pane = new JPanel();
JPanel pane2 = new JPanel();
AccomodationList list;
String[] roomOrCabin = {"Room","Cabin"};
String[] booked = {"Booked","Not Booked","All"};
JComboBox dropRoom = new JComboBox(roomOrCabin);
JComboBox dropBook = new JComboBox(booked);
JCheckBox ownerButton = new JCheckBox("Owner",true);
JTable table;
DefaultTableModel model;
Vector<String> columnNames;
TableRowSorter<TableModel> trs;
public myGui(AccomodationList list){
super(new BorderLayout());
pane.setLayout( new BorderLayout() );
columnNames = new Vector<String>();
columnNames.addElement("Accomodation Number");
columnNames.addElement("Number of Beds");
columnNames.addElement("Number of Rooms");
columnNames.addElement("Type");
columnNames.addElement("Cost per Night");
columnNames.addElement("Owner");
columnNames.addElement("Booked");
columnNames.addElement("Client");
columnNames.addElement("Booked Statistics");
Vector<Vector> f = populateTable(list);
table = new JTable(f, columnNames);
JScrollPane scroll = new JScrollPane(table);
model = new DefaultTableModel(f,columnNames){
@Override
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
trs = new TableRowSorter<TableModel>(model);
table.setRowSorter(trs);
this.list = list;
pane.add(table);
JScrollPane scrollPane = new JScrollPane( table );
table.setAutoCreateRowSorter(false);
pane.add( scrollPane, BorderLayout.CENTER );
pane2.add(dropRoom);
pane2.add(dropBook);
pane2.add(ownerButton);
pane.add(pane2,BorderLayout.SOUTH);
this.add(pane);
dropRoom.addActionListener(this);
dropBook.addActionListener(this);
ownerButton.addActionListener(this);
}
public Vector<Vector> populateTable(AccomodationList list){
Vector<Vector> f = new Vector<Vector>();
String t;
for(Accomodation a: list.getList()){
Vector<Object> v = new Vector<Object>();
t=String.valueOf(a.getNoAcc());
v.addElement(a.getNoAcc());
t=String.valueOf(a.getSumOfBeds());
v.addElement(t);
t=String.valueOf(a.getNoBedrooms());
v.addElement(t);
t=String.valueOf(a.getType());
v.addElement(t);
t=String.valueOf(a.getCost());
v.addElement(t);
t=String.valueOf(a.getInitials(a.getOwner()));
v.addElement(t);
t=String.valueOf(a.getBooked().toString());
v.addElement(t);
t=String.valueOf(a.getInitials(a.getClient()));
v.addElement(t);
t=String.valueOf(a.getBookedCnt());
v.addElement(t);
f.addElement(v);
}
return f;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == dropRoom){
String text = dropRoom.getSelectedItem().toString();
System.out.println("text: "+text);
if (text.length() == 0) {
trs.setRowFilter(null);
} else {
try {
trs.setRowFilter(
RowFilter.regexFilter(text));
} catch (PatternSyntaxException pse) {
System.err.println("Bad regex pattern");
}
}
}
if(e.getSource() == dropBook){
String selected = dropBook.getSelectedItem().toString();
String text="";
if ("Booked".equals(selected)){text = "true";}else if("Not Booked".equals(selected)){text = "false";}else{text = "";}
if (text.length() == 0) {
trs.setRowFilter(null);
} else {
try {
trs.setRowFilter(
RowFilter.regexFilter(text));
} catch (PatternSyntaxException pse) {
System.err.println("Bad regex pattern");
}
}
}
if(e.getSource() == ownerButton){
JCheckBox cb = (JCheckBox) e.getSource();
if (cb.isSelected()) {
System.out.println("Owner is enabled");
}
else {
System.out.println("Owner is disabled");
}
}
}
}
Each row can have or not have a String
value for the column Owner. I want to sort the table on those two facts using the JCheckBox.