Solved

setselectedindex in JComboBox not appearing in UI when set

Posted on 2006-06-27
15
453 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
Updating Java 9 87
Strange loading of website behaviour 3 23
github account with ecipse 1 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now