Why doesn't JTextfield in java display text at runtime? - java

Hope you are all well!
Hope you understand my Java question... I have created a JFrame window with text to display, but it doesn't display at run-time (as it should) unless I maximise the Frame window.
Can't understand it?
Here's some code:
package test;
import javax.swing.*;
class Test{
private String x;
private Test() {
x="150";
}
public static void main(String[] args) {
Test o1 = new Test();
JTextField l = new JTextField(o1.x, JTextField.CENTER);
l.setAlignmentX(0);
l.setAlignmentY(0);
JFrame window = new JFrame("Hello World!");
window.setSize(800, 600);
window.setResizable(true);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(l);
}
}

Altering the size of the frame (by maximising it) will cause it to be repainted. The reason it needs to be repainted is that you added content to it after you already made it visible.
Instead, you could move window.setVisible(true); to the end, so you don't show the window until you've added everything to it.

Related

Java Swing adding a JButton to a JFrame doesn't work

I have been coding with Swing for a long moment in windows and when I switched to Manjaro Linux, something as basic as adding a JButton to a JFrame wouldn't work. (Also I have no output in the console, no missing libraries only a blank window).
Can someone help me figure this out?
Here is the code:
package test;
import javax.swing.JButton;
import javax.swing.JFrame;
public class main {
public static void main(String[] args) {
JFrame window = new JFrame();
JButton button = new JButton("My button");
window.setSize(600,600);
window.setVisible(true);
window.add(button);
}
}
As mentioned in a comment:
window.setSize(600,600);
window.setVisible(true);
window.add(button);
Should instead be
window.add(button);
window.pack();
window.setVisible(true);
Always add all the components before setting the window visible.
window.setSize(600,600); is no better than a guess, whereas window.pack(); makes the window the size it needs to be.
This is the code used for the above screenshot. Further details in code comments.
import java.awt.Insets;
import javax.swing.*;
public class DisplayButton {
public static void main(String[] args) {
JFrame window = new JFrame();
JButton button = new JButton("My button");
// This is a random guess as to how big the window should be. DON'T GUESS
//window.setSize(600,600);
// The button should be added before the frame is packed to size and displayed
window.add(button);
// But since you want it BIG, let's make it so,
// usefully by first increasing the text size
button.setFont(button.getFont().deriveFont(60f));
// Then add a chunk of insets - adjust to need.
button.setMargin(new Insets(20, 100, 20, 100));
// Make the window the size it NEEDS to be to display the button.
window.pack();
window.setVisible(true);
}
}

How do I prevent JFrame from always being on top of all other applications?

Before anyone asks, I have tried using setAlwaysOnTop(false). Here is a repeatable example.
import javax.swing.JFrame;
public class SOQ_20200913
{
public SOQ_20200913()
{
JFrame frame = new JFrame("SOQ_20200913");
//for simplicity's sake, you could also comment these 2 lines - they don't seem to help or hurt the situation
frame.setLocation(200, 200);
frame.setSize(300, 300);
frame.setAlwaysOnTop(false);
frame.setVisible(true);
}
public static void main(String[] args)
{
SOQ_20200913 stackOverflowQuestion = new SOQ_20200913();
}
}
After I run, I click away and try to click on my code, and then my web browser, but the JFrame always remains on top.
Am I missing something? Is there some other field I should be setting here?

I use setVisible(true) but Java Swing Window will not appear

I don't use anything IDE.
import javax.swing.*;
public class FirstAttempt
{
public static void main(String[] args)
{
JFrame window = new JFrame("My first window");
window.setSize(100,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
It doesn't appear. Why?
Perhaps the window initializes visibility outside of your monitor's display area? Try adding
frame.setLocationRelativeTo(null);
after your setVisible() call to ensure that the window is initialized at the center of the screen.

Prevent a JWindow to remain always on top

I created a JFrame and and a JWindow. My problem is that when I click on another application my JFrame passes behind the application but not my JWindow which remains always on top.
I tried to call setAlwaysOnTop(false) on my JWindow but this doesn't change anything.
I would like that the JWindow "follows" the JFrame.
Here's my test code:
public class WindowAlwaysOnTop {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 400));
final JWindow window = new JWindow(frame);
window.setAlwaysOnTop(false);
window.setSize(new Dimension(200, 200));
frame.setVisible(true);
window.setVisible(true);
}
}
This problem occurred with JRE 1.6.0_32 and is solved with JDK7.
Don't use a JWindow.
Instead use a JDialog. Just make sure you specify the frame as the parent when you create the dialog. You can use an undecorated dialog if you don't like the titlebar.

JTabbedPane shows itself randomly

the problem I encountered is weird for me, because I was doing everything step by step, correctly (in my opinion) and finally when I could say I finished one part of my program it appeared to make a fun of me. The actual problem is that in GUI I created I used a JPanel, then I've put it into a JTabbedPane which I've finally put into a JFrame. Everything is fine and works apart from times when it doesn't. I know it sounds strange, but after running program once I get what I wanted (Frame with tabbed pane containing panel with some stuff in it) and then when I run it again it either show the correct thing again or just empty frame. The worst thing is that it's so random, I haven't got a clue what can be wrong, I don't even know what exactly should I google to find it out. The code is:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class GUI extends JFrame {
JFrame frame = new JFrame("WakeOnLan script generator");
JPanel panel1 = new JPanel(null);
JTextArea text; //= new JTextArea("test");
JScrollPane scroll = new JScrollPane();
JButton but = new JButton("test");
JTabbedPane tab = new JTabbedPane();
public GUI() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width-w)/3;
int y = (dim.height-h)/4;
frame.setSize(500,500);
frame.setLocation(x,y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(null);
createTab1();
tab.addTab("Tab 1", panel1);
tab.setVisible(true);
tab.setBounds(0, 0, 500, 500);
frame.add(tab);
}
public void createTab1(){
text = new JTextArea("test");
text.setVisible(true);
scroll.setViewportView(text);
scroll.setBounds(10,10,465,300);
panel1.setLayout(null);
panel1.add(scroll);
panel1.setVisible(true);
panel1.setSize(500,500);
//panel.setBackground(Color.blue);
}
}
And then I just run it in the main method in other class:
public class GUIStarter {
public static void main(String[] args) {
GUI start = new GUI();
}
}
So could anyone give me an answer or just a hint?
Thank you.
You should call frame.setVisible(true) after adding all your components to your JFrame. So try moving it to the end of your constructor.
Alternatively, you can call frame.validate() after all the components have been added.

Categories