• Post Reply Bookmark Topic Watch Topic
  • New Topic

About JComboBox  RSS feed

 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I have been learning java for 3 months.

Please help me with my code. i created a JFrame included Jcombobox and JLabel. i wanted to display the image on the jlabel area when the combobox is selected but i could not add the images to the jlabel as well as i could not link the combobox to jlabel.
i have attached the image too.

here is the code:

combo.png
[Thumbnail for combo.png]
image of the intended code
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on How to Use Combo Boxes for a working example that does this.

Also, when you post code in the forum don't forget to use the "code" tags so the code retains its formatting and is more readable.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added the code tags just this once so you can see how much better the post looks.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i looked at the website you mentioned How to Use Combo Boxes.

And also i tried with eclipse but could not linked the combobox to Jlabel.

also the codes are completely different. there are not createAndShow stuff on eclipse.

i am really confused.

please help me guys.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well since you are displaying an image, you should not be using a JLabel. But JLabel is fine for testing

Second what "event" is needed to trigger the image to change? Add this event listener to the JComboBox.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laddie Larry wrote:And also i tried with eclipse but could not linked the combobox to Jlabel.

also the codes are completely different. there are not createAndShow stuff on eclipse.


Huh? Does Eclipse somehow prevent you from writing methods of your choice?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Well since you are displaying an image, you should not be using a JLabel.


Why not? What would you recommend instead?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laddie Larry wrote:

Laddie, you really need to learn about Laying Out Components Within a Container.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
K. Tsang wrote:Well since you are displaying an image, you should not be using a JLabel.


Why not? What would you recommend instead?


You will be working with an image (java.awt.Image). Actually java.awt.image.BufferedImage
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you be working with a BufferedImage? I would also just use a JLabel containing an ImageIcon to display the image.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first I thought the same thing ImageIcon next to JLabel. But then from what the OP screen shot, it's more like those web/js dropdown.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right guys. I really need to learn a lot about java stuff.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I gave you the link to the Swing tutorial that does exactly what you want.

Read the tutorial, download the example and modify the example for your exact requirement.

You should be spending time learning Java not learning how to use an IDE. If you ever switch IDE's then your code will not be portable. An IDE is a tool to help your edit and compile. You should not rely on the IDE to generate your code.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
can anyne send me a simple code of the program that i want?
i mean about JComboBox and displaying image on the JLabel like the attachment i sent 2 weeks ago.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you understand the Tutorials link which RC sent you? Which part are you having problems with? Did you find the link from the tutorial to the combo box code? It seems to do exactly what you are asking for.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did but i was not able to dispaly the image.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Didn't you understand the Tutorials link which RC sent you? Which part are you having problems with? Did you find the link from the tutorial to the combo box code? It seems to do exactly what you are asking for.


I did friend but i was not able to dispaly the image.
here is the code:

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



public class petshow extends JPanel
implements ActionListener {
JLabel petpicture;

public petshow() {
super(new BorderLayout());

String[] pets = { "Cat", "Dog", "cow" };

JComboBox petList = new JComboBox(pets);
petList.setSelectedIndex(4);
petList.addActionListener(this);

petpicture = new JLabel();
petpicture.setFont(petpicture.getFont().deriveFont(Font.ITALIC));
petpicture.setHorizontalAlignment(JLabel.CENTER);
updateLabel(pets[petList.getSelectedIndex()]);
petpicture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

petpicture.setPreferredSize(new Dimension(177, 122+10));

add(petList, BorderLayout.PAGE_START);
add(petpicture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}


public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
updateLabel(petName);
}

protected void updateLabel(String names) {
ImageIcon icon = createImageIcon( names + ".png");
petpicture.setIcon(icon);
petpicture.setToolTipText("A drawing of a " + names.toLowerCase());
if (icon != null) {
petpicture.setText(null);
} else {
petpicture.setText("Image not found");
}
}

protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = petshow.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}


private static void createAndShowGUI() {

JFrame frame = new JFrame("Mypets");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new petshow();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have downloaded the example from the tutorial and it works fine.

What error messages to you get? If the tutorial example can't find the image you will get an error message. If the image is found, then the code will work.

Start with the tutorial example, not your own code.

Use the "code tags" when you post code so the code retains its formatting.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Well I have downloaded the example from the tutorial and it works fine.

What error messages to you get? If the tutorial example can't find the image you will get an error message. If the image is found, then the code will work.

Start with the tutorial example, not your own code.

Use the "code tags" when you post code so the code retains its formatting.


Hi friends:

I have downloaded the code and run it on command line as well as eclipse but it has been giving me the error messages like

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is part of a stack trace; you need to post more of the stack trace (post all of it) before we know what has gone wrong. We have a helpful page about how to copy that sort of thing.
 
Latif Hemid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow finally i did it.

Thans for youe guys help and support you are really helpfull.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how did you sort it out? What was the Exception you had there? We would like to know, and so would anybody else who has the same problem in future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!