你clearl y誤解了編輯和渲染器的角色。編輯器允許用戶編輯單元格的狀態,渲染器呈現單元格的狀態。
他們通過使用TableModel
來做到這一點。首先查看Concepts: Editors and Renderers和Using Other Editors瞭解更多詳情。
要開始與你正在做錯誤的事情......
signalTable.setModel(new DefaultTableModel(
new Object[][] {
{"AAA", "A_SIGNAL", null, null, null, null, null, null, null, null, "Example", Boolean.TRUE},
},
new String[] {
"ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
}
) {
Class[] columnTypes = new Class[] {
String.class, String.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, String.class, Boolean.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
的「顏色」單元的單元值應true
或false
(也可以是其他任何「開」 /「關」值你想要的,只要編輯器和渲染器知道如何對付他們,但在這個例子中,我使用boolean
S)
的columnType
S表示這些細胞也應Boolean.class
signalTable.setModel(new DefaultTableModel(
new Object[][]{
{"AAA", "A_SIGNAL", false, false, false, false, false, false, false, false, "Example", Boolean.TRUE},},
new String[]{
"ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
}
) {
Class[] columnTypes = new Class[]{
String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, String.class, Boolean.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
所以,現在,如果你什麼也沒做,你會在這些單元中有JCheckBox
,但那不是我們想要的。
public class ButtonEditor extends DefaultCellEditor
{
protected JButton button;
public ButtonEditor(JCheckBox checkBox)
{
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.GREEN);
button.repaint();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
return button;
}
}
在這裏,你忽略它傳遞給你的編輯器JCheckBox
,並創建自己的,但有兩個問題。 DefaultCellEditor
從JCheckBox
獲取單元格值,而不是其他某個狀態(因此始終爲false
),並且您從不配置button
以表示請求時單元格的當前狀態。
因爲我不想處理其他組件,所以我會做一些有點不同的事情。
public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
private JLabel editor;
public ButtonEditor() {
editor = new JLabel();
editor.setBackground(Color.GREEN);
editor.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
editor.setOpaque(!editor.isOpaque());
stopCellEditing();
}
});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof Boolean) {
boolean result = (boolean) value;
editor.setOpaque(!result);
}
return editor;
}
@Override
public Object getCellEditorValue() {
return editor.isOpaque();
}
@Override
public boolean isCellEditable(EventObject e) {
return true;
}
}
這僅僅是一個JLabel
點擊它時,會切換它的不透明狀態。我們也利用這個狀態,以確定單元格的值時,編輯器「停止」
public class TableColorRenderer extends JLabel implements TableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
return this;
}
}
坦白地說,這是一個有點用處,它從來不做事與單元格的值
public class TableColorRenderer extends JLabel implements TableCellRenderer {
public TableColorRenderer() {
setBackground(Color.GREEN);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Boolean) {
boolean result = (boolean) value;
setOpaque(result);
} else {
setOpaque(false);
}
return this;
}
}
再次,非常簡單。我們檢查細胞(如傳遞給我們)的值,並相應地改變opaque
狀態
而且,因爲從來沒有人相信我,一個可運行的例子
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class Example {
public static void main(String[] args) {
new Example();
}
public Example() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private final JTable signalTable;
public TestPane() {
signalTable = new JTable();
signalTable.setModel(new DefaultTableModel(
new Object[][]{
{"AAA", "A_SIGNAL", false, false, false, false, false, false, false, false, "Example", Boolean.TRUE},},
new String[]{
"ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
}
) {
Class[] columnTypes = new Class[]{
String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, String.class, Boolean.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
TableColorRenderer renderer = new TableColorRenderer();
signalTable.getColumn("0").setCellRenderer(renderer);
signalTable.getColumn("1").setCellRenderer(renderer);
signalTable.getColumn("2").setCellRenderer(renderer);
signalTable.getColumn("3").setCellRenderer(renderer);
signalTable.getColumn("4").setCellRenderer(renderer);
signalTable.getColumn("5").setCellRenderer(renderer);
signalTable.getColumn("6").setCellRenderer(renderer);
signalTable.getColumn("7").setCellRenderer(renderer);
signalTable.getColumn("0").setCellEditor(new ButtonEditor());
signalTable.getColumn("1").setCellEditor(new ButtonEditor());
signalTable.getColumn("2").setCellEditor(new ButtonEditor());
signalTable.getColumn("3").setCellEditor(new ButtonEditor());
signalTable.getColumn("4").setCellEditor(new ButtonEditor());
signalTable.getColumn("5").setCellEditor(new ButtonEditor());
signalTable.getColumn("6").setCellEditor(new ButtonEditor());
signalTable.getColumn("7").setCellEditor(new ButtonEditor());
setLayout(new BorderLayout());
add(new JScrollPane(signalTable));
}
}
public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
private JLabel editor;
public ButtonEditor() {
editor = new JLabel();
editor.setBackground(Color.GREEN);
editor.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
editor.setOpaque(!editor.isOpaque());
stopCellEditing();
}
});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof Boolean) {
boolean result = (boolean) value;
editor.setOpaque(!result);
}
return editor;
}
@Override
public Object getCellEditorValue() {
return editor.isOpaque();
}
@Override
public boolean isCellEditable(EventObject e) {
return true;
}
}
public class TableColorRenderer extends JLabel implements TableCellRenderer {
public TableColorRenderer() {
setBackground(Color.GREEN);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Boolean) {
boolean result = (boolean) value;
setOpaque(result);
}
return this;
}
}
}
爲什麼會出現約JButton的說明和代碼顯示JCheckBox的 – mKorbel
1.閱讀甲骨文教程如何使用表,存儲在模型JCheckBox的代表AbstractButton.isSelected布爾值,2。有沒有理由使用自定義(和錯誤的)渲染器和編輯器(缺少stopCellEditing,並將布爾值存儲在XxxTableModel中),然後Renderer沒有存儲任何值,在Renderer – mKorbel