0

Basically I want to know how to get access to JCombobox functions from JTable at a specific row. If anyone could help me to understand how to set for example first row (only first row - the other rows must not be affected and keep its functional) of the table in its combobox column to select item at index 0 (which is "Item 1").

This is the code that I currently have:

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class ComboboxColumnDemo {

    static JFrame frame;
    static JTable table;

    public static void main(String[] args) {
        createFrameAndTable();

        // Resize to avoid error which makes it invisible.
        frame.resize(500, 600);

        // Turn a column into a combobox
        makeColumnToBeACombobox(1);
    }

    public static void createFrameAndTable() {
        // Create frame
        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setTitle("Demo program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null); // Set frame in the middle of the screen
        frame.setVisible(true);

        // Create table
        table = new JTable();
        table.setModel(new DefaultTableModel(

                // Create rows
                new Object[][] { { null, null, null }, { null, null, null }, },

                // Create headers
                new String[] { "Time", "Address", "Comment" })

        {
            //Set columns to be editable or uneditable
            boolean[] columnEditables = new boolean[] { false, true, false };

            //No idea what it does but it probably needs to be here
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });

        // Create JScrollPane and table to it
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(table);

        // Add JScrollPane with table to the frame
        frame.add(scrollPane);
    }

    /**
     * This method makes a column to be a combobox
     */
    public static void makeColumnToBeACombobox(int whichColumn) {
        JComboBox<String> combobox = new JComboBox<String>();

        // Add items to JComboBox
        combobox.addItem("Item 1");
        combobox.addItem("Item 2");
        combobox.addItem("Item 3");

        // Make column to be a JComboBox column
        table.getColumnModel().getColumn(whichColumn).setCellEditor(new DefaultCellEditor(combobox));
    }
}
Corey
  • 94
  • 10
  • 1
    You simply set the table model's value to the value desired, that's it. The renderer should automatically handle the combobox display for you. – DontKnowMuchBut Getting Better Jun 07 '20 at 14:22
  • This is not what I want. I know that I can use setValueAt() method to put a value. But what I want is something that will getComboboxObjectFromJTable(fromWhichRow ,fromWhichColumn).setSelectedIndex(whichIndex); and it will set the value from the combobox current state... – Corey Jun 07 '20 at 14:24
  • The combo box is shared by all rows in the table. It does not have a current state. The combo box is only active when the cell is being edited at which time its selected state is based on the data obtained from the TableModel. If you are asking how to have a combo box as an editor only in the first row and then have the default editor for the other rows then check out: https://stackoverflow.com/questions/4211452/how-to-add-unique-jcomboboxes-to-a-column-in-a-jtable-java/4211552#4211552 – camickr Jun 07 '20 at 15:14
  • 1
    *"select item at index 1 (which is "Item 1")."* No it isn't. Indexes start at 0. – Andrew Thompson Jun 07 '20 at 16:33
  • Sorry. You are correct. I will fix that. – Corey Jun 11 '20 at 15:53

0 Answers0