JCheckBox检查JTable列

时间:2015-11-25 12:21:52

标签: java swing jtable jcheckbox

我是Java GUI的初学者。

我有一个带有几列的 JTable ,其中一列有某些行的值,而对于其他一些行则没有值。

我想让一个JCheckBox监听器看看JTable的该列内是否有值,如果选择该框来表示显示这些值的表。如果我取消选中该框,我希望该表显示该列中没有值。如何才能做到? 谢谢。 这是我的代码。我也有两个JComboBox可以做我想让他们做的事。

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");


        }

    }
}


}

每行可以具有或不具有列所有者的String值。我想使用JCheckBox对这两个事实进行排序。

1 个答案:

答案 0 :(得分:1)

首先,你需要为你的复选框添加一个监听器,如下所示:

checkbox.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange() == ItemEvent.SELECTED) {


        }else {


        }
    }
});

现在的问题是,当您选中复选框时,您想要做什么。您需要在表模型中使用某种排序算法。这个网站有一个很好的例子:

http://www.java2s.com/Tutorial/Java/0240__Swing/SampleSortingTableModel.htm

您只需要在表格模型中表示的实体对象中添加某种可比较。默认情况下,无论如何都会在底部显示空值。