JTable计算正在使用的行数并返回该数字

时间:2014-04-04 16:11:56

标签: java swing jtable jbutton rows

我正在开展一个小型项目" Car Park Table"。它允许您添加,删除和搜索汽车。除此之外,它意味着返回已经停放的汽车数量。 我的问题是:

如何编写一个代码,用于计算使用的行数并返回正在使用的数字? 大概是通过按钮或通过标签自动更新。

注意:对于混乱感到抱歉。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class ParkingTable {

  public static void main(String args[]) {



    JFrame frame = new JFrame("Car Parking Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




 /**********************************************************************/ 


   String columns[] = { "Reg","Bay" };
    Object rows[][] = {

    //Population
    { "IL39D",     1 }, 
    { "HEL39D",     2 }, 
    { "AD903",     3 },
    { "TE3P3P",    4 }, 
    { "EPO338C",   5 }, 
    { "J03C33",    6 },
    { "AOEC3CC",   7 }, 
    { "VVVAV33",   8 }, 
    { "AVARG033",  9 },
    { "AEED33",    10 }, };



    TableModel model = new DefaultTableModel(rows, columns) {
  public Class getColumnClass(char column) {
    Class returnValue;
    if ((column >= -1) && (column < getColumnCount())) {
      returnValue = getValueAt(-1, column).getClass();
    } else {
      returnValue = Object.class;
    }
    return returnValue;
  }
};

  /**********************************************************************/   


    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<>(model);
    table.setRowSorter(sorter);



  /**********************************************************************/  
    //Table
    JScrollPane scrTable = new JScrollPane(table);
    frame.add(scrTable , BorderLayout.CENTER);


   //Panel
    JPanel panel = new JPanel(new BorderLayout());
    frame.add(panel, BorderLayout.NORTH);


 /**********************************************************************/
    //Find The Car
    final JTextField tfFilter = new JTextField();

    tfFilter.addFocusListener(new FocusAdapter() {
   public void focusGained(FocusEvent fEvt){     
      tfFilter.setText("Enter The Reg Number To Find The Car");
      tfFilter.selectAll();

   } 

});

tfFilter.addKeyListener(new KeyAdapter(){
    public void keyReleased(KeyEvent e){
        String st=tfFilter.getText();
        tfFilter.setText(st.toUpperCase());
    }
});

 /**********************************************************************/

    //Reg Number
    final JTextField tfReg= new JTextField("Enter Car Reg");

    tfReg.addFocusListener(new FocusAdapter() {
       public void focusGained(FocusEvent fEvt){
      tfReg.setText("Enter Car Reg");
      tfReg.selectAll();
       } 

});
     tfReg.addKeyListener(new KeyAdapter(){
    public void keyReleased(KeyEvent e){
        String st=tfReg.getText();
        tfReg.setText(st.toUpperCase());
    }
    });


/**********************************************************************/



//Bay Number
    final JTextField tfBay= new JTextField("Enter The Bay NUMBER");

    tfBay.addFocusListener(new FocusAdapter() {
   public void focusGained(FocusEvent fEvt){
      tfBay.setText("Enter The Bay NUMBER");
      tfBay.selectAll();
   } 

    });

   tfBay.addKeyListener(new KeyAdapter(){
     public void keyTyped(KeyEvent e){
     char vChar=e.getKeyChar();
     if(!(Character.isDigit(vChar)||
             (vChar== KeyEvent.VK_BACK_SLASH)||
             (vChar==KeyEvent.VK_DELETE))){
         e.consume();
     }
 }
   });


 /**********************************************************************/

//Butons
JButton btnFilter = new JButton("Filter");
JButton btnAdd = new JButton ("Add");
JButton btnDelete=new JButton("Delete");


//Filter For The Reg
btnFilter.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    String text = tfFilter.getText();
    if (text.length() == 0) {
      sorter.setRowFilter(null);
    } else {
      sorter.setRowFilter(RowFilter.regexFilter(text));
         }
     }
});

/**********************************************************************/  

//Add Car To Bay
btnAdd.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        DefaultTableModel model=(DefaultTableModel) table.getModel();
        if(!tfReg.getText().trim().equals("")){
            model.addRow(new   Object[]{tfReg.getText(),tfBay.getText()});


              }
       }      
});

/**********************************************************************/

//Delete The Selected Car
btnDelete.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        DefaultTableModel model=(DefaultTableModel) table.getModel();
        if(table.getSelectedRow()==-1){
            if(table.getRowCount()==0){   
            }
        }else{
            model.removeRow((table.getSelectedRow()));
            }    
      }
});

/**********************************************************************/

//Placing Of  Buttons And  Text Boxes
frame.add(btnFilter, BorderLayout.SOUTH);
frame.add(btnAdd, BorderLayout.WEST);
frame.add(btnDelete, BorderLayout.EAST);

panel.add(tfReg,BorderLayout.CENTER);
panel.add(tfBay,BorderLayout.AFTER_LAST_LINE);
panel.add(tfFilter,BorderLayout.BEFORE_FIRST_LINE);

frame.setSize(350, 350);
frame.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:4)

您可以使用model.getRowCount()方法返回表格中的行数。 t

您还可以使用this constructor限制模型以保留创建数据模型的特定行数。

public DefaultTableModel(Vector columnNames,
                 int rowCount)