Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Hugo

Thread: Comboboxes and JTables

  1. #1
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default Comboboxes and JTables

    Hi everyone

    I would like to know if someone can help me with a problem on comboboxes / dropdown boxes?

    I have a table (JTable). Both AbstractTableModel and DefaultTableModel implementations gives me the same problem.

    The table has multiple columns and multiple rows. One of the columns contain comboboxes from which values can be chosen.

    The problem has more to do with the event handling when all the values in the comboboxes are selected.

    If you then click on a dropdownbox (bringing up a list of values to choose from), the event handler fires again, returning the row index of the previous row (if you called the getSelectedRow in the event handler).

    Is there any way that I can click on a combobox and get the index of the selected row in which the combobox resides instead of the previous row's index?

    There are 3 classes. One class is the Main, the other is just a GUI class and the problem is with the third class.

    Main Class

    Java Code:
    package TableAndComboBox;
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            GUI gui = new GUI();
        }
    }
    GUI Class

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class GUI extends JFrame {
    
        public GUI()
        {
            JPanel panel = new JPanel();
            JButton but1 = new JButton("Ok");
            TableCombobox1 tableCombobox = new TableCombobox1();
    
            panel.setLayout(new BorderLayout());
    
            panel.add(but1, BorderLayout.NORTH);
    
            this.add(panel, BorderLayout.NORTH);
            this.add(tableCombobox, BorderLayout.SOUTH);
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(320, 240);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    }

    Problem Class

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    public class TableCombobox1 extends JPanel implements ItemListener {
    
        ZebraJTable table;
    
        public TableCombobox1() {
    
            JPanel panel1 = new JPanel(new BorderLayout());
    
            table = new ZebraJTable(); //= createTable(); //new JTable();
            table.setModel(setupDefaultTableModel());
    
            table.setRowSelectionInterval(0, 0);
    
            TableColumn sportColumn = table.getColumnModel().getColumn(5);
    
            JComboBox comboBox = new JComboBox();
            comboBox.addItem("Test 1");
            comboBox.addItem("Test 2");
            comboBox.addItem(Test 3");
            sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
            comboBox.addItemListener(this);
    
            panel1.add(table, BorderLayout.CENTER);
            panel1.setVisible(true);
            this.add(panel1);
            this.setVisible(true);
        }
    
        public DefaultTableModel setupDefaultTableModel()
        {
            DefaultTableModel defTableModel = new DefaultTableModel();
    
            defTableModel.addColumn("First Name");
            defTableModel.addColumn("Last Name");
            defTableModel.addColumn("Sport");
            defTableModel.addColumn("# of Years");
            defTableModel.addColumn("Vegetarian");
            defTableModel.addColumn("Choose");
    
            Object[] obj = {"One", "Two", "Three", "Four", "Five"};
            Object[] obj1 = {"One", "Two", "Three", "Four", "Five"};
            Object[] obj2 = {"One", "Two", "Three", "Four", "Five"};
    
            defTableModel.addRow(obj);
            defTableModel.addRow(obj1);
            defTableModel.addRow(obj2);
    
            return defTableModel;
        }
    
        public void itemStateChanged(ItemEvent e) {
    
             JComboBox comb = (JComboBox)e.getSource();
    
             if (table.getSelectedRow() > -1)
             {
                if (e.getStateChange() == ItemEvent.SELECTED)
                {
    
                    System.out.println(table.getSelectedRow());
                }
             }
        }
    aditipatil likes this.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    An ItemListener generates two events, one the the item that was unselected and one for the item that was selected.

    But I'm not sure what you are trying to do. You appear to add the ItemListener to the combo box, but are trying to display the selected row of the table. I would think you should be using a ListSelectionListener on the table.

    Read the section from the Swing tutorial on Implementing Listeners for Commonly Handled Events (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners). You will find examples for both types of listeners.

  3. #3
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default Comboboxes and JTables

    Hi camickr

    Thank you for replying!

    The value that the user selects from the comboboxes gets saved immediately in a database.

    The problem is that once all values were chosen (comboboxes populated in all rows) and the user selects a combobox; for e.g. if the third row is selected in the table and the user selects the combobox in the first row, the result that getSelectedRow outputs would be 2 instead of 0 (the numbers are row indexes).

    This means that changes to the third row in the table would be saved to the database, when you click the combobox in the first row. After selecting a value in the first row, changes would be made correctly to the first row, but the third row would then also be modified which is the problem.

    I am not sure if I am using the correct even handler for solving this problem, not sure if it makes sense to anyone, but that is what the compiler outputs.

    Maybe I should give this some more thought.

  4. #4
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Smile --solved--

    Hi everyone

    I have managed to solve the problem. It is mostly a workaround, but I think it should do for the time being. If I have some time on my hands, I will try and post the solution on the forum.

    Once again thank you camickr for the link on the event handling. I will try to get to that reading material. :)

  5. #5
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default Solution

    Hi Everyone

    Here is the solution to the problem.

    Instead of using an ItemListener, an ActionListener was implemented for the combobox instead.

    Inside the ActionListener event, I tested the condition of the combobox.

    In this case it was something like:

    combobox.isShowing()

    All the required code was implemented in the handler, and it really worked great!

    Thanks for those who tried to help me.

Similar Threads

  1. Jtables
    By amiral.smith in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2011, 03:22 PM
  2. Trying to fill multiple comboboxes by using one.
    By Stevemc in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 07:59 AM
  3. comboboxes
    By duncan913 in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 06:26 AM
  4. Replies: 3
    Last Post: 06-02-2010, 06:58 PM
  5. Scrolling with JTables
    By hiranya in forum AWT / Swing
    Replies: 5
    Last Post: 10-30-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •