How do I use jlists in JTable cells?

I want a simple way to put JList in the JTable column I already have jlists and tables, but when I put them into the table, jlists displays as strings, which is normal because I use defaulttablemodel I have rewritten getcolumnclass () to:

public Class<? extends Object> getColumnClass(int c)
{
    return getValueAt(0,c).getClass();
}

But this is just formatting integer and floating point values

I think setvalueat () and getvalueat () should also be overridden in order to call JList Getselectedvalues() returns an array of strings, but I can't figure out how I also want cells to be editable, so users can choose one or more options from JList After editing a row, I use the Save button to save the changes in the database, so I don't think I need listselectionlistener, JList Getselectedvalues () works properly

I know this is a common question, but I can't find the answer here If this is a duplicate, please let me know and I will delete it

Solution

I did it. For everyone who needs the same thing, that's what I did:

1) I created a jscrolltablerenderer and set the columns I need to display JList to use this renderer

table.getColumnModel().getColumn(5).setCellRenderer(new JScrollTableRenderer());

Jscrolltablerenderer class content:

public class JScrollTableRenderer extends DefaultTableCellRenderer {

JScrollPane pane = new JScrollPane();

public JScrollTableRenderer()
{
    super();
}

public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
{
    pane = (JScrollPane) value;
    return pane;
}
}

2) I created a jscrolltable editor and set the columns I need to display JList to use the editor

table.getColumnModel().getColumn(5).setCellEditor(new JScrollTableEditor());

Jscrolltableeditor class content:

public class JScrollTableEditor extends AbstractCellEditor implements TableCellEditor {
    JScrollPane component = new JScrollPane();
    public Component getTableCellEditorComponent(JTable table,int rowIndex,int vColIndex)
    {
        component = ((JScrollPane) value);
        return ((JScrollPane) value);
    }

    public Object getCellEditorValue()
    {
        return component;
    }

    }

3) I added this method to the JTable model:

public Class<? extends Object> getColumnClass(int c)
            {
                if(c == 5) return JScrollPane.class;
                else return getValueAt(0,c).getClass();
            }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>