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.
Related
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.
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.
Before you say something, i know the implications of having more than one JFrame. I'm kinda delayed and i need to add the components manually.
So, i open a JFrame that i have designed with a button click:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame DataCalc = new JFrame();
DataCalc.setVisible(true);
DataCalc.setSize(500, 500);
DataCalc.setLocationRelativeTo(null);
}
Then the JFrame shows up but doesn't show my components. I read that if i setVisible before adding components they won't show, but they're already there cause i designed them.
If i change my code and add the setSize and setLocation like the following code, nothing happens besides the JFrame opening.
public DataCalc() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
initComponents();
}
Sry for the post, i'll edit my post if you need more info.
JFrame DataCalc = new JFrame();
Should be:
JFrame dataCalc = new DataCalc(); // use the CUSTOM frame!
I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn't show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.
Here is my simple code :
public static void main(String[] args) {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setVisible(true);
frame.setSize(new Dimension(800, 600));
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
}
So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.
Thanks :)
Do not add components to JFrame after the JFrame is visible (setVisible(true))
Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.
Use EDT (Event-Dispatch-Thread)
call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by #Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}
Your simple code is missing a few things.
You have to invoke SwingUtilities to put the Swing components on the event dispatch thread.
You should call the setDefaultCloseOperation on the JFrame.
You have to call the JFrame methods in the correct order. The setSize or pack method is called, then the setVisible method is called last.
public class SimpleFrame implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleFrame());
}
}
I'm following through a book called "The JFC Swing Tutorial (Second Edition)" and I'm pretty much at the start I have followed this code and it should be displaying the button and the label in the content pane, but All im getting is a blank screen. any ideas?
Thanks.
import java.awt.GridLayout;
import javax.swing.*;
public class m extends JFrame
{
void UserFrame()
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hellow You");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new GridLayout(0,1));
//makes label
JLabel label = new JLabel("Sup ");
//adds to the frames content pane a label
frame.getContentPane().add(label);
JButton button = new JButton("Hai");
frame.getContentPane().add(button);
jp.add(button);
jp.add(label);
jp.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
//pack set the window to what it needs AKA to display all components
frame.pack();
//frame.setSize(250, 250);
//shows window
frame.setVisible(true);
}
public static void main(String[] args)
{
final m window = new m();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
window.UserFrame();
}
});
}
}
Simply add
frame.add(jp);
just before
frame.pack();
What's happening here? You correctly add all your widgets to a JPane, but you basically threw that JPane away and didn't use it anywhere.
This will be sufficient just to get it to work properly.
If you want to do it correctly, you should also remove frame.getContentPane().add(label); and frame.getContentPane().add(button); (Thank you #dic19 for noting that!). These will not work the way you used it.