Solved

setselectedindex in JComboBox not appearing in UI when set

Posted on 2006-06-27
15
463 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
print map entry 34 68
tomcat not starting 6 45
jdbc error in jsp application 20 45
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.
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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