4
我不明白prepareEditor是如何工作的,我爲prepareRenderer與行動從prepareEditor不能設置可見規則,正如我在附加的圖像捕捉JTable中prepareEditor如何工作
規則1。 st行按照我的預期工作,其他TableCells是不可見的,如果JCheckBox沒有被選中(使用JTable選擇hightlighter),但是如果我在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, "[email protected]", false, "00 000 000"},
{false, "Edd", false, "[email protected]", false, "00 000 000"},
{false, "Paul", false, "[email protected]", false, "00 000 000"},
{false, "ZOO", false, "[email protected]", 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();
}
});
}
}
+++
編輯真的沒有baterry在內,我看不到樹不見林:-(
感謝@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, "[email protected]", false, "00 000 000"},
{false, "Edd", false, "[email protected]", false, "00 000 000"},
{false, "Paul", false, "[email protected]", false, "00 000 000"},
{false, "ZOO", false, "[email protected]", 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();
}
});
}
}
很難說任何+1 – mKorbel