#JLabel only appearing on second monitor

1 messages · Page 1 of 1 (latest)

pure inletBOT
#

<@&987246487241105418> please have a look, thanks.

pure inletBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

acoustic dew
#

It may have to do with using action listener on jtextfield. Try using a button instead to do the task, or another listener like a change listener like seen in this in the 3rd answer on the following link https://stackoverflow.com/questions/11257506/error-on-adding-changelistener-on-jtextfield

hearty crag
#

Okay, but why does it only work when I move it to another monitor? And it works when I bring it back to the first monitor too

#

I appreciate the help

acoustic dew
#

@hearty crag it may have to do with using action listener with jtextfield and having to lose focus on the field before it can do the action. I've had problems before about focus getting stuck on components and not updating things.

hearty crag
#

so using changeListener is the best solution for it? @acoustic dew

acoustic dew
#

It'll be a better solution in my eyes, and should solve your problem.

hearty crag
#

Alright, I'll keep you updated

hearty crag
#

@acoustic dew I'm completely lost on how to use the changelistener, I'm sort of new to Java Swing, how would I go about it?

#
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.DirectoryNotEmptyException;

public class MyGame implements ActionListener {
    private JFrame frame;
    private JPanel panel;
    private JLabel label;
    private JLabel label2;
    private JTextField textField;

    public MyGame() {

        frame = new JFrame();
        panel = new JPanel();
        textField = new JTextField();
        label = new JLabel();
        label2 = new JLabel();

        label.setFont(new Font("Times New Roman",Font.PLAIN,16));
        frame.setMinimumSize(new Dimension(500,500));
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("My Text-Based Adventure Game");
        frame.setVisible(true);
        panel.setBackground(Color.pink);
        panel.isOpaque();

        label.setText("Please enter your name, adventurer: ");

        textField.setColumns(30);
        panel.add(label);
        panel.add(textField);
        textField.addActionListener(this);

        frame.pack();

    }
    public static void main(String[] args) {
        new MyGame();
    }
    public void actionPerformed(ActionEvent e) {
        String name = textField.getText();
        System.out.println(name);
        label2.setText("Welcome, "+name+"! Good luck on your journey.");
        panel.add(label2);



    }

}
#

There's my source code so far

hearty crag
#

Also, I tried the same thing with a button but got the same error.

acoustic dew
# hearty crag Also, I tried the same thing with a button but got the same error.

Alright, since I made it to my computer, I think I found a better solution than the ones mentioned above. Using a keylistener paired up with the enter key, the following executes your code when pressing enter. Thankfully, keyPressed events are pretty easy to work with and are readable.

This is the modified constructor:

public MyGame() {

        frame = new JFrame();
        panel = new JPanel();
        textField = new JTextField();
        label = new JLabel();
        label2 = new JLabel();

        label.setFont(new Font("Times New Roman",Font.PLAIN,16));
        frame.setMinimumSize(new Dimension(500,500));
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("My Text-Based Adventure Game");
        frame.setVisible(true);
        panel.setBackground(Color.pink);
        panel.isOpaque();

        label.setText("Please enter your name, adventurer: ");

        textField.setColumns(30);
        panel.add(label);
        panel.add(textField);
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar() == KeyEvent.VK_ENTER) {
                    System.out.println(textField.getText());
                    label2.setText("Welcome, " + textField.getText() + "! Good luck on your journey.");
                }
            }
        });
        panel.add(label2);
        frame.pack();

    }
pure inletBOT
hearty crag
#

I had researched keylistener but I never understood it completely, thank you for helping!

#

Would I need a whole nother method for this to work like with actionlistener?

acoustic dew
#

You can use it in the anonymous class style I used above instead of implementing it or you can implement it like you did with the actionevent and attach an action listener to the text field.

hearty crag
#

Okay

acoustic dew
#

Implementing it would require many of the methods to be added to the code, or if you use anonymous, you can chose which you want to add. Since you'll only need one right now, its probably best to use the anonymous version.

hearty crag
#

Alright

#

@acoustic dew Same error with the screens, it only shows up after I move it to another monitor

#

It goes both ways so it isnt a monitor issue

acoustic dew
#

Strange. It works just fine on my screen. It may be a bug with the IDE. I had a bug with IDEA where resizing any app, even the most minimal ones would crash the app. But another IDE would work fine, or on a different computer with the same IDE. Can you try it in a different IDE?

hearty crag
#

I'll go for it with netbeans sure

#

@acoustic dew Same thing happens in VS code

#

Wouldn't work with netbeans for some reason

#

So its something on my end, its not the IDE, its not the monitor

#

Let me try disconnecting my second monitor and running it

#

Nothing

acoustic dew
#

Its possible it could be a display driver issue. Is your driver up to date?

hearty crag
#

I believe so; how would I check on macOS?

#

I'm on macOS Sonoma beta, that could be the issue

acoustic dew
#

It looks like you can do so by checking if your mac has any software updates.

hearty crag
#

It doesn't, but Sonoma is a very early beta so it very well could be something with that

#

Especially since it works on your end, which is im assuming Windows

acoustic dew
#

Yeah. There isn't any issues here. There was some issues using the action listener on the text field that you had previously not executing in the intended manner.

hearty crag
#

Someone in another disc said it was a "repaint issue"? And refused to elaborate haha

#

Hmm

#

I'll just deal with it until I can revert back to macOS Ventura and see what happens then

acoustic dew
#

I suppose a solution could be to refresh the components being displayed on the screen on your end, which can be done by the following:

frame.getContentPane().removeAll();        frame.getContentPane().add(panel);
frame.revalidate();
frame.repaint();

Does this do anything?

pure inletBOT
acoustic dew
#

add that to the key listener keypressed method and see

#

That should essentially clear the screen and readd the items to the screen

hearty crag
#

Where would I add that in exactly?

acoustic dew
#
textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar() == KeyEvent.VK_ENTER) {
                    System.out.println(textField.getText());
                    label2.setText("Welcome, " + textField.getText() + "! Good luck on your journey.");
                    frame.getContentPane().removeAll();
                    frame.getContentPane().add(panel);
                    frame.revalidate();
                    frame.repaint();
                }
            }
        });
pure inletBOT
hearty crag
#

It works!

#

Will I have to do that every single time I add an element to the panel?

acoustic dew
#

you could create a method to do that and just add it to your listeners like the following

textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar() == KeyEvent.VK_ENTER) {
                    System.out.println(textField.getText());
                    label2.setText("Welcome, " + textField.getText() + "! Good luck on your journey.");
                    refreshScreen(frame,panel);
                }
            }
        });
private static void refreshScreen(JFrame frame, JPanel panel) {
        frame.getContentPane().removeAll();
        frame.getContentPane().add(panel);
        frame.revalidate();
        frame.repaint();
    }
pure inletBOT
hearty crag
#

alrighty

acoustic dew
#

I did use that specific code in an app I made for switching between various jpanels with a cardlayout manager on a single frame from a JMenu. Glad it could also be used as a workaround for display issues!

hearty crag
#

It seems helpful