加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JTable JComboBox错误的第一个项目名称

发布时间:2020-12-15 04:31:11 所属栏目:Java 来源:网络整理
导读:我在一个JTable中添加了一个Swing JComboBox,但是我的第一个项目的标签总是 javax.swing.JComboBox(… 我究竟做错了什么? 更新:这是我的代码: import java.awt.Color;import java.awt.Dimension;import java.util.ArrayList;import javax.swing.DefaultCe
我在一个JTable中添加了一个Swing JComboBox,但是我的第一个项目的标签总是 javax.swing.JComboBox(…

enter image description here

我究竟做错了什么?

更新:这是我的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

public class TableSelection extends JPanel {

    private DefaultTableModel model = new DefaultTableModel();
    private JTable table = new JTable(model);

    public TableSelection() {
        model = (DefaultTableModel) table.getModel();
        ArrayList<String> labels = new ArrayList<String>();
        labels.add("");
        for (int i = 1; i < 10 + 1; i++) {
            labels.add("" + (i - 1));
        }

        model.addColumn("Column");
        model.addColumn("Column2");

        JComboBox<String> jcombo1 = new JComboBox<String>();
        jcombo1.setModel(new DefaultComboBoxModel(labels.toArray()));
        jcombo1.setBackground(Color.WHITE);
        jcombo1.setSelectedIndex(1);
        DefaultCellEditor editor = new DefaultCellEditor(jcombo1);

        table.getColumnModel().getColumn(0).setCellEditor(editor);

        model.addRow(new JComboBox[]{jcombo1});

        table.setPreferredScrollableViewportSize(new Dimension(560,300));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.add(new JScrollPane(table));
        table.setRowSelectionInterval(0,0);

    }

    public static void main(String[] args) {
        JFrame f = new JFrame("TableSelection");
        f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new TableSelection());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

解决方法

关键问题在于:

model.addRow(new JComboBox[]{combo});

不要将组件添加到表模型中.相反,让renderer使用“显示对象字符串值的标签”来处理作业.您的初始标签是一个空字符串,如下所示.

model.addRow(new String[]{labels.get(0)});

image

此外:

>代码到界面,例如列表与LT;字符串> labels = new ArrayList<>()
>从event dispatch thread开始.

测试代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

public class TableSelection extends JPanel {

    private final DefaultTableModel model = new DefaultTableModel();
    private final JTable table = new JTable(model);

    public TableSelection() {
        List<String> labels = new ArrayList<>();
        labels.add("");
        for (int i = 1; i < 10 + 1; i++) {
            labels.add("" + (i - 1));
        }
        model.addColumn("Column 1");
        model.addColumn("Column 2");

        JComboBox<String> combo = new JComboBox<>();
        combo.setModel(new DefaultComboBoxModel(labels.toArray()));
        combo.setBackground(Color.WHITE);
        combo.setSelectedIndex(1);
        DefaultCellEditor editor = new DefaultCellEditor(combo);
        table.getColumnModel().getColumn(0).setCellEditor(editor);
        //model.addRow(new JComboBox[]{combo});
        model.addRow(new String[]{labels.get(0)});
        table.setPreferredScrollableViewportSize(new Dimension(320,120));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.add(new JScrollPane(table));
        table.setRowSelectionInterval(0,0);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame f = new JFrame("TableSelection");
            f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new TableSelection());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读