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)
Related
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.
Similar to what you can do with a Java's Canvas class, I am currently trying to write a method to set an LWJGL Display's maximum size. The only problem I have run into is that LWJGL's Display class doesn't have a setWidth or setHeight method, and I can't think of an way I could write my own. My first thought was to somehow get the instance of the JFrame that the window was using, but there's no method for this either so I feel like I am SOL. I also tried using DisplayMode and just resetting the size through that, but it closes the window and reopens it every time it resizes. I am trying to write a method that'd simply resize the window as if the user was dragging it to enlarge it. Does anybody know how I could/should go about doing this? Is it even possible to get this smooth max-size effect I am looking for, considering that the display doesn't even update until you let go after dragging/resizing it manually?
If you need more control over your window, you can try using an AWT Frame as a parent to an LWJGL Canvas. This would allow your code to take advantage of default window methods, including setWidth() and setHeight() found in the Java standard libraries, prevent and control window behavior, and add listeners to run code for certain events.
First, import java.awt.* and then set up code that looks something like this:
Create a Frame and Canvas:
Frame frame = new Frame("LWJGL Game Window");
frame.setLayout(new BorderLayout());
Canvas canvas = new Canvas();
Add the Canvas to the Frame:
frame.add(canvas, BorderLayout.CENTER);
Setup LWJGL and apply it to the Canvas:
try {
Display.setParent(canvas);
frame.setPreferredSize(new Dimension(1024, 786));
frame.setMinimumSize(new Dimension(800, 600));
frame.pack();
frame.setVisible(true);
Display.create();
while(!Display.isCloseRequested() && !closeRequested)
{
// main code or call to code goes here
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
Display.update();
}
Display.destroy();
frame.dispose();
System.exit(0);
} catch (LWJGLException e) {
e.printStackTrace();
}
Not sure how much you know about Java, but from here, you could add an ActionListerner or ComponentListerner that does something if the window is resized.
Also check out the LWJGL wiki for AWT Frames. This should, at least, help to point you in the right direction.
I'm working on an online mode for a new game and in order to prevent cheating I need fix window sizes (and both players need a window with the same sizes).
I used 'jframe.setResizable(false);' but it seems to be "glitchy".
When I click the window and move it away from the border of the screen, Windows does minimize it.
Here's a video about it:
http://www.youtube.com/watch?v=AQ7OHJOuLSk&feature=youtu.be
I've tried following code in order to fix it:
Dimension d = new Dimension(width, height);
panel.getJFrame().setMaximumSize(d);
panel.getJFrame().setMinimumSize(d);
panel.setMaximumSize(d);
panel.setMinimumSize(d);
and I created a Component Listener:
if (max_height!=-1){
if (e.getComponent().getSize().getHeight()>max_height){
e.getComponent().setSize((int) e.getComponent().getSize().getWidth(),max_height);
}
}
if (max_width!=-1){
if (e.getComponent().getSize().getHeight()>max_width){
e.getComponent().setSize(max_width,(int) e.getComponent().getSize().getHeight());
}
and I tried to work with Layouts but nothing worked.
What I need now is either the possibility to prevent that minimize "glitch" (If it is a glitch) or a way to make the JPanel not resizable. Like when the size of the JFrame window is changed, the JPanel always stays the same. It's neither streched nor minimized.
Help is much appreciated :)
Sincerely Felix
So far the best patch for this annoying issue is the following. Doesn't matter where you call the setResizable(false) method. Just add this piece of code after you setVisible(true).
private void sizeBugPatch() {
while (frame.getWidth() > yourWidth) {
frame.pack();
}
}
Where yourWidth is the width you've set in any of the possible ways, either manually or by overriding setPreferredSize methods. The explanation is quite easy, frame.pack() seems to reset frame.setResizable(boolean b) somehow. You could use an if instead of the while loop but I prefer while to exclude the case the window would still be extra-sized even after a second pack().
Did you initialize the variable jframe or are you calling the general Class?
Because if you do it like this:
JFrame frame = new JFrame();
frame.setResizable(false);
It works fine for me...
I'm trying to implement a new Jframe in net beans with
JFrame frame = new JFrame("shooter");
however when it is run nothing is happening. I have run the same code in another IDE and it works fine however I am not seeing a JFrame on the screen. I ran a test to see weather everything was compiling and that there was no syntactical or lexical error so nothing seems to be wrong per say.
Thanks.
It is difficult to provide an answer with the information you've provided, but I'll try my best. By instantiating a new JFrame object, you aren't making it appear on the screen.
An example of creating and showing a new frame would look something like this
JFrame frame = new JFrame("Title");
frame.setSize(500, 500); //This method will set the size of the frame to 500 by 500.
frame.setVisible(true); //This method makes the frame visible
I hope I could be of help.
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);
}