作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我不明白 prepareEditor 是如何工作的,我无法使用 prepareEditor 中的 Action 为 prepareRenderer 设置可见性规则,正如我在附加图像中捕获的那样
第 1.st 行的规则按我的预期工作,如果未选择 JCheckBox(也使用 JTable 选择 hightlighter),其他 TableCells 是不可见的,但是如果我在 1.st TableColumn 中选择 JCheckBox,那么我不能仅显示 Rule + Mail TableColumn,仅显示 TableRow 中的所有组件,我该如何实现
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableWithCheckBoxOrBlank {
private Object[] columns = new Object[]{
"Select", "Name", "Rule", "Mail", "Include", "Phone"};
private Object[][] data = {
{false, "Bill", false, "Blabla@bla", false, "00 000 000"},
{false, "Edd", false, "Blabla@bla", false, "00 000 000"},
{false, "Paul", false, "Blabla@bla", false, "00 000 000"},
{false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};
public void makeUI() {
DefaultTableModel model = new DefaultTableModel(data, columns) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return column != 1;
}
@Override
public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return Boolean.class;
case 3:
return String.class;
case 4:
return Boolean.class;
case 5:
return String.class;
}
return null;
}
};
final JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
return c;
}
return Box.createRigidArea(c.getPreferredSize());
}
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
Component c = super.prepareEditor(editor, row, column);
if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
return c;
}
return Box.createRigidArea(c.getPreferredSize());
}
};
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getColumn() == 0) {
if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
}
table.repaint();
}
}
});
JScrollPane scroll = new JScrollPane(table);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JFrame frame = new JFrame("Table With Check Box Or Blank");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableWithCheckBoxOrBlank().makeUI();
}
});
}
}
+++
编辑真的没有电池,我看不到树木的森林:-(
感谢@camickr 的帮助,到处都有很多大错误/问题......
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableWithCheckBoxOrBlank {
private Object[] columns = new Object[]{
"Select", "Name", "Rule", "Mail", "Include", "Phone"};
private Object[][] data = {
{false, "Bill", false, "Blabla@bla", false, "00 000 000"},
{false, "Edd", false, "Blabla@bla", false, "00 000 000"},
{false, "Paul", false, "Blabla@bla", false, "00 000 000"},
{false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};
public void makeUI() {
DefaultTableModel model = new DefaultTableModel(data, columns) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
if (column == 0 || column == 2 || column == 4) {
return true;
}
if (column == 1 || column == 3 || column == 5) {
return false;
}
return (Boolean) getValueAt(row, 0);
}
@Override
public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return Boolean.class;
case 3:
return String.class;
case 4:
return Boolean.class;
case 5:
return String.class;
}
return null;
}
};
final JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
return c;
} else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
return c;
} else if (column == 0 || column == 1) {
return c;
}
return Box.createRigidArea(c.getPreferredSize());
}
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
Component c = super.prepareEditor(editor, row, column);
if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
return c;
} else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
return c;
} else if (column == 0 || column == 1) {
return c;
}
return Box.createRigidArea(c.getPreferredSize());
}
};
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getColumn() == 0) {
if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
}
table.repaint();
} else if (e.getColumn() == 2) {
if ((Boolean) table.getValueAt(table.getSelectedRow(), 2)) {
table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 4);
}
table.repaint();
}
}
});
JScrollPane scroll = new JScrollPane(table);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JFrame frame = new JFrame("Table With Check Box Or Blank");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableWithCheckBoxOrBlank().makeUI();
}
});
}
}
最佳答案
我认为 prepareRenderer(...)
代码应该是:
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if ((column == 2 || column == 3) && (Boolean)getValueAt(row, 0)) {
return c;
}
// if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
if (column == 0 || column == 1) {
return c;
}
return Box.createRigidArea(c.getPreferredSize());
}
然后你应该去掉 prepareEditor(...)
代码。相反,您应该在 isCellEditable(...)
方法中包含更多逻辑。像这样的东西:
public boolean isCellEditable(int row, int column) {
if (column == 0) return true;
if (column == 1 || column == 4 || column == 5) return false;
return (Boolean)getValueAt(row, 0);
}
关于java - JTable prepareEditor 是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045851/
我不明白 prepareEditor 是如何工作的,我无法使用 prepareEditor 中的 Action 为 prepareRenderer 设置可见性规则,正如我在附加图像中捕获的那样 第 1
我是一名优秀的程序员,十分优秀!