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

时间:2013-11-17 20:15:57

标签: java swing jtable jcombobox

我写了这个简单的程序:

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();
            }
        });
    }
}

我尝试运行它,您会发现在第二列中正确显示JComboBox组件时出现问题,在第一列中正确显示了这些组件,您可以看到每个所选项目已设置为代码,而在第二列中存在一些问题:相对单元格上没有。

你可以告诉我为什么吗?我该如何解决这个问题呢?

由于

1 个答案:

答案 0 :(得分:2)

您对两个JComboBox列使用相同的ColumnModel组件,后者又共享相同的ComboBoxModel。所选项目从一列中的任何更改都将反映在另一列中。创建第二个组合框

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

以便在任一列中可以独立地进行任何更改。