gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:20 25 4
gpt4 key购买 nike

我正在做一个小项目“ parking 场 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);

}

}

最佳答案

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

您还可以限制您的模型以包含使用 this constructor 创建数据模型的特定行数.

public DefaultTableModel(Vector columnNames,
int rowCount)

关于java - JTable 计算正在使用的行数并返回数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com