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 6339333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:37:46+00:00 2026-05-24T19:37:46+00:00

I do not know where to add ActionListeners/ItemListener and need help: This is the

  • 0

I do not know where to add ActionListeners/ItemListener and need help:
This is the desired output:

  • If you selected a shape in checkboxes, it will draw a type of shape you selected
  • and if you selected radiobutton, it will fill color(maybe a blue color)

here’s the code:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;

public class ARadioCombo {

   public static void main(String args[]) {

      JFrame frame = new JFrame("Radio/Combo Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel(new GridLayout(0, 1));
      Border border = BorderFactory.createTitledBorder("Fill/Unfill");

      panel.setBorder(border);
      ButtonGroup group = new ButtonGroup();
      JRadioButton aRadioButton = new JRadioButton("Fill Color");
      panel.add(aRadioButton);
      group.add(aRadioButton);

      aRadioButton = new JRadioButton("Remove Fill");
      panel.add(aRadioButton);
      group.add(aRadioButton);

      Container contentPane = frame.getContentPane();
      contentPane.add(panel, BorderLayout.WEST);
      panel = new JPanel(new GridLayout(0, 1));

      border = BorderFactory.createTitledBorder("Select Shape");
      panel.setBorder(border);

      JCheckBox aCheckBox = new JCheckBox("Oval");
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Square", true);
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Rectangle");
      panel.add(aCheckBox);

      aCheckBox = new JCheckBox("Circle");
      panel.add(aCheckBox);

      contentPane.add(panel, BorderLayout.EAST);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
}
  • 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-24T19:37:47+00:00Added an answer on May 24, 2026 at 7:37 pm

    You should add your ActionListeners to any buttons that the user interacts with, here your JRadioButtons. So where you have this:

      JRadioButton aRadioButton = new JRadioButton("Fill Color");
      panel.add(aRadioButton);
      group.add(aRadioButton);
    
      aRadioButton = new JRadioButton("Remove Fill");
      panel.add(aRadioButton);
      group.add(aRadioButton);
    

    You could have something like this:

      ActionListener myActionListener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            // TODO: put in code I want to have happen on button selection 
            // One ActionListener can likely be used for all buttons in this 
            // small program.
            // as noted below, it could be as simple as one line saying:
            // repaint();
         }
      };
      JRadioButton aRadioButton = new JRadioButton("Fill Color");
      panel.add(aRadioButton);
      group.add(aRadioButton);
      aRadioButton.addActionListener(myActionListener);
    
      aRadioButton = new JRadioButton("Remove Fill");
      panel.add(aRadioButton);
      group.add(aRadioButton);
      aRadioButton.addActionListener(myActionListener); // add to each radiobutton object
    

    Also, shouldn’t your JCheckBoxes also be JRadioButtons that with the help of a second ButtonGroup object only allow one selection at a time?

    Also:

    • Get most all of that code out of the main method and into a Java class proper.
    • If you are going to draw on a JPanel, you’ll need to subclass JPanel and override its paintComponent method. Inside of that method you’ll have if blocks that will change what is drawn depending on the state of your JRadioButtons. If this were my project, I’d have my main class extend JPanel and then draw in its paintComponent method. Then in my main method, I’d create a JFrame, and add an instance of this class into the JFrame’s contentPanel
    • One solution is to simply call repaint() on the drawing JPanel in the ActionListener actionPerformed, and have the paintComponent method poll the JRadioButtons for their states and in if blocks decide what to draw.
    • You’ll want to leave all mention of deadlines and urgencies out of your posts here as that is your problem not ours, and doesn’t help us help you to a solution. I have taken the liberty to remove this from your original post.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Close mysql connection (PHP) Please help me, I not know where add
I do not know how to ask this question. So I will just give
I know UIView are not thread safe so i cant add a view on
I do not know if this is possible, but here goes. And working with
I do not know what is wrong with this code. alert shows that data
I have downloaded objectListView but do not know how to add the component to
i need your help badly because i cannot solve this problem on my own.
I do not know why my google markers are not showing. I am using
EPD python does not know to look for the Ubuntu repository python so installing
I do not know what I am doing when it comes to JSON. So

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.