我不明白prepareEditor的工作方式,我无法通过prepareEditor中的Action为prepareRenderer设置可见性规则,因为我在所附的Image中捕获了该信息
1.st行的规则符合我的预期,如果未选择JCheckBox(其他JTable选择也同样突出),则其他TableCell将不可见,但是如果我在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);
}
SVN是基于客户/服务器模式: 复制-修改-合并方案(Subversion默认的模式): 在这种模型里,每一个客户读取项目配置库建立一个私有工作副本——版本库中文件和目录的本地映射。用户并行工作,修改各自的工作副本,最终,各个私有的复制合并在一起,成为最终的版本,这种系统通常可以辅助合并操作,但是最终要靠人工去确定正误。 锁定-修改-解锁方案 在这样的模型里,在一个时间段里配置库的一个文件只允许被
我们平时浏览网页的时候,会打开浏览器,输入网址后按下回车键,然后就会显示出你想要浏览的内容。在这个看似简单的用户行为背后,到底隐藏了些什么呢? 对于普通的上网过程,系统其实是这样做的:浏览器本身是一个客户端,当你输入URL的时候,首先浏览器会去请求DNS服务器,通过DNS获取相应的域名对应的IP,然后通过IP地址找到IP对应的服务器后,要求建立TCP连接,等浏览器发送完HTTP Request(请
以下是我的代码块: 控制台无错误;但内容不仅仅是附加的。
问题内容: 我正在尝试使用比较器来帮助对对象列表进行排序。在以下示例中,我对比较器的工作原理以及确切的工作方式有疑问: 从上面可以看到,有必要根据方法返回的整数排名对学生进行比较和排序。顾名思义,这是一项繁重的操作。以上方法会是最有效的吗?或者基本上遍历我要排序的列表中的每个学生,执行每个学生,然后将结果存储在Student对象的字段中会更好。然后,比较器将执行以下操作: 这两种方法是否具有可比性
我正在尝试使用比较器来帮助排序对象列表。我有一个问题,关于比较器到底是如何工作的,以及它在下面的示例中到底会做什么: 如上面所示,需要根据方法返回的整数等级对学生进行比较和排序。顾名思义,这是一项繁重的操作。上述方法是否最有效?或者更好的做法是遍历我试图排序的列表中的每个学生,对每个学生执行,并将结果存储在student对象的字段中。然后,比较器将执行以下操作: 编辑:让我们假设,为了它,不可能添