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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:37:05+00:00 2026-05-30T04:37:05+00:00

I am trying to get a JLabel to appear when a JButton is clicked.

  • 0

I am trying to get a JLabel to appear when a JButton is clicked. I have added an action listener and added the component to the layout. I am using the label1.setVisible(true) when the JButton is clicked in actionPerformed. I still can’t get it work. Can some look at my code?

public class LearnAppMain extends JFrame implements ActionListener {

// Define variables
public JButton button1;
public JLabel label1;
    public JTextField field1;

    private Image image1;
private String apple = "apple.jpg";

public LearnAppMain() {

    ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
    JLabel label1 = new JLabel(image1);

    button1 = new JButton("A");
    button1.addActionListener(this);

    field1 = new JTextField(10);

    // Create layout
    setLayout(new FlowLayout());

    // create Container
    final Container cn = getContentPane();

    cn.add(button1);
    cn.add(field1);
    cn.add(label1);

    // setLayout(new FlowLayout());
    setSize(250, 250);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();

    if (e.getSource() == button1) {
        label1.setVisible(true);
        field1.setText("Apple");
    }

}

 }

I have my main method in another class file. The error I get leads me to the label1.setVisible(true);

Every question I’ve seen they say to do this, but I’m wondering if there is something else that needs to be added.

  • 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-30T04:37:07+00:00Added an answer on May 30, 2026 at 4:37 am

    There were a couple of issues here:

    • Your label1 was hidden by doing JLabel label in the constructor. You basically declared another variable called label1 in your constructor that hid the one in the class itself.
    • Your label was visible on the startup – I used label.setVisible(false) for the test, but you might want otherwise

    I also put the creation of Image aside as I did not have an image, so uncomment that and change as appropriate.

    Here’s a complete working version:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LearnAppMain extends JFrame implements ActionListener {
    
    // Define variables
    public JButton button1;
    public JLabel label1;
        public JTextField field1;
    
        private Image image1;
    private String apple = "apple.jpg";
    
    public LearnAppMain() {
    
        //ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
        //JLabel label1 = new JLabel(image1);
        label1 = new JLabel("hello");
        label1.setVisible(false);
    
        button1 = new JButton("A");
        button1.addActionListener(this);
    
        field1 = new JTextField(10);
    
        // Create layout
        setLayout(new FlowLayout());
    
        // create Container
        final Container cn = getContentPane();
    
        cn.add(button1);
        cn.add(field1);
        cn.add(label1);
    
        // setLayout(new FlowLayout());
        setSize(250, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
    
        Object source = e.getSource();
    
        if (e.getSource() == button1) {
            label1.setVisible(true);
            field1.setText("Apple");
        }
    
    }
    public static void main(String[] args) {
      new LearnAppMain();
    }
    }
    

    I’d suggest using separate (usually inner-class) ActionListener instances instead of overriding actionPerformed. See e.g. this for a similar example if you are interested:

    • http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/events/BeeperProject/src/events/Beeper.java

    Also, if you are using this in a bigger application (i.e. not just experimenting or for prototyping), make sure all Swing code is run on EDT.

    You typically use SwingUtilities.invokeLater for that purpose.

    Hope this helps.

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

Sidebar

Related Questions

I'm trying to get a JLabel to display new line characters by using HTML
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get comfortable with jQuery and I have encountered some sample code that
I'm completely new to Linux and have been trying to get my (Windows built)
I am trying get the values using SP.The query is below. create proc [dbo].[GetOrdersByUserID11]
I am trying to get the the bilde() methode add images too my JLabel
I am using JDateChooser in JCalendar (with Swing). I am trying to get a
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then

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.