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

  • Home
  • SEARCH
  • 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 8763839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:52:41+00:00 2026-06-13T15:52:41+00:00

How do I make only the text belonging to the button the user presses

  • 0

How do I make only the text belonging to the button the user presses only shows. As of right now this code opens all of the windows showing the texts of the other buttons.I was thinking of a IF statement but I am not sure what do put in for a button clicked.If anyone knows how to generate a if-statement for a button clicked please share the information.

The first class

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class rohwcg extends JFrame
{

// adds the buttons
private JButton minerbutton;
private JButton farmerbutton;
private JButton lumberjackbutton;
    private JButton blacksmithbutton;

public rohwcg()
{
    super ("Realms of Havenwood Class Guide");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setBackground(Color.GREEN);

    //miner button
    Icon mbutton = new ImageIcon (getClass() .getResource("miner.png"));
    minerbutton = new JButton(mbutton);
    add(minerbutton);

    //farmer button
    Icon fbutton = new ImageIcon (getClass() .getResource("farmer.png"));
    farmerbutton = new JButton(fbutton);
    add(farmerbutton);

    //lumberjack button
    Icon lbutton = new ImageIcon (getClass() .getResource("lumberjack.png"));
    lumberjackbutton = new JButton(lbutton);
    add(lumberjackbutton);

    //blacksmith button
    Icon bbutton = new ImageIcon (getClass() .getResource("blacksmith.png"));
    blacksmithbutton = new JButton(bbutton);
    add(blacksmithbutton);

    //the action of the button.
    HandlerClass handler = new HandlerClass();
    minerbutton.addActionListener(handler);
    farmerbutton.addActionListener(handler);
    lumberjackbutton.addActionListener(handler);
    blacksmithbutton.addActionListener(handler);

    //sets the position of the button to center.
    blacksmithbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    minerbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    lumberjackbutton.setAlignmentX(Component.CENTER_ALIGNMENT);
    farmerbutton.setAlignmentX(Component.CENTER_ALIGNMENT);    
}

private class HandlerClass implements ActionListener
{
    //what happens when you click the button, below.
    public void actionPerformed(ActionEvent event)
    {           
        //farmer
        String farmertext = "null farmer";  
        JOptionPane.showMessageDialog(farmerbutton,farmertext,"The Farmer Class",JOptionPane.PLAIN_MESSAGE);

        //miner
        String minertext = "null miner";
        JOptionPane.showMessageDialog(minerbutton,minertext, "The Miner Class", JOptionPane.PLAIN_MESSAGE);

        //blacksmith
        String blacksmithtext ="null blacksmith";
        JOptionPane.showMessageDialog(blacksmithbutton,blacksmithtext, "The BlackSmith Class", JOptionPane.PLAIN_MESSAGE);

        //lumberjack
        String lumberjacktext = "null lumberjack";
        JOptionPane.showMessageDialog(lumberjackbutton, lumberjacktext, "The Lumberjack Class", JOptionPane.PLAIN_MESSAGE);         
    }

} 
}

The 2nd class

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class thehandler {
public static void main(String args []) 
{
    rohwcg classes1 = new rohwcg();
    classes1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    classes1.setSize(700,300);
    classes1.setVisible(true);

    //Sets the position of the window to a comman ratio. 
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    int x = d.width / 2;
    int y = (d.height / 2 ) - classes1.getHeight();
    classes1.setLocation(x,y);
}

}
  • 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-13T15:52:43+00:00Added an answer on June 13, 2026 at 3:52 pm

    Never-mind i found an answer! quite easy all you need to do is add an object get clicked

    private class HandlerClass implements ActionListener
    

    {

    public void actionPerformed(ActionEvent click)  {
    
        Object source = click.getSource();
    
        if(source == farmerbutton)
        {           
        String farmertext = "Blocks a Farmer cannot break:" +"\r\n" +"\r\n" + "Any type of logs" + "\r\n" + "Stone" + "\r\n" + " Coal ore" + "\r\n" + "Iron ore" + "\r\n" + "Gold ore" + "\r\n" + "Diamond Ore" + "\r\n" + "Redstone ore" + "\r\n" + "Lapiz ore";  
        JOptionPane.showMessageDialog(farmerbutton,farmertext,"The Farmer Class",JOptionPane.PLAIN_MESSAGE);
        }
    
    
        if(source == minerbutton)
        {
        String minertext = "null miner";
        JOptionPane.showMessageDialog(minerbutton,minertext, "The Miner Class", JOptionPane.PLAIN_MESSAGE);
        }
    
        if(source == blacksmithbutton)
        {
        //blacksmith
        String blacksmithtext ="null blacksmith";
        JOptionPane.showMessageDialog(blacksmithbutton,blacksmithtext, "The BlackSmith Class", JOptionPane.PLAIN_MESSAGE);
        }
    
        if(source == lumberjackbutton)
        {
        //lumberjack
        String lumberjacktext = "null lumberjack";
        JOptionPane.showMessageDialog(lumberjackbutton, lumberjacktext, "The Lumberjack Class", JOptionPane.PLAIN_MESSAGE); 
        }
    }
    

    }

    }

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

Sidebar

Related Questions

I'm using gtk.combo_box_new_text() to make combobox list, this uses a gtk.ListStore to store only
[This will only make sense if you've seen Kevin Smith's 'Erlang in Practice' screencasts]
I am trying to make this landscape only iphone app. I only use this
I want to make a simple application having only one text field and a
I want to make an input with some read-only text and some writable text.
Is there a way in MVC3 to make a read-only Text Box where the
I have a read-only text-box with text in it. How to make it editable
how do i make only 1 object creation of any class?
Is there any way to make only a non-transparent portion of an image to
So I have a subclassed UITableView that lists data. I need to make only

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.