簡體   English   中英

如何通過相反的組合框列JTable設置一列值

[英]how to set one column value by opposite combobox column JTable

我如何將組合框選擇的值設置為其相反的列。

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

您需要使用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);
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM