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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:15:36+00:00 2026-06-08T10:15:36+00:00

Possible Duplicate: Issues with ActionListener (Java) I am trying to implement action listener on

  • 0

Possible Duplicate:
Issues with ActionListener (Java)

I am trying to implement action listener on two buttons in JFrame, but the issue is one of the two button is performing both the functions; but i’ve not configured it to do so.

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

public class MyChangingCirlce implements ActionListener {
    JButton colorButton, labelButton;
    JLabel myLabel;
    MyDrawPanel mdp;
    JFrame frame;

    public static void main(String[] args) {
        MyChangingCirlce mcc = new MyChangingCirlce();
        mcc.createFrame();
    } // end of main

    public void createFrame() {
        frame = new JFrame();
         colorButton = new JButton("Changing Colors");
         labelButton = new JButton("Change Label");
        myLabel = new JLabel("I'm a label");
        mdp = new MyDrawPanel();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(BorderLayout.CENTER, mdp);
        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.EAST, labelButton);
        frame.getContentPane().add(BorderLayout.WEST, myLabel);
        colorButton.addActionListener(this);
        labelButton.addActionListener(this);
        frame.setSize(300, 300);
        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == colorButton) {
            frame.repaint();
        } else {
            myLabel.setText("That's it");
        }

    }

}

My labelButton is performing both the action only 1 time; i.e it changes the color of the circle along with the label text.

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

public class MyDrawPanel extends JPanel{

    public void paintComponent(Graphics g)
    {
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue= (int) (Math.random() * 255);
        Color randomColor = new Color(red,green,blue);
        g.setColor(randomColor);
        g.fillOval(20,70,100,100);
    }
}
  • 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-06-08T10:15:38+00:00Added an answer on June 8, 2026 at 10:15 am

    You override colorButton and labelButton. So the ‘else’ is always kicking in. Changing the label will cause a redraw.

    change

        JButton colorButton = new JButton("Changing Colors");
        JButton labelButton = new JButton("Change Label");
    

    to

        colorButton = new JButton("Changing Colors");
        labelButton = new JButton("Change Label");
    

    After writing myself a class to test it, I came about with this:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Date;
    
    public class Foo implements ActionListener {
    
        JButton colorButton, labelButton;
        JLabel myLabel;
        JFrame frame;
        MyDrawPanel mdp;
    
        public static void main(String[] args) {
            Foo mcc = new Foo();
            mcc.createFrame();
        } //end of main
    
        public void createFrame() {
            frame = new JFrame();
            colorButton = new JButton("Changing Colors");
            labelButton = new JButton("Change Label");
            myLabel = new JLabel("I'm a label");
            mdp = new MyDrawPanel();
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            mdp.setPreferredSize(new Dimension(150, 150));
            jsp.setLeftComponent(mdp);
            frame.setBounds(10, 10, 600, 600);
            JPanel right = new JPanel();
            right.add(BorderLayout.SOUTH, colorButton);
            right.add(BorderLayout.EAST, labelButton);
            right.add(BorderLayout.WEST, myLabel);
            jsp.setRightComponent(right);
            frame.getContentPane().add(jsp);
            colorButton.addActionListener(this);
            labelButton.addActionListener(this);
            frame.setVisible(true);
    
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == colorButton) {
                myLabel.setText("Color button's it");
                frame.repaint();
            } else {
                myLabel.setText("That's it" + new Date().toString());
            }
    
        }
    
        public class MyDrawPanel extends JPanel {
    
            @Override
            public void paintComponent(Graphics g) {
                int red = (int) (Math.random() * 255);
                int green = (int) (Math.random() * 255);
                int blue = (int) (Math.random() * 255);
                Color randomColor = new Color(red, green, blue);
                g.setColor(randomColor);
                g.fillOval(20, 70, 100, 100);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Browser Independence issue Code Working for IE but not for Firefox and
Possible Duplicate: Some issues trying to read a file with cbc.read.table function in R
Possible Duplicate: Memory Issues on iphone Hello Everybody, I was developed one small business
Possible Duplicate: Java pass by reference issue In my codes below, methodA will be
Possible Duplicate: Issue while saving image using savefiledialog I use windows forms in C#.
Possible Duplicate: Encoding issue: £ pound symbol appearing as <?> symbol I am doing
Possible Duplicate: Floating point issue in C #include<stdio.h> main() { int a,b; float f;
Possible Duplicate: multiple UIAlertView issue I`am beginner ios developer. And I faced with the
Possible Duplicate: Android Layout and positioning issue Design layout which support for all kind
Possible Duplicate: Ruby On Rails 3 and Webrick issue $ rails server /Users/Vineeth/.rvm/gems/ruby-1.9.3-p194/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require':

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.