Solved

setselectedindex in JComboBox not appearing in UI when set

Posted on 2006-06-27
15
467 Views
Last Modified: 2008-02-01
Here is my code:

      private void initGUI() {
            try {
                  setPreferredSize(new Dimension(400, 300));
                  this.setLayout(null);
                  {
                        String[] ports = SerialUtils.detectCommPorts();
                        Arrays.sort(ports);
                        
                        jScrollPane1 = new JScrollPane();
                        this.add(jScrollPane1);
                        jScrollPane1.setBounds(7, 7, 294, 231);
                        {
                              DefaultTableModel jTable1Model = new DefaultTableModel();
                              jTable1Model.addColumn("Channel");
                              jTable1Model.addColumn("Serial Port");
                              jTable1 = new JTable();
                              jScrollPane1.setViewportView(jTable1);
                              jTable1.setModel(jTable1Model);
                              
                          JComboBox comboBox = new JComboBox();
                          comboBox.addItem("N/A");
                          for (int i = 0; i < ports.length; i++) {
                                jTable1Model.addRow(new String[]{"Channel " + (char)Integer.parseInt("00" + (41 + i), 16)});
                                comboBox.addItem(ports[i]);
                          }
                          jTable1.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
                          comboBox.setSelectedIndex(0);
                          comboBox.requestFocus();
                          System.out.println(comboBox.hasFocus());
                        }
                  }
                  {
                        jButton1 = new JButton();
                        BorderLayout jButton1Layout = new BorderLayout();
                        jButton1.setLayout(jButton1Layout);
                        this.add(jButton1);
                        jButton1.setText("Submit");
                        jButton1.setBounds(308, 119, 84, 21);
                        jButton1.addMouseListener(new MouseAdapter() {
                              public void mouseClicked(MouseEvent evt) {
                                    submit();
                              }
                        });
                  }
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
0
Comment
Question by:abcast
  • 7
  • 5
  • 3
15 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 16995922
do you mean comboBox.setSelectedIndex(0) does not select item 0 ? what happens ?
0
 

Author Comment

by:abcast
ID: 16996461
It seems to select the index, but the text associated with that index does not appear in the swing UI.
0
 
LVL 92

Expert Comment

by:objects
ID: 16996822
What does it display?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:abcast
ID: 16996830
nothing
0
 
LVL 92

Expert Comment

by:objects
ID: 16996883
you never add the combo to your gui
if you have a member var for the combo that u *do* add then you should be adding the itenms to that var

i. this should not be a local var:

                       JComboBox comboBox = new JComboBox();

and instead initialising the member var

                       comboBox = new JComboBox();
0
 

Author Comment

by:abcast
ID: 16996909
>>you never add the combo to your gui

I'm able to select the items in the box from my gui, though.  I'm not sure I understand.
0
 
LVL 92

Expert Comment

by:objects
ID: 16996931
looks like the combo you are adding to your gui is not the same one as you add items in the code above.
See my previous comment.
0
 

Author Comment

by:abcast
ID: 17002272
>>looks like the combo you are adding to your gui is not the same one as you add items in the code above.
See my previous comment.

This can't be.  If the combo box in my GUI was not the same combo box that I add items to, then I would not be able to see the items in my GUI, which I can.  Right?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17002319
please post all the code if possible
0
 

Author Comment

by:abcast
ID: 17002337
import java.awt.Dimension;
import java.util.Arrays;

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

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ChannelMapTab extends javax.swing.JPanel {

      {
            //Set Look & Feel
            try {
                  javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());
            } catch(Exception e) {
                  e.printStackTrace();
            }
      }

      private JScrollPane jScrollPane1;
      private JButton jButton1;
      private JTable jTable1;
      private JComboBox comboBox;

      /**
      * Auto-generated main method to display this
      * JPanel inside a new JFrame.
      */
      public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.getContentPane().add(new ChannelMapTab());
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
      }
      
      public ChannelMapTab() {
            super();
            initGUI();
      }
      
      private void initGUI() {
            try {
                  setPreferredSize(new Dimension(400, 300));
                  this.setLayout(null);
                  {
                        String[] ports = SerialUtils.detectCommPorts();
                        Arrays.sort(ports);
                        
                        jScrollPane1 = new JScrollPane();
                        this.add(jScrollPane1);
                        jScrollPane1.setBounds(7, 7, 294, 231);
                        {
                              DefaultTableModel jTable1Model = new DefaultTableModel();
                              jTable1Model.addColumn("Channel");
                              jTable1Model.addColumn("Serial Port");
                              jTable1 = new JTable();
                              jScrollPane1.setViewportView(jTable1);
                              jTable1.setModel(jTable1Model);
                              
                          comboBox = new JComboBox();
                          comboBox.addItem("N/A");
                          for (int i = 0; i < ports.length; i++) {
                                jTable1Model.addRow(new Character[]{new Character((char)Integer.parseInt("00" + (41 + i), 16))});
                                comboBox.addItem(ports[i]);
                          }
                          jTable1.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
                        }
                  }
                  {
                        jButton1 = new JButton();
                        BorderLayout jButton1Layout = new BorderLayout();
                        jButton1.setLayout(jButton1Layout);
                        this.add(jButton1);
                        jButton1.setText("Submit");
                        jButton1.setBounds(308, 119, 84, 21);
                        jButton1.addMouseListener(new MouseAdapter() {
                              public void mouseClicked(MouseEvent evt) {
                                    submit();
                              }
                        });
                        
                   
                  }
                  
                  comboBox.setSelectedIndex(0);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      
      private void submit() {
            int rows = jTable1.getRowCount();
            
            for (int i = 0; i < rows; i++) {
                  if (!jTable1.getValueAt(i, 1).equals("N/A"))
                        ChannelMap.getInstance().addMapping((Character)jTable1.getValueAt(i,0), (String)jTable1.getValueAt(i,1));
            }
            
            System.out.println(ChannelMap.getInstance().toString());
      }
      
      public JScrollPane getJScrollPane1() {
            return jScrollPane1;
      }
      
      public JTable getJTable1() {
            return jTable1;
      }

}
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17002734
change
jTable1Model.addRow(new String[]{"Channel " + (char)Integer.parseInt("00" + (41 + i), 16)});
to
jTable1Model.addRow(new Object[]{(char)('A' + i), "N/A"});
0
 

Author Comment

by:abcast
ID: 17002822
I assume you meant
jTable1Model.addRow(new Object[]{new Character((char)('A' + i)), "N/A"});


Well, the value of index 0 now appears in the combo boxes.  However, when I try to set the selected index to another index value, the GUI does not reflect this.  So, for example, if i change setSelectedIndex(0) to 1, the value of 0 remains in the GUI.
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 17002990
you should use table.setValueAt(value, row, col); for each row you want the update be reflected
0
 

Author Comment

by:abcast
ID: 17003020
Ok.  Why won't setselectedindex work in this case?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17003046
its only an editor, not a way to set cell values (99% sure)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 38
String array comparison 4 45
Better way to make a string with template variables in java 3 33
spring maven example issues 3 40
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question