Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

Can I put JLabel array in JComboBox?

Thread view: 
niceguy16 - 27 Apr 2006 04:32 GMT
Here is the code.  It runs but does not display the labels.  Why?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Combo implements ActionListener{
    JComboBox comboBox;
    JLabel i= new JLabel();
    JLabel[] items = new JLabel[4];
 public static void main(String[] args) {
   JFrame f = new JFrame("Interest Rate");
   Combo c=new Combo();
   f.setSize(300, 200);
   f.setLocation(200, 200);
   Container pane = f.getContentPane();
   c.items[0] = new JLabel("5.25");
    c.items[1] = new JLabel("5.5");
    c.items[2] = new JLabel("5.75");
   c.items[3] = new JLabel("6.0");
   c.create();
   c.comboBox.setEditable(true);
    BorderLayout brd = new BorderLayout();
    pane.setLayout(brd);
    pane.add(c.comboBox,BorderLayout.NORTH);
    pane.add(c.items[0],BorderLayout.CENTER);
    pane.add(c.i, BorderLayout.SOUTH);
   f.setVisible(true);
     }

public void actionPerformed(ActionEvent ae) {
    Object  selection = comboBox.getSelectedItem();
    i.setText("User has selected interest rate " + selection + "%");
   }

private void create(){
   comboBox = new JComboBox(items);
   comboBox.addActionListener(this);
   }}
Bart Cremers - 27 Apr 2006 06:44 GMT
Of course you can, although I don't see the use for it in your example.
The default renderers and editors for a JComboBox are designed to work
with Strings and primite wrappers, so in your example it would be
better to add an array of Double objects to the combobox:

Double[] items = new Double[4];

...

c.intems[0] = new Double(5.25);
...
pane.add(new JLabel(c.items[0].toString()), BorderLayout.CENTER);
...

This should render perfectly.

If you want for some obscure want to add an array of JLabels you'll
have to override the renderer (c.comboBox.setRenderer(subclass of
DefaultListCellRenderer). More work is involved even if you want the
combobox also to be editable. This requires you to set the editor on
the combobox (c.combBox.setEditor(implementation of ComboBoxEditor). As
you can see a lot of work while you probable gain nothing from it.

Bart
Vova Reznik - 27 Apr 2006 14:48 GMT
> Here is the code.  It runs but does not display the labels.  Why?
>
[quoted text clipped - 35 lines]
>     comboBox.addActionListener(this);
>     }}

Because combo box renderer uses toString() to show.
You may write your own version of JLabel where
toString() will return text:

public String toString() {
  return getText();
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.