Solved

setselectedindex in JComboBox not appearing in UI when set

Posted on 2006-06-27
15
475 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

631 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