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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:07:05+00:00 2026-05-10T18:07:05+00:00

Full disclaimer: I’m a CS student, and this question is related to a recently

  • 0

Full disclaimer: I’m a CS student, and this question is related to a recently assigned Java program for Object-Oriented Programming. Although we’ve done some console stuff, this is the first time we’ve worked with a GUI and Swing or Awt. We were given some code that created a window with some text and a button that rotated through different colors for the text. We were then asked to modify the program to create radio buttons for the colors instead—this was also intended to give us practice researching an API. I’ve already handed in my assignment and received permission from my instructor to post my code here.

What’s the best way to implement button actions in Java? After some fiddling around, I created the buttons like this:

class HelloComponent3 extends JComponent     implements MouseMotionListener, ActionListener {     int messageX = 75, messageY= 175;      String theMessage;     String redString = 'red', blueString = 'blue', greenString = 'green';     String magentaString = 'magenta', blackString = 'black', resetString = 'reset';      JButton resetButton;     JRadioButton redButton, blueButton, greenButton, magentaButton, blackButton;     ButtonGroup colorButtons;      public HelloComponent3(String message) {      theMessage = message;      //intialize the reset button     resetButton = new JButton('Reset');     resetButton.setActionCommand(resetString);     resetButton.addActionListener(this);      //intialize our radio buttons with actions and labels     redButton = new JRadioButton('Red');     redButton.setActionCommand(redString);     ... 

And added action listeners…

redButton.addActionListener(this); blueButton.addActionListener(this); ... 

A stub was already created for the actionPerformed method to give us an idea on how to use it, but since there was only a single button in the template, it wasn’t clear how to implement multiple buttons. I tried switching on a String, but quickly realized that, since a String isn’t a primitive type, I couldn’t use it for a switch statement. I could have improvised with an if-else chain, but this is what I came up with instead. It seems far from elegant, and there must be a better way. If there is, what is it? Is there a way to switch on a string? Or choose an action in a more scaleable fashion?

public void actionPerformed(ActionEvent e){      if (e.getActionCommand().equals(resetString)) {         messageX = 75; messageY = 175;         setForeground(Color.black);         blackButton.setSelected(true);         repaint();         return;     }      if ( e.getActionCommand().equals(redString) ) {         setForeground(Color.red);         repaint();         return;     }      if ( e.getActionCommand().equals(blueString) ) {         setForeground(Color.blue);         repaint();         return;     }      if ( e.getActionCommand().equals(greenString) ) {         setForeground(Color.green);         repaint();         return;     }      if ( e.getActionCommand().equals(magentaString) ) {         setForeground(Color.magenta);         repaint();         return;     }      if ( e.getActionCommand().equals(blackString) ) {         setForeground(Color.black);         repaint();         return;     } } 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T18:07:05+00:00Added an answer on May 10, 2026 at 6:07 pm

    Instead of writing this:

    resetButton.addActionListener(this); 

    You could also write this:

    resetButton.addActionListener(new ActionListener() {     public void actionPerformed(ActionEvent evt) {         resetButtonActionPerformed(evt);     } }); 

    And instead of writing one big actionPerformed() for all actions, you can (and then have to) write this:

    public void resetButtonActionPerformed(ActionEvent evt) {     messageX = 75; messageY = 175;     setForeground(Color.black);     blackButton.setSelected(true);     repaint(); } 

    I don’t know if this is the most elegant solution, but at least you no longer have that big if construct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Full disclaimer: this is not really a homework, but I tagged it as such
The full question should be Is this correct or is it some bug I
Disclaimer This is not a question about whether we should be escaping for database
Disclaimer: I'm new to object-oriented PHP and I'm generally terrible at the language. So
Full disclaimer: java guy, trying on the .Net hat... I've gone through the walk
Full disclosure: This is my first time doing any significant programming in C, and
Full disclosure...this is my first SO question. Please be kind if I leave anything
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Full Question: From a child table linked to a row in its grandparent table
Full disclosure : This is for a homework assignment. This is driving me nuts.

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.