Combbox action listener to show and hide an image
I've created a combobox with two options: Hide and Show inside a JPanel. I want to know how I can write an action listener, so when I select "Show" from the drop-down, a picture appears next to the combobox, and hides it when "Hide" is select. Many thanks in advance. Following is what I've got so far.
myPanel = new javax.swing.JPanel();
myLabel = new javax.swing.JLabel();
myComboBox = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
myLabel.setText("myLabel:");
myComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hide", "Show" }));
javax.swing.GroupLayout myPanelLayout = new javax.swing.GroupLayout(myPanel);
myPanel.setLayout(myPanelLayout);
myPanelLayout.setHorizontalGroup(
myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(myPanelLayout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(myLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(237, 237, 237))
);
myPanelLayout.setVerticalGroup(
myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(myPanelLayout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(myLabel)
.addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap开发者_StackOverflow社区(36, Short.MAX_VALUE))
);
Ok so i think i understand what you wanted to do so i threw some code together in my eclipse, i hope this helps you in some way!
package com.detter.john;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public final class ComboBoxExample extends JFrame implements ActionListener
{
public ComboBoxExample()
{
super("Combo Box Example");
setLayout(null);
panel = new JPanel();
box = new JComboBox(options);
box.addActionListener(this);
panel.add(box);
panel.setSize(100,100);
panel.setLocation(0,0);
panel.setVisible(true);
imagePanel = new ImagePanel();
add(panel);
add(imagePanel);
setSize(WIDTH,HEIGHT);
setLocation(X,Y);
setResizable(false);
setVisible(true);
}
public class ImagePanel extends JPanel
{
public ImagePanel()
{
try
{
showing = true;
image = ImageIO.read(new File("..\\Combobox action listener to show" +
" and hide an image\\Images\\SampleImage.PNG"));
}catch(Exception e){e.printStackTrace();}
setSize(WIDTH,HEIGHT);
setLocation(X,Y);
setVisible(true);
}
public void showImage()
{
showing = true;
repaint();
}
public void hideImage()
{
showing = false;
repaint();
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.clearRect(0,0,WIDTH,HEIGHT);
if(showing)
g2.drawImage(image,0,0,null);
}
private boolean showing;
private BufferedImage image;
private static final int WIDTH = 100;
private static final int HEIGHT = 100;
private static final int X = 100;
private static final int Y = 0;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==box)
{
switch(box.getSelectedIndex())
{
case SHOW:
imagePanel.showImage();
break;
case HIDE:
imagePanel.hideImage();
break;
}
}
}
private JPanel panel;
private ImagePanel imagePanel;
private JComboBox box;
private String options[] = {"show","hide"};
private static final int SHOW = 0;
private static final int HIDE = 1;
private static final int X = 0;
private static final int Y = 0;
private static final int WIDTH = 206;
private static final int HEIGHT = 132;
public static void main(String args[]){new ComboBoxExample();}
}
Read the JComboBox API and follow the link to the section in the Swing tutorial on 'How to Use Combo Boxes". It has an example of selecting from a list of pictures. I'm sure you can modify it to do what you want.
If you need more help then post your SSCCE that demonstrates the problem.
精彩评论