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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:57:51+00:00 2026-06-14T16:57:51+00:00

I have a label and a button in a class called FrameTest , when

  • 0

I have a label and a button in a class called FrameTest, when i press the button, a method named buttonpressed get’s executed from the class Test. In this buttonpressed method i will set a text to the label found in the FrameTest class.

The problem i have is that, the text for the label is not getting set. The reason is that i am creating a separate object to call the buttonpressed method;

public void actionPerformed(ActionEvent arg0) {
                    Test t = new Test();
                    t.buttonpress();
                }

and i am creating a separate object in the main method of the Test class to create the UI.

public static void main(String[] args) {

         FrameTest f = new FrameTest();
         f.mainScreen();

    }

The full code as follows;

public class FrameTest extends JFrame {

private JPanel contentPane;
private JLabel lblLabel;
private FrameTest ft = this;

//private FrameTest frame;
/**
 * Launch the application.
 */
public  void mainScreen() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //FrameTest frame = new FrameTest();
                //setVisible(true);

                FrameTest frame = ft;
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public void writeLabel(String k){
    this.lblLabel.setText(k);

}

public FrameTest() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH); 

    //setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    lblLabel = new JLabel("LABEL");
    contentPane.add(lblLabel, BorderLayout.CENTER);

    JButton btnNewButton = new JButton("Press");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Test t = new Test();
            t.buttonpress();
        }
    });
    contentPane.add(btnNewButton, BorderLayout.WEST);
    //pack();
    setLocationByPlatform(true);
}

}

Test Class

public class Test {


public static void main(String[] args) {

     FrameTest f = new FrameTest();
     f.mainScreen();

}

public void buttonpress(){
     FrameTest f = new FrameTest();

     f.writeLabel("Button was pressed");

}
  • 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-14T16:57:53+00:00Added an answer on June 14, 2026 at 4:57 pm

    1) Dont extend JFrame class unnecessarily.

    2) dont use setContentPane() unless thats what you want. Rather just simply JFrame#add(..).

    3) Steer away from EventQueue and use SwingUtilities block rather

    4) Dont forget to call JFrame#pack(); before setting JFrame visible.

    5) Java naming convention is CamelCase so buttonPress() is correct not buttonpress()

    Here is an example I made (basically your code fixed):

    Test.java: (This is the main class which will create an instance of your FrameTest and has the method to change JLabel text)

    import javax.swing.SwingUtilities;
    
    public class Test {
    
        private static FrameTest f;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    f = new FrameTest();
                    f.mainScreen();
                }
            });
        }
    
        void buttonPress() {
            f.writeLabel("Hello");
        }
    }
    

    FrameTest.java: (This class will show the JFrame and create a new instance of class Test to call buttonPress()):

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    public class FrameTest {
    
        private JPanel panel;
        private JLabel lblLabel;
        private JFrame frame;
    
        private void initComponents() {
            frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    
            panel = new JPanel();
            panel.setBorder(new EmptyBorder(5, 5, 5, 5));
            panel.setLayout(new BorderLayout(0, 0));
    
            lblLabel = new JLabel("LABEL");
            panel.add(lblLabel, BorderLayout.CENTER);
    
            JButton btnNewButton = new JButton("Press");
            btnNewButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test t = new Test();
                    t.buttonPress();
                }
            });
    
            panel.add(btnNewButton, BorderLayout.WEST);
    
            frame.add(panel);
    
            frame.setLocationByPlatform(true);
            frame.pack();
            frame.setVisible(true);
        }
    
        public void writeLabel(String k) {
            this.lblLabel.setText(k);
        }
    
        void mainScreen() {
            initComponents();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a label and button inside update panel, when i try to get
I have three button named(titled) hello, nothing, heaven and one label (IBOutlet UIlabel lab).
I have a Windows Form application with a button named loginBtn and a label
This is my problem, I have one textbox, one button and one label. Everything
Using hover class of css. I have a label called show & i have
I have created a user control (link) which has a label and a button.
I have a button, the label of which I want to change back and
I have a simple metro app contains a button, a label and a drop
I have a user control that contains multiple controls (CheckBox, Button, Label...). I want
When I have many controls on a Form (i.e. Label, Button etc) that do

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.