gpt4 book ai didi

java - JTable boolean 类

转载 作者:行者123 更新时间:2023-11-29 03:40:11 24 4
gpt4 key购买 nike

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

class ColorTableModel extends AbstractTableModel {

Object rowData[][] = { { "value1", Boolean.FALSE },
{ "value1", Boolean.FALSE }, { "value1", Boolean.FALSE },
{ "value1", Boolean.FALSE}, { "value1", Boolean.FALSE }, };

String columnNames[] = { "English", "Boolean" };

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

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

public int getRowCount() {
return rowData.length;
}

public Object getValueAt(int row, int column) {
return rowData[row][column];
}

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

public void setValueAt(Object value, int row, int column) {
rowData[row][column] = value;
}

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

public class EditableColorColumn {

public static void main(String args[]) {
JFrame frame = new JFrame("Editable Color Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new ColorTableModel();
JTable table = new JTable(model);
// TableColumn column = table.getColumnModel().getColumn(3);
// column.setCellRenderer(renderer);
// column.setCellEditor(editor);

JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}

如果我勾选相应的行,我想得到第一列的值。我已经阅读了很多关于此的内容,但我不能只是将其写入代码。你能编写一些代码吗?

示例场景,当我勾选 row1 时复选框,它将 system.out.println()结果为 value1 .

最佳答案

您在 AbstractTableModel 中实现的 setValueAt() 未能触发通知监听器更改的事件:

@Override
public void setValueAt(Object value, int row, int column) {
rowData[row][column] = value;
fireTableCellUpdated(row, column);
}

纠正后,TableModelListener 将看到每个更改。尝试注释掉 fireTableCellUpdated() 行以查看差异。

另外:

代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
* @see https://stackoverflow.com/q/13497276/230513
*/
public class EditableColorColumn {

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new EditableColorColumn().display();
}
});
}

private void display() {
JFrame frame = new JFrame("Editable Color Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final TableModel model = new ColorTableModel();
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(400, 150));
table.getModel().addTableModelListener(new TableModelListener() {

@Override
public void tableChanged(TableModelEvent e) {
System.out.println(model.getValueAt(e.getFirstRow(), 0)
+ " " + model.getValueAt(e.getFirstRow(), 1));
}
});

frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private static class ColorTableModel extends AbstractTableModel {

String columnNames[] = {"English", "Boolean"};
Object rowData[][] = {
{"value1", Boolean.FALSE},
{"value2", Boolean.TRUE},
{"value3", Boolean.FALSE},
{"value4", Boolean.TRUE},
{"value5", Boolean.FALSE},};

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

@Override
public String getColumnName(int column) {
return columnNames[column];
}

@Override
public int getRowCount() {
return rowData.length;
}

@Override
public Object getValueAt(int row, int column) {
return rowData[row][column];
}

@Override
public Class getColumnClass(int column) {
return (getValueAt(0, column).getClass());
}

@Override
public void setValueAt(Object value, int row, int column) {
rowData[row][column] = value;
fireTableCellUpdated(row, column);
}

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

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

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