Java GUI Resize form after you create it - java

public static void main(String[] args) {
JFrame frame = new JFrame("Fantasy Football Toolkit");
frame.setContentPane(new GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 475);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Above is the main method to my GUI application. I'm wondering if I can change the size of the form after this main method runs. What if I want to change the size when I click a button? The problem is that I can't access frame. This has to be possible one way or another. I have looked all over for a solution but can't find one. Thanks.

You can always access the top level JFrame from any component that it holds via SwingUtilities.getWindowAncestor(yourComponent). This method returns a Window object, but if you are 100% sure that it's a JFrame, you can always cast it (or test it first that this is so).
Once obtained, you can always call pack() on it to resize it to the preferred sizes of the components that it contains. In general you will want to avoid setting sizes directly as that can lead to bugs later on when you modify your program.

Related

Adding multiple objects to JFrame without using a LayoutManager

Is there a way to add objects to a JFrame without using Layout Manager? I have tile objects(for the game 2048) that I am trying to add to a JFrame so I can call the JFrame then have a loop forever where the tiles are forever repainting themselves and I can press arrows to make them move depending on constraints(like if it can move in a specific direction.
Why I don't want to use a specific layout manager - my objects are tiles in the game 2048- which means they are constantly changing position which would mess with the layout manager setup ie flowlayout where all the JPanel objects are in a specific order and position.
Heres where I am trying to instantiate the JFrame:
public static void main(String[] args) throws InterruptedException {
//set up JFrame, tile objects
frame = new JFrame();
a = new tile(100, 100, frame);
b = new tile(200, 200, frame);
frame.addKeyListener(a);
frame.add(a);
frame.add(b);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setVisible(true);
//a loop so that it is continuously repainting and when i press a key something else happens
while(true) {
a.repaint();
b.repaint();
Thread.sleep(10);
}
It is possible to work with Swing without LayoutManager. Not using a LayoutManager allows and requires the application to have full control on the absolute position of the components.
Check out these fine resources:
https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Java swing GUI absolute positioning
In a nutshell, creating a container without a layout manager involves the following steps:
Set the container's layout manager to null by calling setLayout(null).
Call the Component class's setBounds() method for each of the container's children.
Call the Component class's repaint() method.

How to set a JPanel to a different one [duplicate]

To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action.
So, what's the correct way of doing such a thing? I've tried
panel = new CustomJPanelWithComponentsOnIt();
parentFrameJPanelBelongsTo.pack();
but this won't work. What would you suggest?
Your use case, seems perfect for CardLayout.
In card layout you can add multiple panels in the same place, but then show or hide, one panel at a time.
1) Setting the first Panel:
JFrame frame=new JFrame();
frame.getContentPane().add(new JPanel());
2)Replacing the panel:
frame.getContentPane().removeAll();
frame.getContentPane().add(new JPanel());
Also notice that you must do this in the Event's Thread, to ensure this use the SwingUtilities.invokeLater or the SwingWorker
frame.setContentPane(newContents());
frame.revalidate(); // frame.pack() if you want to resize.
Remember, Java use 'copy reference by value' argument passing. So changing a variable wont change copies of the reference passed to other methods.
Also note JFrame is very confusing in the name of usability. Adding a component or setting a layout (usually) performs the operation on the content pane. Oddly enough, getting the layout really does give you the frame's layout manager.
Hope this piece of code give you an idea of changing jPanels inside a JFrame.
public class PanelTest extends JFrame {
Container contentPane;
public PanelTest() {
super("Changing JPanel inside a JFrame");
contentPane=getContentPane();
}
public void createChangePanel() {
contentPane.removeAll();
JPanel newPanel=new JPanel();
contentPane.add(newPanel);
System.out.println("new panel created");//for debugging purposes
validate();
setVisible(true);
}
}
On the user action:
// you have to do something along the lines of
myJFrame.getContentPane().removeAll()
myJFrame.getContentPane().invalidate()
myJFrame.getContentPane().add(newContentPanel)
myJFrame.getContentPane().revalidate()
Then you can resize your wndow as needed.
Game game = new Game();
getContentPane().removeAll();
setContentPane(game);
getContentPane().revalidate(); //IMPORTANT
getContentPane().repaint(); //IMPORTANT
It all depends on how its going to be used. If you will want to switch back and forth between these two panels then use a CardLayout. If you are only switching from the first to the second once and (and not going back) then I would use telcontars suggestion and just replace it. Though if the JPanel isn't the only thing in your frame I would use
remove(java.awt.Component) instead of removeAll.
If you are somewhere in between these two cases its basically a time-space tradeoff. The CardLayout will save you time but take up more memory by having to keep this whole other panel in memory at all times. But if you just replace the panel when needed and construct it on demand, you don't have to keep that meory around but it takes more time to switch.
Also you can try a JTabbedPane to use tabs instead (its even easier than CardLayout because it handles the showing/hiding automitically)
The other individuals answered the question. I want to suggest you use a JTabbedPane instead of replacing content. As a general rule, it is bad to have visual elements of your application disappear or be replaced by other content. Certainly there are exceptions to every rule, and only you and your user community can decide the best approach.
Problem: My component does not appear after I have added it to the container.
You need to invoke revalidate and repaint after adding a component before it will show up in your container.
Source: http://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html
I was having exactly the same problem!! Increadible!! The solution I found was:
Adding all the components (JPanels) to the container;
Using the setVisible(false) method to all of them;
On user action, setting setVisible(true) to the panel I wanted to
show.
// Hiding all components (JPanels) added to a container (ex: another JPanel)
for (Component component : this.container.getComponents()) {
component.setVisible(false);
}
// Showing only the selected JPanel, the one user wants to see
panel.setVisible(true);
No revalidate(), no validate(), no CardLayout needed.
The layout.replace() answer only exists/works on the GroupLayout Manager.
Other LayoutManagers (CardLayout, BoxLayout etc) do NOT support this feature, but require you to first RemoveLayoutComponent( and then AddLayoutComponent( back again. :-) [Just setting the record straight]
I suggest you to add both panel at frame creation, then change the visible panel by calling setVisible(true/false) on both.
When calling setVisible, the parent will be notified and asked to repaint itself.
class Frame1 extends javax.swing.JFrame {
remove(previouspanel); //or getContentPane().removeAll();
add(newpanel); //or setContentPane(newpanel);
invalidate(); validate(); // or ((JComponent) getContentPane()).revalidate();
repaint(); //DO NOT FORGET REPAINT
}
Sometimes you can do the work without using the revalidation and sometimes without using the repaint.My advise use both.
Just call the method pack() after setting the ContentPane, (java 1.7, maybe older) like this:
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
....
frame.setContentPane(panel1);
frame.pack();
...
frame.setContentPane(panel2);
frame.pack();
...

Java Swing elements change every runtime

So I made a program using java's swing library. I made a program that graphs equations and here is the main method if it's relevant:
public static void main(String[] args) throws Exception {
JFrame frame= new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new GridLayout(1,2));
GraphPanel gp = new GraphPanel();
GraphPanel gp2 = new GraphPanel();
//gp.functs.add(new Function(Phrase.createPhrase("2(25-x^2)^(1/2)")));
//gp.functs.add(new Function(Phrase.createPhrase("-1.1((25-x^2)^(1/2))")));
gp.functs.add(new Function(Phrase.createPhrase("x^2")));
//gp.functs.add(new Function(Phrase.createPhrase("-4/x^2+6")));
gp2.functs.add(new Function(Phrase.createPhrase("sinx")));
frame.add(gp);
frame.add(gp2);
frame.pack();
frame.setSize(800, 800);
//gp.setBorder(BorderFactory.createLineBorder(Color.RED));
gp.setBounds(100, 100, 700, 700);//I WANT THIS TO ALWAYS RUN
}
I ran two trial runs of the program WITHOUT CHANGING ANY PART OF IT and this is what it looked like:
Then the next time i ran it:
If it's relevant, GraphPanel is of type JLabel.
I know that if i use a null absolute LayoutManager, it will always work. But I'm just wondering why swing has such inconsistencies in it. I've noticed stuff like this before but I though it was just some error in the program. Why is this?
Thanks in advance!
Start by moving frame.setVisible(true); to the end of the main method
This gp.setBounds(100, 100, 700, 700); is pointless, as gp is under the control of a layout manager (GridLayout)
There's no point in using both path and setSize, pack is generally a safer option, but that will depend on your components correctly overriding getPreferredSize
But I'm just wondering why swing has such inconsistencies in it. I've noticed stuff like this before but I though it was just some error in the program. Why is this?
Mostly because you're not using the API properly. It's possible, because of the way a JFrame is physically attached to a native peer, that the frame may or may not actually be visible on the screen when you reach gp.setBounds.
Also, because you're doing all your work from within the "main" thread and not the Event Dispatching Thread, you're running the risk of a race condition between them, see Initial Threads for more details.
Swing is VERY flexible, it's also unforgiving when you do the wrong things (or things the wrong way)

Is there a way I can swap JPanel classes into and out of a JFrame? [duplicate]

To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action.
So, what's the correct way of doing such a thing? I've tried
panel = new CustomJPanelWithComponentsOnIt();
parentFrameJPanelBelongsTo.pack();
but this won't work. What would you suggest?
Your use case, seems perfect for CardLayout.
In card layout you can add multiple panels in the same place, but then show or hide, one panel at a time.
1) Setting the first Panel:
JFrame frame=new JFrame();
frame.getContentPane().add(new JPanel());
2)Replacing the panel:
frame.getContentPane().removeAll();
frame.getContentPane().add(new JPanel());
Also notice that you must do this in the Event's Thread, to ensure this use the SwingUtilities.invokeLater or the SwingWorker
frame.setContentPane(newContents());
frame.revalidate(); // frame.pack() if you want to resize.
Remember, Java use 'copy reference by value' argument passing. So changing a variable wont change copies of the reference passed to other methods.
Also note JFrame is very confusing in the name of usability. Adding a component or setting a layout (usually) performs the operation on the content pane. Oddly enough, getting the layout really does give you the frame's layout manager.
Hope this piece of code give you an idea of changing jPanels inside a JFrame.
public class PanelTest extends JFrame {
Container contentPane;
public PanelTest() {
super("Changing JPanel inside a JFrame");
contentPane=getContentPane();
}
public void createChangePanel() {
contentPane.removeAll();
JPanel newPanel=new JPanel();
contentPane.add(newPanel);
System.out.println("new panel created");//for debugging purposes
validate();
setVisible(true);
}
}
On the user action:
// you have to do something along the lines of
myJFrame.getContentPane().removeAll()
myJFrame.getContentPane().invalidate()
myJFrame.getContentPane().add(newContentPanel)
myJFrame.getContentPane().revalidate()
Then you can resize your wndow as needed.
Game game = new Game();
getContentPane().removeAll();
setContentPane(game);
getContentPane().revalidate(); //IMPORTANT
getContentPane().repaint(); //IMPORTANT
It all depends on how its going to be used. If you will want to switch back and forth between these two panels then use a CardLayout. If you are only switching from the first to the second once and (and not going back) then I would use telcontars suggestion and just replace it. Though if the JPanel isn't the only thing in your frame I would use
remove(java.awt.Component) instead of removeAll.
If you are somewhere in between these two cases its basically a time-space tradeoff. The CardLayout will save you time but take up more memory by having to keep this whole other panel in memory at all times. But if you just replace the panel when needed and construct it on demand, you don't have to keep that meory around but it takes more time to switch.
Also you can try a JTabbedPane to use tabs instead (its even easier than CardLayout because it handles the showing/hiding automitically)
The other individuals answered the question. I want to suggest you use a JTabbedPane instead of replacing content. As a general rule, it is bad to have visual elements of your application disappear or be replaced by other content. Certainly there are exceptions to every rule, and only you and your user community can decide the best approach.
Problem: My component does not appear after I have added it to the container.
You need to invoke revalidate and repaint after adding a component before it will show up in your container.
Source: http://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html
I was having exactly the same problem!! Increadible!! The solution I found was:
Adding all the components (JPanels) to the container;
Using the setVisible(false) method to all of them;
On user action, setting setVisible(true) to the panel I wanted to
show.
// Hiding all components (JPanels) added to a container (ex: another JPanel)
for (Component component : this.container.getComponents()) {
component.setVisible(false);
}
// Showing only the selected JPanel, the one user wants to see
panel.setVisible(true);
No revalidate(), no validate(), no CardLayout needed.
The layout.replace() answer only exists/works on the GroupLayout Manager.
Other LayoutManagers (CardLayout, BoxLayout etc) do NOT support this feature, but require you to first RemoveLayoutComponent( and then AddLayoutComponent( back again. :-) [Just setting the record straight]
I suggest you to add both panel at frame creation, then change the visible panel by calling setVisible(true/false) on both.
When calling setVisible, the parent will be notified and asked to repaint itself.
class Frame1 extends javax.swing.JFrame {
remove(previouspanel); //or getContentPane().removeAll();
add(newpanel); //or setContentPane(newpanel);
invalidate(); validate(); // or ((JComponent) getContentPane()).revalidate();
repaint(); //DO NOT FORGET REPAINT
}
Sometimes you can do the work without using the revalidation and sometimes without using the repaint.My advise use both.
Just call the method pack() after setting the ContentPane, (java 1.7, maybe older) like this:
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
....
frame.setContentPane(panel1);
frame.pack();
...
frame.setContentPane(panel2);
frame.pack();
...

Java -- JDialog unmovable

What code will facilitate making a JDialog unmovable? I've looked at two options:
setUndecorated(true); which works but removes all the trimmings.
addComponentListener and overriding the componentMoved() method, which causes the JDialog to subsequently call induceEpilepticSeizure() upon moving.
Any ideas?
My first instinct is - you can't unless you DO use setUndecorated(true)... You could manually put some trimmings there, but, well, UGH!
So if you want the native trimmings AND you want it immovable without the horrible flickering from using a component listener, I think you can't.
You could create a border manually that LOOKS like the default border...here's an example of how to do it, although I've intentionally made the border look like the ugliest thing you've seen all day. You'll need to find the right combination of BorderFactory calls to achieve what you want to do.
public static void main(String[] args) throws InterruptedException {
JDialog frame = new JDialog((Frame) null, "MC Immovable");
frame.setUndecorated(true);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.RED));
panel.add(new JLabel("You can't move this"));
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}

Categories