gpt4 book ai didi

java - 删除 JTable 行

转载 作者:行者123 更新时间:2023-12-01 18:52:09 24 4
gpt4 key购买 nike

我正在研究 jTable,我打算删除特定行,作为在表上进行的数据操作的一部分。本质上,我已经能够成功删除用户指定的行,但我真正想做的是根据 boolean 状态或作为表四列之一选择的复选框删除几行。

我已附上有关运行代码的当前结果的屏幕截图,并且希望能够根据选定的 boolean 状态或复选框删除行。

下面是我的代码,包括扩展 AbstractTableModel 的表模型代码:

package com.TableRowSelectProgramatically;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

@SuppressWarnings("serial")
public class JTableRowSelectProgramatically extends JPanel {

public MyTableModel MyTableModel;

public String Cell1 = "ABCD";

public JTableRowSelectProgramatically() {
initializePanel();
}

private void initializePanel() {

setLayout(null);
setPreferredSize(new Dimension(1250, 700));

// Table model
MyTableModel = new MyTableModel();

// Table
final JTable table = new JTable(MyTableModel);
table.setFillsViewportHeight(true);

// Row data
Object[] values = {Cell1, "EFGH", "IJKL", new Boolean(false)};
Object[] values2 = {"UVWX","QRST","MNOP", new Boolean(false)};
Object[] values3 = {"ABCD","YZAB","CDEF", new Boolean(false)};
final Object[] values4 = {"QWERTY","YTREWQ","QWERTY", new Boolean(false)};

// Insert row data
MyTableModel CustomTableModel = (MyTableModel) table.getModel();
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);

// Create edit menu label
JLabel labelEditMenu = new JLabel("EDIT MENU:\n");

// Create add row btn
JButton addRow = new JButton("Add Row");

// Attach listener for add row btn
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyTableModel.insertData(values4);
}
});

// Create delete row btn
JButton deleteRow = new JButton("Delete Row");

// Attach listener for delete btn
deleteRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyTableModel.removeRow(1);
}
});

// Create scroll pane
JScrollPane pane = new JScrollPane(table);
pane.setBounds(30, 30, 500, 500);

//
JTextField AgentIDTextField = new JTextField();

// Populate the JPanel
JPanel dataEntryPanel = new JPanel(new FlowLayout());
//dataEntryPanel.setBackground(Color.orange);
dataEntryPanel.setBounds(540, 30, 500, 50);
//dataEntryPanel.add(AgentIDTextField);
dataEntryPanel.add(labelEditMenu);
dataEntryPanel.add(addRow);
dataEntryPanel.add(deleteRow);

// Join up GUI
add(pane);
add(dataEntryPanel);
}

// Enable visibity of frame
public static void showFrame() {
JPanel panel = new JTableRowSelectProgramatically();

JFrame frame = new JFrame("Test table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}

// Launch prog
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JTableRowSelectProgramatically.showFrame();
}
});
}

// Create custom table model for data entry
class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"COLUMN 0", "COLUMN 1", "COLUMN 2", "COLUMN 3"};
private Vector data = new Vector();

@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return columnNames.length;
}

@SuppressWarnings("rawtypes")
@Override
public Object getValueAt(int row, int col) {
return ((Vector) data.get(row)).get(col);
}

public String getColumnName(int col){
return columnNames[col];
}

public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}

public void setValueAt(Object value, int row, int col){
((Vector) data.get(row)).setElementAt(value, col);
fireTableCellUpdated(row,col);
}

public boolean isCellEditable(int row, int col){
if (3 == col){
return true;
}
else {
return false;
}
}

public void insertData(Object[] values){
data.add(new Vector());

for(int i =0; i<values.length; i++){

System.out.println("data.size is: " + data.size());
((Vector) data.get(data.size()-1)).add(values[i]);
}

fireTableDataChanged();
}

public void removeRow(int row){
data.removeElementAt(row);
fireTableDataChanged();
}
}
}

删除 JTable 行的新尝试:

 public void deleteRow() {
for (int i = 0; i < getRowCount(); i++) {

Object columnState = getValueAt(i, 3);

System.out.println("STEP 6 - In row " + i + " boolean value is: " + columnState);

boolean columnStateAsBoolean = (Boolean) columnState;

System.out.println("STEP 6 - In row " + i + " Column State As Boolean is: " + columnStateAsBoolean);

if(columnStateAsBoolean == true) {
removeRow(i);
}

System.out.println("-------------------------------------");
}

}

最佳答案

I really want to do is to delete several rows based on boolean states or check boxes

创建一个从最后一行开始并倒数到 0 的循环。

然后,对于每一行,您都使用 table.getValueAt(...) 方法来获取该列的 boolean 值。

如果值为 true,则删除该行。

关于java - 删除 JTable 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15489195/

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