#JLabel only appearing on second monitor
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
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
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
@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.
so using changeListener is the best solution for it? @acoustic dew
It'll be a better solution in my eyes, and should solve your problem.
Alright, I'll keep you updated
@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
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();
}
Detected code, here are some useful tools:
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?
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.
Okay
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.
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
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?
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
Its possible it could be a display driver issue. Is your driver up to date?
I believe so; how would I check on macOS?
I'm on macOS Sonoma beta, that could be the issue
It looks like you can do so by checking if your mac has any software updates.
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
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.
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
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?
Detected code, here are some useful tools:
add that to the key listener keypressed method and see
That should essentially clear the screen and readd the items to the screen
Where would I add that in exactly?
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();
}
}
});
Detected code, here are some useful tools:
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();
}
Detected code, here are some useful tools:
alrighty
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!
It seems helpful