简体   繁体   English

JTable,JComboBox - 在第二列中显示JComboBox的问题

[英]JTable, JComboBox - problems in showing JComboBox in second column

I have written this simple program: 我写了这个简单的程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JcomboboxJtableDemo  extends JPanel
                          implements ActionListener {

    private DefaultTableModel tableModel;
    JTable table = new JTable (tableModel);
    private JScrollPane scrollpaneTable = new JScrollPane( table );
    private JPanel PaneBottoniTabella = new JPanel( );

    public JcomboboxJtableDemo() {
        super(new BorderLayout());

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        JComboBox comboBox = new JComboBox(petStrings);
        comboBox.setSelectedIndex(4);

        tableModel = CreateTableModel();

        tableModel.insertRow( 0, new Object[] {"Header col1", ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[0], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[1], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[2], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[3], ""} );
        tableModel.setValueAt("Header col2", 0, 1); 

        DefaultCellEditor editor = new DefaultCellEditor(comboBox);
        table.getColumnModel().getColumn(0).setCellEditor(editor);
        table.getColumnModel().getColumn(1).setCellEditor(editor);



        //Lay out the demo.
        add(comboBox, BorderLayout.PAGE_START);
        add(table, BorderLayout.PAGE_END);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }


    private final DefaultTableModel CreateTableModel () {
     DefaultTableModel modello = new DefaultTableModel( new Object[] { "Col1","Col2" }, 0 ) {
        @Override
        public boolean isCellEditable(int row, int column) {
          return true;
        }
     };  
    table.setModel(modello);
        return modello;
   } 

   private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new JcomboboxJtableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
   public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I you try to run it you will see that there is a problem in showing correctly the JComboBox components in the second column, in the first column the are correctly shown and you can see each selected item as set in the code, instead in the second column there are some problems: none on the relative cell. 我试图运行它你会看到在第二列中正确显示JComboBox组件时出现问题,在第一列中正确显示,你可以看到代码中设置的每个选定项目,而不是第二列列有一些问题:相对单元格没有。

Could you tell me why? 你能告诉我为什么吗? How can I solve the problem? 我该如何解决这个问题?

Thanks 谢谢

You're using the same JComboBox component for both ColumnModel columns which in turn share the same ComboBoxModel . 您对两个ColumnModel列使用相同的JComboBox组件,而这些列又共享相同的ComboBoxModel Any change in the selected item from one column will be reflected in the other column. 所选项目从一列中的任何更改都将反映在另一列中。 Create a second combobox 创建第二个组合框

JComboBox comboBox2 = new JComboBox(petStrings);
...
table.getColumnModel().getColumn(1).setCellEditor(editor2);

so that any changes can occur independently in either column. 这样任何更改都可以在任一列中独立发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM