JXTable 中 JComboBox 的问题

发布于 2025-01-02 06:10:56 字数 2336 浏览 1 评论 0原文

我正在尝试将 JXTable 中的一列设为组合框。很难解释我面临的问题。当您单击下拉菜单时,有时下拉菜单不会保持打开状态足够长的时间,以便您选择值。它只是关闭并选择一些值。有时它会保持开放状态供您选择一个值。 这是 SSCCE:

import java.awt.Color;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.CellContext;
import org.jdesktop.swingx.renderer.ComponentProvider;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;

public class Test {

    public static void main(String[] args) {
        JXFrame frame=new JXFrame();
        Object[][] rows = new Object[10][];

        for (int i = 0; i < rows.length; i++) {
        rows[i] = new Object[]{
        "Test data ","Yes"};
        }

        DefaultTableModel model = new DefaultTableModel(rows,
        new String[]{
        "Title 1", "Title 2"
        });

        final JXTable table = new JXTable(model);
        DefaultComboBoxModel cmodel = new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"});
        table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(cmodel)));
        table.getColumnExt(1).setCellEditor(new ComboBoxEditor(cmodel));
        table.setVisibleRowCount(10);
    frame.setContentPane(table);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
        frame.setVisible(true);
    }
}

class ComboBoxEditor extends DefaultCellEditor {
    public ComboBoxEditor(ComboBoxModel model) {
        super(new JComboBox(model));
    }
}

class ComboBoxProvider extends ComponentProvider<JComboBox> {
    private static final long serialVersionUID = 1L;
    private JComboBox box;
    public ComboBoxProvider(ComboBoxModel model){
        box.setModel(model);
    }
    @Override
    protected void configureState(CellContext context) {
        box.setForeground(Color.black);
    }
    @Override
    protected JComboBox createRendererComponent() {
        box = new JComboBox();
        box.setForeground(Color.black);
        return box;
    }
    @Override
    protected void format(CellContext context) {
        box.setForeground(Color.black);
        rendererComponent.setSelectedItem(context.getValue());
    }
}

I am trying to make a column in a JXTable to be a combo box. Its difficult to explain the problem I am facing. When you click on the drop down, at times the drop down does not stay open long enough for you to choose the value. It just closes and some value is chosen. At times it stays open for you to choose a value.
Here's the SSCCE:

import java.awt.Color;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.CellContext;
import org.jdesktop.swingx.renderer.ComponentProvider;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;

public class Test {

    public static void main(String[] args) {
        JXFrame frame=new JXFrame();
        Object[][] rows = new Object[10][];

        for (int i = 0; i < rows.length; i++) {
        rows[i] = new Object[]{
        "Test data ","Yes"};
        }

        DefaultTableModel model = new DefaultTableModel(rows,
        new String[]{
        "Title 1", "Title 2"
        });

        final JXTable table = new JXTable(model);
        DefaultComboBoxModel cmodel = new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"});
        table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(cmodel)));
        table.getColumnExt(1).setCellEditor(new ComboBoxEditor(cmodel));
        table.setVisibleRowCount(10);
    frame.setContentPane(table);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
        frame.setVisible(true);
    }
}

class ComboBoxEditor extends DefaultCellEditor {
    public ComboBoxEditor(ComboBoxModel model) {
        super(new JComboBox(model));
    }
}

class ComboBoxProvider extends ComponentProvider<JComboBox> {
    private static final long serialVersionUID = 1L;
    private JComboBox box;
    public ComboBoxProvider(ComboBoxModel model){
        box.setModel(model);
    }
    @Override
    protected void configureState(CellContext context) {
        box.setForeground(Color.black);
    }
    @Override
    protected JComboBox createRendererComponent() {
        box = new JComboBox();
        box.setForeground(Color.black);
        return box;
    }
    @Override
    protected void format(CellContext context) {
        box.setForeground(Color.black);
        rendererComponent.setSelectedItem(context.getValue());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2025-01-09 06:10:56

要解决此问题,请确保您不为编辑器和渲染器共享相同的 DefaultComboxBoxModel 实例。因此,不要这样做:

DefaultComboBoxModel cmodel = new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"});
    table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(cmodel)));
    table.getColumnExt(1).setCellEditor(new ComboBoxEditor(cmodel));

这样做:

    table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(
             new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"}))));
    table.getColumnExt(1).setCellEditor(new ComboBoxEditor(
            new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"})));

To solve this issues make sure you dont share the same instance of the DefaultComboxBoxModel for the editor and renderer.. So instead of this:

DefaultComboBoxModel cmodel = new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"});
    table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(cmodel)));
    table.getColumnExt(1).setCellEditor(new ComboBoxEditor(cmodel));

Do this:

    table.getColumnExt(1).setCellRenderer(new DefaultTableRenderer(new ComboBoxProvider(
             new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"}))));
    table.getColumnExt(1).setCellEditor(new ComboBoxEditor(
            new DefaultComboBoxModel(new String[] {"Yes","No","Maybe"})));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文