Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:48+00:00 2026-05-23T15:08:48+00:00

I’ve created a combobox with two options: Hide and Show inside a JPanel. I

  • 0

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(36, Short.MAX_VALUE))
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:08:49+00:00Added an answer on May 23, 2026 at 3:08 pm

    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();}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.