我遇到了问题。
我有一个JTable,在第一列中包含描述符,然后是不同类型的 第二列中的JComponents(标准为JTextField,布尔值为JCheckBox,整数为JCombobox) 我创建了自己的Cellrenderer,但是当我尝试更改(编辑)f.e的值时。 JCheckBox,CheckBox消失,并且有一个普通的TextField。
所以我尝试编写自己的CellEditor,但它并没有真正起作用。 你们可以帮帮我吗?
由于 Developer_X
String[] columnNames =
{"", ""};
Object[][] data =
{
{ "A", ""},
{ "B", ""},
{ "C", new Integer(0) },
{ "D", new Boolean(true) },
{ "E", "A.B.C" },
{ "F", "Rhababer" }
};
JTable table = new JTable(data, columnNames)
{
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column)
{
if(column==0)
return false;
else
return true;
}
};
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setDefaultRenderer( Object.class, new FormalDataCellRenderer(table.getDefaultRenderer(Object.class)));
table.setDefaultRenderer( Boolean.class, new FormalDataCellRenderer(table.getDefaultRenderer(Boolean.class)));
table.setDefaultRenderer( Integer.class, new FormalDataCellRenderer(table.getDefaultRenderer(Integer.class)));
table.setDefaultEditor(Object.class, new FormalDataCellEditor(table.getCellEditor()));
// FormalDataCellRenderer
public class FormalDataCellRenderer implements TableCellRenderer
{
private TableCellRenderer normal;
public FormalDataCellRenderer(TableCellRenderer cellRenderer)
{
super();
this.normal = cellRenderer;
}
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
{
if(column==1)
{
if(value instanceof Boolean)
{
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));
typeSelection.setSelectedIndex((Integer)(value));
return typeSelection;
}
}
return normal.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
// FormalDataCellEditor
public class FormalDataCellEditor extends DefaultCellEditor
{
public FormalDataCellEditor(TableCellEditor editor)
{
super(new JCheckBox()); //? What shall I do?
}
@Override
public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
{
if(column==1)
{
System.out.println(value.toString());
if(value instanceof Boolean)
{
if(value.equals(new Boolean(true)))
{
value = new Boolean(false);
}
else
{
value = new Boolean(true);
}
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));
int selected = (Integer)(value);
if(selected<typeSelection.getItemCount()-1)
selected++;
else
selected = 0;
typeSelection.setSelectedIndex(selected);
value = new Integer(selected);
return typeSelection;
}
}
return super.getTableCellEditorComponent(table, value, isSelected,row, column);
}
}
答案 0 :(得分:0)
所以这是我的完整例子。
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class EasyExample
{
// Attributes
// Constructor
public EasyExample()
{
}
// Methods
public static void main(String[] args)
{
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame();
frame.setTitle("Table Example");
frame.setSize(screen.width/4, screen.height/2);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
JPanel Panel = new JPanel();
Panel.setLayout(new BoxLayout(Panel,BoxLayout.Y_AXIS));
frame.add(Panel,"Center");
JPanel formalData = new JPanel();
formalData.setBorder(BorderFactory.createTitledBorder("Formal Data"));
formalData.setLayout(new BorderLayout());
Panel.add(formalData);
String[] columnNames = {"", ""};
Object[][] data =
{
{ "A", ""},
{ "B", ""},
{ "C", new Integer(0)},
{ "D", new Boolean(true)},
{ "E", "A.B.C" },
{ "F", "Rhababer"}
};
JTable table = new JTable(data, columnNames)
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column)
{
if(column==0)
return false;
else
return true;
}
};
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setDefaultRenderer( Object.class, new FormalDataCellRenderer(table.getDefaultRenderer(Object.class)));
table.setDefaultRenderer( Boolean.class, new FormalDataCellRenderer(table.getDefaultRenderer(Boolean.class)));
table.setDefaultRenderer( Integer.class, new FormalDataCellRenderer(table.getDefaultRenderer(Integer.class)));
table.setDefaultEditor(Object.class, new FormalDataCellEditor(table.getCellEditor()));
formalData.add(new JScrollPane(table));
frame.setVisible(true);
}
static class FormalDataCellRenderer implements TableCellRenderer
{
private TableCellRenderer normal;
public FormalDataCellRenderer(TableCellRenderer cellRenderer)
{
super();
this.normal = cellRenderer;
}
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
{
if(column==1)
{
if(value instanceof Boolean)
{
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem("male");
typeSelection.addItem("female");
typeSelection.setSelectedIndex((Integer)(value));
return typeSelection;
}
}
return normal.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
static class FormalDataCellEditor extends DefaultCellEditor
{
public FormalDataCellEditor(TableCellEditor editor)
{
super(new JCheckBox()); //? What shall I do?
}
@Override
public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
{
if(column==1)
{
if(value instanceof Boolean)
{
if(value.equals(new Boolean(true)))
{
value = new Boolean(false);
}
else
{
value = new Boolean(true);
}
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem("male");
typeSelection.addItem("female");
int selected = (Integer)(value);
if(selected<typeSelection.getItemCount()-1)
selected++;
else
selected = 0;
typeSelection.setSelectedIndex(selected);
value = new Integer(selected);
return typeSelection;
}
}
return super.getTableCellEditorComponent(table, value, isSelected,row, column);
}
}
}