Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

setselectedindex in JComboBox not appearing in UI when set

Posted on 2006-06-27
15
Medium Priority
?
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

715 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