我有一个相当复杂的JTable我正在努力。行和列的数量是从列表构建的。然后根据单元格编辑器需要更改的行的哪一列。在Goggling寻找解决方案时我发现了这个: http://www.java2s.com/Code/Java/Swing-Components/EachRowwithdifferentEditorExample.htm
我按原样使用了该代码并运行它以确保它正在使用当前的Java版本并且确实如此。所以我在我已经构建的JTable中使用它,但似乎正在发生的是当我将它应用于多个列时,它最终只能用于第一列。
我整理了一个完全可执行的代码段,我已经评论并设置它来模拟我的数据库数据。代码非常好评,所以应该很容易遵循。这个样本至少对我来说仍然会产生问题。
如果你们中的任何人都能看到错误的话,我将非常感谢你的帮助。
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class EachRowEditorExample extends JFrame {
public EachRowEditorExample() {
super("EachRow Editor Example");
List<Membership> _Memberships = new ArrayList<Membership>();
DefaultTableModel DataModel;
//Lets Start by simluating the data.
for (int z = -2; z < 10; z++)
{
Membership m = new Membership();
m.setId(z);
if (z == -2)
{
m.setMembership("Employee");
m.setEft(false);
}
else if (z == -1)
{
m.setMembership("NONE");
m.setEft(false);
}
else
{
m.setMembership("Membership " + z);
if (z < 3 )
m.setEft(true);
else
m.setEft(false);
}
_Memberships.add(m);
}
//*******************************************************
// Starts Copy and paste from Program
//*******************************************************
//lets build our Data Model
int size = _Memberships.size();
Object[][] rows = new Object[size][];
String[] cols = new String[size];
int x = 0;
for(Iterator<Membership> i = _Memberships.iterator(); i.hasNext(); )
{
Membership side = i.next();
cols[x] = side.getMembership();
Object[] row = new Object[size];
int b = 0;
for(Iterator<Membership> j = _Memberships.iterator(); j.hasNext(); )
{
Membership top = j.next();
if (side.getId() == top.getId() && (side.isEft() && top.isEft()))
{
row[b] = null;
}
else
row[b] = NUHPADX(side.getId(), top.getId());
b++;
}
rows[x] = row;
x++;
}
DataModel = new DefaultTableModel(rows,cols);
//Now Lets create our JTable and configure it.
JTable table = new JTable(DataModel) {
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
Object t = getValueAt(rowIndex, rowIndex);
if (t == null)
return false;
else
return true;
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
Object t = getValueAt(row, column);
if (t == null)
{
c.setBackground(Color.black);
}
else
{
Color PRIMARY_ROW_COLOR = Color.white;
Color ALTERNATE_ROW_COLOR = new Color(0xA9F1ED);
//even index, selected or not selected
if (row % 2 == 0)
{
if (column % 2 == 0)
c.setBackground(PRIMARY_ROW_COLOR.darker());
else
c.setBackground(PRIMARY_ROW_COLOR);
}
else
{
if (column % 2 == 0)
c.setBackground(ALTERNATE_ROW_COLOR.darker());
else
c.setBackground(ALTERNATE_ROW_COLOR);
}
}
return c;
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getTableHeader().setReorderingAllowed(false);
JList rowHeader = new JList(cols);
rowHeader.setCellRenderer(new RowHeaderRenderer(table));
//Lets Place the new table in a Scroll Pane Since there could be alot of data.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setRowHeaderView(rowHeader);
getContentPane().add(scrollPane, BorderLayout.CENTER);
//Now lets Build the Cell Editors
int c = 0;
int r = 0;
for(Iterator<Membership> a = _Memberships.iterator(); a.hasNext();)
{
Membership top = a.next();
EachRowEditor rowEditor = new EachRowEditor(table);
for(Iterator<Membership> b = _Memberships.iterator(); b.hasNext();)
{
Membership side = b.next();
if (side.getId() == top.getId() && (side.isEft() && top.isEft()))
{
//rowEditor.setEditorAt(r, null);
}
else if (side.getId() != top.getId() && (side.isEft() && top.isEft()))
{
NuhpadxCell t = new NuhpadxCell(side, top, new DefaultComboBoxModel(new String[] {"N", "U", "D", "A"}));
t.setSelectedItem(NUHPADX(side.getId(), top.getId()));
rowEditor.setEditorAt(r, new DefaultCellEditor(t));
}
else
{
NuhpadxCell t = new NuhpadxCell(side, top, new DefaultComboBoxModel(new String[] {"N", "A", "H", "X", "P"}));
t.setSelectedItem(NUHPADX(side.getId(), top.getId()));
rowEditor.setEditorAt(r, new DefaultCellEditor(t));
}
r++;
}
table.getColumn(top.getMembership()).setCellEditor(rowEditor);
table.revalidate();
c++;
}
table.removeColumn(table.getColumn("NONE"));
//*******************************************************
// Ends Copy and paste from Program
//*******************************************************
setSize(1060, 600);
setVisible(true);
}
//This function jsut simluates a entire class
public String NUHPADX(int side, int top)
{
Random generator = new Random();
int roll = generator.nextInt(7);
switch (roll)
{
case 0:
return "N";
case 1:
return "U";
case 2:
return "H";
case 3:
return "P";
case 4:
return "A";
case 5:
return "P";
case 6:
return "A";
default:
return "N";
}
}
public static void main(String[] args) {
EachRowEditorExample frame = new EachRowEditorExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html
/* (swing1.1.1) */
class EachRowEditor implements TableCellEditor {
protected Hashtable editors;
protected TableCellEditor editor, defaultEditor;
JTable table;
/**
* Constructs a EachRowEditor. create default editor
*
* @see TableCellEditor
* @see DefaultCellEditor
*/
public EachRowEditor(JTable table) {
this.table = table;
editors = new Hashtable();
defaultEditor = new DefaultCellEditor(new JTextField());
}
/**
* @param row
* table row
* @param editor
* table cell editor
*/
public void setEditorAt(int row, TableCellEditor editor) {
editors.put(new Integer(row), editor);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
editor = (TableCellEditor) editors.get(new Integer(row));
if (editor == null) {
editor = defaultEditor;
}
return editor.getTableCellEditorComponent(table, value, isSelected, row, column);
}
public Object getCellEditorValue() {
return editor.getCellEditorValue();
}
public boolean stopCellEditing() {
return editor.stopCellEditing();
}
public void cancelCellEditing() {
editor.cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
selectEditor((MouseEvent) anEvent);
return editor.isCellEditable(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
editor.addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
editor.removeCellEditorListener(l);
}
public boolean shouldSelectCell(EventObject anEvent) {
selectEditor((MouseEvent) anEvent);
return editor.shouldSelectCell(anEvent);
}
protected void selectEditor(MouseEvent e) {
int row;
if (e == null) {
row = table.getSelectionModel().getAnchorSelectionIndex();
} else {
row = table.rowAtPoint(e.getPoint());
}
editor = (TableCellEditor)editors.get(new Integer(row));
if (editor == null) {
editor = defaultEditor;
}
}
}
//*******************************************************
// Starts Copy and paste from Program
//*******************************************************
class RowHeaderRenderer extends JLabel implements ListCellRenderer {
private static final long serialVersionUID = 4619707414623897299L;
public RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;
}
}
class NuhpadxCell extends JComboBox {
private static final long serialVersionUID = -4464709511574911230L;
private Membership _side;
private Membership _top;
public NuhpadxCell(Membership s, Membership t, DefaultComboBoxModel aModel)
{
_side = s;
_top = t;
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DoUpdate();
}
});
setModel(aModel);
}
private void DoUpdate() {
// I would then update my database useing the ID of the side and top memberships as keys.
}
}
//*******************************************************
// Ends Copy and paste from Program
//*******************************************************
//A bare Skeleton of the membership class needed to run this code.
class Membership {
protected int id;
protected String membership;
protected boolean eft;
public int getId() {
return id;
}
public void setId(int value) {
this.id = value;
}
public String getMembership() {
return membership;
}
public void setMembership(String value) {
this.membership = value;
}
public boolean isEft() {
return eft;
}
public void setEft(boolean value) {
this.eft = value;
}
}
答案 0 :(得分:2)
1.issue来自代码行,
table.removeColumn(table.getColumn("NONE"));
2.然后JTables view
会返回与column index
中存储的TableModel
不同的table.convertColumnIndexToModel()
,您必须在prepareRenderer
和Editor
中致电isCellXxx
太
3.从JTable
移除Model
到DataModel = new DefaultTableModel(rows, cols) {
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
Object t = getValueAt(rowIndex, rowIndex);
if (t == null) {
return false;
} else {
return true;
}
}
};
e.i。
table.setPreferredScrollableViewportSize(table.getPreferredSize());
4.call setSize(1060, 600);
然后您可以将pack();
更改为setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5.还有JFrame
6.don&#39; t InitialThread
,创建一个局部变量
7.然后你的主要课程应该包括public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
EachRowEditorExample frame = new EachRowEditorExample();
}
});
}
Column
修改
1,我只是想隐藏那个列并没有真正完全从模型中删除它,这就是代码应该做的事情(根据Java Docs)。
table.removeColumn(table.getColumn("NONE"));
remove concrete Column
only from JTables View
more in the API
and JTables tutorial
,此XxxTableModel
仍然出现在{{1}}中,最好是在您旁边进行测试
中对此进行了详细介绍至于2,不确定为什么真的适用。
答案 1 :(得分:2)
对于看起来非常简单的事情,您的解决方案似乎非常复杂。实际上你只有两个不同的CellEditor,它们都基于JComboBox。
因此,更简单的解决方案是使用DefaultCellEditor
扩展JComboBox
并覆盖getTableCellEditorComponent(),具体取决于您所在的行,使用适当的模型更新JComboBox
然后将呼叫返回super
。
最后,在表格的所有列上设置此单元格编辑器。
还要考虑遵循Java编码约定(变量和方法以小写字母开头,类以大写字母开头等等)
编辑:
class MyTableCellEditor extends DefaultCellEditor {
private DefaultCellEditor defaultEditor;
private DefaultComboBoxModel nudaModel = new DefaultComboBoxModel(new String[] { "N", "U", "D", "A" });
private DefaultComboBoxModel nahxpModel = new DefaultComboBoxModel(new String[] { "N", "A", "H", "X", "P" });
public MyTableCellEditor() {
super(new JComboBox());
defaultEditor = new DefaultCellEditor(new JTextField());
}
private JComboBox getComboBox() {
return (JComboBox) editorComponent;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
Membership top = _Memberships.get(column);
Membership side = _Memberships.get(row);
if (side.getId() == top.getId() && side.isEft() && top.isEft()) {
return defaultEditor.getTableCellEditorComponent(table, value, isSelected, row, column);
} else if (side.getId() != top.getId() && side.isEft() && top.isEft()) {
getComboBox().setModel(nudaModel);
} else {
getComboBox().setModel(nahxpModel);
}
return super.getTableCellEditorComponent(table, NUHPADX(side.getId(), top.getId()), isSelected, row, column);
}
}
这里是TableModel的实现:
class MyDataModel extends AbstractTableModel{
@Override
public int getRowCount() {
return _Memberships.size();
}
@Override
public int getColumnCount() {
return _Memberships.size();
}
@Override
public String getColumnName(int column) {
// Here feel free to return the appropriate column names.
return super.getColumnName(column);
}
@Override
public Object getValueAt(int row, int column) {
return NUHPADX(_Memberships.get(row).getId(), _Memberships.get(column).getId());
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// Here you can do whatever you want to say that is editable or not
// If it is editable, you need to override setValueAt
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// Here you need to implement the update of your model
}
}