我正在尝试创建一个JTable,它存储布尔复选框,以前的浏览时间,网站及其URL。我在其中添加了一个菜单栏,其中包含两个菜单项Remove和Select All。
我的问题是选择全部。我想通过单击选择所有表行。但是当我点击它时,它会使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);
}
}
}
}
}
这是一个可能有用的简单示例。单击该按钮将复选框单元格的布尔值设置为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);
}
});
}
}
只需在选择数据时应用JTable
的基本代码。防爆。 jTable.SelectAll()
允许用户选择表中的所有数据。
if (buttonclicked) {
for (int i = 0; i < jTable1.getRowCount(); i++) {
jTable1.setValueAt(true, i, 0);
}
} else {
for (int i = 0; i < jTable1.getRowCount(); i++) {
jTable1.setValueAt(false, i, 0);
}
}
此代码选择同一列上的所有复选框,并取消选择,例如取消选中复选框。