简体   繁体   English

JCheckBox 检查 JTable 列中的值

[英]JCheckBox checks for values in JTable column

I am a beginner at Java GUIs.我是 Java GUI 的初学者。

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.我有一个带有几列的JTable ,其中一列具有某些行的值,而其他一些行则没有值。

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.我想让 JCheckBox 侦听器查看 JTable 的该列中是否有值,以及是否选择了该框来对显示这些值的表进行排序。 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.我还有两个 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");


        }

    }
}


}

Each row can have or not have a String value for the column Owner.每行可以有或没有列所有者的String值。 I want to sort the table on those two facts using the JCheckBox.我想使用 JCheckBox 根据这两个事实对表格进行排序。

Firstly you need to add a listener to your checkbox like so :首先,您需要在复选框中添加一个侦听器,如下所示:

checkbox.addItemListener(new ItemListener() {

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


        }else {


        }
    }
});

Now the question is what do you want to do when you select the checkbox.现在的问题是当您选中复选框时您想做什么。 You want some kind of sorting algorithm in your table model.您需要在表模型中使用某种排序算法。 There is a good example at this website :这个网站有一个很好的例子:

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

You just need to add some kind of Comparable to your entity objects which are represented in the table model.您只需要向表模型中表示的实体对象添加某种Comparable即可。 By default empty values will be shown at the bottom anyway.默认情况下,无论如何都会在底部显示空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM