如何通过相反的组合框列JTable设置一个列值

时间:2021-08-27 09:10:54

how can i set the combobox selected value to its opposit column.

如何将组合框选定值设置为其对应列。

List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

      private DefaultTableModel model;
      JTable table;
      JButton buttonSave;
    public tableee() {
        // Create the editors to be used for each row

        initComponents();
        String[] items1 = { "one", "two", "three" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        String[] items2 = { "one", "two", "three" };
        JComboBox comboBox2 = new JComboBox( items2 );
        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
        editors.add( dce2 );

        String[] items3 = { "one", "two", "three" };
        JComboBox comboBox3 = new JComboBox( items3 );
        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
        editors.add( dce3 );

        //  Create the table with default data

        Object[][] data =
        {
            {new String("1"),new String("")}, //enter data-----------------------------
            {new String("2"),new String("")},
            {new String("3"),new String("")},
            {new String("4"),new String("")}

        };
        String[] columnNames = {"Type","Value"};
         model = new DefaultTableModel(data, columnNames);
         table = new JTable(model)
        {
            //  Determine editor to be used by row
            @Override
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(row);
                else
                    return super.getCellEditor(row, column);
            }
        };

      // table.setPreferredScrollableViewportSize(new Dimension(40,50));
       JScrollPane scrollPane = new JScrollPane(table);

setLayout(new BorderLayout());
//setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(BorderLayout.NORTH, new JLabel("Mon panier", JLabel.CENTER));
add(BorderLayout.CENTER, scrollPane);
JButton buttonAdd = new JButton("Add");
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
Object obj1 = GetData(table, 1, 2);
System.out.println("Cell value of 2 column and 3 row :" + obj1);
}
private Object GetData(JTable table, int row_index, int col_index) {
                return table.getModel().getValueAt(row_index, col_index);
            }
        });
}
}

1 个解决方案

#1


0  

You need to use ActionListeners. For example, you can add an ActionListener to each combobox and then change the table when the action is fired:

您需要使用ActionListeners。例如,您可以向每个组合框添加一个ActionListener,然后在触发操作时更改表:

    JComboBox comboBox1 = new JComboBox(items1);
    comboBox1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String value = (String)cb.getSelectedItem();
            table.setValueAt(value, 0, 0);
        }
    });

#1


0  

You need to use ActionListeners. For example, you can add an ActionListener to each combobox and then change the table when the action is fired:

您需要使用ActionListeners。例如,您可以向每个组合框添加一个ActionListener,然后在触发操作时更改表:

    JComboBox comboBox1 = new JComboBox(items1);
    comboBox1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String value = (String)cb.getSelectedItem();
            table.setValueAt(value, 0, 0);
        }
    });