0

I know and saw a few threads on this issue but none of them led me a solution so far. As a beginner, I pushed myself so hard to find a solution on my own but this is the maximum that I could've done.

I'd like to change lbl (in Class A), through btn (in Class B) and with this code, I am having a really long error message but it shows this line in Class B (a.setText("Changed!");).

Thanks.

Class A:

private JFrame frame;
private JPanel panel;
private JLabel lbl;


public a() {

    form();
    labels();

    frame.add(panel);
    frame.setVisible(true);
}

public void form(){

    frame = new JFrame("Frame");
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    panel.setLayout(null);

}


public void labels() {

    lbl = new JLabel("1");
    lbl.setBounds(30, 20, 10, 10);
    panel.add(lbl);


}

public void setText(String text) {

    lbl.setText(text);
    System.out.println("executed");

    panel.validate();
    panel.repaint();
}

public static void main(String[] args) {

    new a();
}

Class B:

private JFrame frame;
private JPanel panel;
private JButton btn;

a a;

public b() {

    form();
    buttons();

    frame.add(panel);
    frame.setVisible(true);
}

public void form(){

    frame = new JFrame("Frame");
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    panel.setLayout(null);
}


public void buttons() {

    btn = new JButton("Click me to change!");
    btn.setBounds(30, 20, 10, 10);
    btn.addActionListener(new ChangeHandler());
    panel.add(btn);
}

class ChangeHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {

        a.setText("Changed!");
    }
}

public static void main(String[] args) {

    new b();
}
aiox
  • 17
  • 6
  • 2
    1) `I am having a really long error message` - we are not mind readers. We don't know what the error is. 2) class names should start with an upper case character 3) class names should be descriptive. 4) Don't use a null layout. Swing was designed to be used with layout managers. 5) components should be added to the frame BEFORE the frame is made visible. 6) So many basic problems. Start by reading the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for basic examples of creating a Swing GUI – camickr Apr 07 '18 at 15:28
  • 1
    Also regarding 2 JFrames, please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Apr 07 '18 at 15:28
  • 1
    That "long error message" is a NullPointerException stacktrace -- you're trying to call a method on `a`, an uninitialized variable. This suggests that you will want to study more Java basics, including how to initialize variables, before using them. Please do check out the Java tutorials which you can find here: [Oracle Java Tutorial Big Index site](http://docs.oracle.com/javase/tutorial/reallybigindex.html). This gap in your understanding is why the similar questions on this subject aren't helping you yet -- you need to increase your basic Java knowledge first. – Hovercraft Full Of Eels Apr 07 '18 at 15:31
  • @camickr I created this classes just to post here, I am working on something completely different so I didn't mind about classes names and same goes with the layout but thank you for pointing out these, too. :) – aiox Apr 07 '18 at 15:33
  • I'll check everything you sent tho, I am sure that they'll help me a lot. Thank you! – aiox Apr 07 '18 at 15:35
  • 2
    @aiox, Yes but you are asking us to help you. We expect any code to be written properly. For one thing the forum highlights class names differently (making it easier to read the code) and they need to be proper names for this to happen. If you want help you make it easy for use to read the code. – camickr Apr 07 '18 at 15:35
  • @camickr I'll be more sensitive for the next time.:) – aiox Apr 07 '18 at 15:46
  • @HovercraftFullOfEels I have followed every step from 'Changing text on a JLabel from another class' and I am able to update my label right now but the problem is I need to type `a a = new a();` but with it, class a opens itself. Is there any way to disable that? – aiox Apr 09 '18 at 13:59
  • You need to pass in a reference to the already displayed A object (terrible name by the way). This means that you need to gain a basic understanding in what a Java object/reference is and what a reference variable is, and how to pass references to where needed. The OOPs section of the basic Java tutorials will help you with this. – Hovercraft Full Of Eels Apr 09 '18 at 14:37

0 Answers0