gpt4 book ai didi

java - 如何选择JTable的所有行?

转载 作者:行者123 更新时间:2023-12-02 03:09:25 24 4
gpt4 key购买 nike

我正在尝试制作一个 JTable 来存储 boolean 复选框、之前的浏览时间、站点及其 URL。我在其中添加了一个菜单栏,其中包含两个菜单项删除全选

我的问题是全选。我想通过单击来选择表格的所有行。但是当我单击它时,它会使 checkBox (如下所示)的所有元素为 true,但不会在表中显示。 我的意思是我在表格中看不到任何选定的刻度线

下面是我的代码。我哪里做错了?任何帮助将不胜感激。

public class HistList extends JPanel {

JTable table = new JTable();
Object[][] data;
public static ArrayList<String> timeList;
public static ArrayList<String> namelist;
public static ArrayList<String> locTable;
JMenuItem remove = new JMenuItem("Remove");
JMenuItem selectAll = new JMenuItem("Select All");
JScrollPane scrollPane = new JScrollPane();
MyTableModel model = new MyTableModel();
int i = 0;
Hello hello;
public static ArrayList<Boolean> checkBox = new ArrayList<>();

HistList(ArrayList timeList, ArrayList namelist, ArrayList locTable) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
count = new ArrayList<>();
this.dateList = dateList;
this.locTable = locTable;
this.namelist = namelist;
this.timeList = timeList;
this.repeatCount = repeatCount;
String[] title = {" ", "Time", "Name", "Location"};
data = new Object[this.title.size()][4];
for (int j = 0; j < data.length; j++) {
for (int k = 0; k < 4; k++) {
if (k == 0) {
data[j][k] = false;
} else if (k == 1) {
data[j][k] = this.timeList.get(j);
} else if (k == 2) {
data[j][k] = this.namelist.get(j);
} else if (k == 3) {
data[j][k] = this.locTable.get(j);
}
}
}
for (int i = 0; i < data.length; i++) {
model.addRow(data[i]);
checkBox.add(false);
}
this.table = new JTable(model);
this.table.setShowGrid(false);

this.scrollPane = new JScrollPane(this.table);
Dimension size = new Dimension(510, 380);
this.scrollPane.setPreferredSize(size);
JPanel contentPane = new JPanel();
contentPane.add(scrollPane);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable) e.getSource();
//do something
}
if (e.getClickCount() == 2) {
try {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
//do something
} catch (IOException ex) {
Logger.getLogger(HistList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
JMenuBar organize = new JMenuBar();
JMenu menu = new JMenu("Organize");
menu.add(remove);
menu.add(selectAll);
selectAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("in select all");
checkBox.clear();
for(int i=0;i<model.getRowCount();i++)
model.setValueAt(true, i,0 );
table.revalidate();
table.validate();
}
});
selectAll.setEnabled(true);
remove.setEnabled(false);
remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int num = 0;
for (int j = model.getRowCount() - 1; j >= 0; j--) {
System.out.println(model.getValueAt(j, 2));
System.out.println(model.getRowCount());
if (checkBox.get(j)) {
model.removeRow(j);
model.fireTableDataChanged();
}
}
}
});
organize.add(menu);

JPanel finale = new JPanel(new BorderLayout());
finale.add(organize, BorderLayout.NORTH);
finale.add(contentPane, BorderLayout.CENTER);
add(finale);
}

public void createAndShowGUI() {
JFrame frame = new JFrame("History");
HistList newContentPane = new HistList(timeList, namelist, locTable);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}

public class MyTableModel extends DefaultTableModel {

public MyTableModel() {
super(new String[]{" ", "Time", "Name", "Location"}, 0);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Class clazz = String.class;
switch (columnIndex) {
case 0:
clazz = Boolean.class;
break;
}
return clazz;
}

@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}

@Override
public void setValueAt(Object aValue, int row, int column) {
if (aValue instanceof Boolean && column == 0) {

Vector rowData = (Vector) getDataVector().get(row);
rowData.set(0, (boolean) aValue);
if (checkBox.size() > row) {
checkBox.remove(row);
}
checkBox.add(row, (Boolean) aValue);
System.out.println(row + " " + checkBox.toString());
if (checkBox.contains(true)) {
remove.setEnabled(true);
} else {
remove.setEnabled(false);
}
}
}

}
}

最佳答案

这是一个可能有帮助的简单示例。单击按钮将复选框单元格的 boolean 值设置为 true:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.*;

public class TableCheckBox extends JFrame {

private static final long serialVersionUID = 1L;
private JTable table;

public TableCheckBox() {

setLayout(new GridLayout(2,1));

Object[] columnNames = {"Check"};
Object[][] data = { {false}, {false}, {false}, {false} };
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return Boolean.class;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);

JButton btn = new JButton("Check All");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row=0; row < table.getRowCount(); row++) {
table.setValueAt(true, row, 0);
}
}
} );
add(btn);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableCheckBox frame = new TableCheckBox();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}

关于java - 如何选择JTable的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41270199/

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