Java – first item name of JTable jcombobox error

I added a swing to a JTable JCombo@R_695_2419 @, but the tag of my first project was always javax swing. JCombo@R_695_2419 @(…

What on earth did I do wrong?

Update: This is my code:

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultCombo@R_695_2419@Model;
import javax.swing.JCombo@R_695_2419@;
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");

        JCombo@R_695_2419@<String> jcombo1 = new JCombo@R_695_2419@<String>();
        jcombo1.setModel(new DefaultCombo@R_695_2419@Model(labels.toArray()));
        jcombo1.setBackground(Color.WHITE);
        jcombo1.setSelectedIndex(1);
        DefaultCellEditor editor = new DefaultCellEditor(jcombo1);

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

        model.addRow(new JCombo@R_695_2419@[]{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);
    }
}

Solution

The key issues are:

model.addRow(new JCombo@R_695_2419@[]{combo});

Do not add components to the table model Instead, let renderer handle the job using "labels that display object string values" Your initial tag is an empty string, as shown below

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

In addition:

>Code to interface, such as list and lt; String > labels = new ArrayList < > () > starts with event dispatch thread

Test code:

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.DefaultCombo@R_695_2419@Model;
import javax.swing.JCombo@R_695_2419@;
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");

        JCombo@R_695_2419@<String> combo = new JCombo@R_695_2419@<>();
        combo.setModel(new DefaultCombo@R_695_2419@Model(labels.toArray()));
        combo.setBackground(Color.WHITE);
        combo.setSelectedIndex(1);
        DefaultCellEditor editor = new DefaultCellEditor(combo);
        table.getColumnModel().getColumn(0).setCellEditor(editor);
        //model.addRow(new JCombo@R_695_2419@[]{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);
        });
    }
}
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
分享
二维码
< <上一篇
下一篇>>