I want to get the present size of my Jframe whenever i click on the button1. This is the code which i have written in my button1clickevent initially when i click on the button i do get a width and height but when i change the size of my Jframe and then again i click on button1 i get the same width and height as i got earlier.The values are not changing anytime.
AppDisplay ap=new AppDisplay(); //class which has got the JFrame
int f12;
f12=ap.getContentPane().getSize().width;
System.out.println(f12+"\n");
f12=ap.getContentPane().getSize().height;
System.out.println(f12);
I think your referencing the wrong Object. What's AppDisplay & why do you need a new instance?
Also, what's wrong with Frame.getSize(); ?
You are printing the size of the Content Pane which is inside the JFrame. Use the JFrame's getSize() method to get the size of the frame.
final Dimension size = ap.getSize();
System.out.println(size);
Also, MadProgrammer makes a good point that the code you show creates a new instance of AppDisplay. You can create as many instances as you want, even without showing them. You will always want to carefully consider the lifecycle of the objects and the scope of the variable names that reference them so that you can refer to the intended object every time.
one idea i found that.
Place a jButton ("jbutton1") one the jFrame.
and write the code inaction performed of the buton.
System.out.println(this.getSize());
then run the frame.
adjust the frame to required size.
then click the button..
we will get the updated size..
then we can set it as the actual size of the frame from properties of jframe...
Related
I have created a java program in which i have used the setContentPane() method on a JFrame. My problem is that the frame does not get updated by the new panel immediately but gets updated upon resizing the frame.
In my program the size of the JFrame is 600,600.
To temporarily avoid this problem, after the setContentPane() method , I have applied a set of 2 statements:-
Frame.setSize(601,601);
Frame.setSize(600,600);
This resizes the frame and brings it back to its original size without showing much change to the user. This is working good and the panel is getting updated.
My question is, am I making any mistake and there is some other way to do it? Or I can continue using this trick? Please answer if there is any other(right) way to do it.
I'm making a program for fun, it's basically a computer navigation GUI, details not required :)
Anyway, so far, I have a button called "new button" that, when clicked, it creates a new button named "test", to an infinite amount. Right now, i have my GUI set up like this:
Class Main extends JPanel (the main panel that holds everything in it, size set as)
Dimension size = new Dimension(300, 200);
setPreferredSize(size);
JFrame holding the Main JPanel, called like:
panel.frame = new JFrame();
panel.frame.setResizable(false);
panel.frame.setTitle(panel.title);
panel.frame.add(panel);
panel.frame.pack();
panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.frame.setLocationRelativeTo(null);
panel.frame.setVisible(true);
So, how would i have the JFrame/JPanel set their size based on the components inside it? I've tried to use panel.frame.pack(); but i get an error most of the time, and the other times it doesnt wrap, it is just a staight line. I want it to resize in the form of a square. Any ideas? Sorry if my question isnt clear/poorly phrased, i've always had issues articulating questions online, much better in person cause i can use my hands! :) Thanks in advance!
Class Main extends JPanel (the main panel that holds everything in it, size set as) Dimension size = new Dimension(300, 200); setPreferredSize(size);
Don't set the preferred size of the panel. The layout manager will determine the preferred size based on the components that you add to the panel.
and the other times it doesnt wrap,
The default layout manager for a JPanel is a FlowLayout. It is not designed to wrap automatically. Maybe use a different layout manager. Or you can try the Wrap Layout which extends FlowLayout to provide dynamic wrapping.
I've tried to use panel.frame.pack(); but i get an error most of the time
What error. I've never seen an error when using the pack() method.
Post a proper SSCCE if you need more help.
What I want is that the 1st time that the user open my app the left panel can use the full screen!
then I need to resize it.
After reading the SlidingPaneLayout source :SlidingPaneLayout.java, you will find DEFAULT_OVERHANG_SIZE = 32 in this class as the default size of the overhang for a pane in the open state.
Since this is a private static constant,you need to change it into a variable and create its set method.
Take care of onMeasure() method and good luck.
I am trying to create a code that simulates a MS Paint in java using Jframe. I want to create a textbox like field like MS Paint has,wherein you drag a box and according to your preference you set a size for it. What I do is I draw a rectangle first and then get the dimensions by mouse event listener and pass these values to a function that creates a JtextArea of given size and width. However, I need to extend the Jframe class which creates a new frame on top of the one that already exists. I try to pass my original frame as a parameter to draw upon for the JtextArea which does not work. Is there any way of implementing JtextArea without extending the frame class? And If possible any relevant example to draw a textbox which is similar to MS Paint. Please note that I don't want to use the Graphics.drawstring method. Thanx.
Since you don't want to use drawString() directly, java.awt.font.TextLayout is probably the best option for rendering text.
Create a temp JTextArea and add to your drawing panel with null layout to be placed over the rectangle.
When edit is done (text entered) remove the temp textarea, get the user entered text and draw it in the original rectangle.
I am using a JPanel (with several labels inside) to add a dynamic information on a graph. This panel is dynamically created, it is not visible before I use it to draw.
For this, I am using a BufferedImage, and I follow approximately the same steps as described on this other question. It works good, as long as I specify all sizes (the panel, and its components).
Like asked as well in comments of the referred question, how can I determine the optimal size of this panel? The same operation would be done if this panel was displayed in a regular frame/layout setting.
In my case, how can I "pack", in a way, this panel, so that its size, and size of its content are set to the optimal (determined by the size of labels, then)?
Suraj and willcodejavaforfood put me on the good track.
Checking what is actually done in a pack() method, I see that this is mostly setting the current size to the one returned by getPreferredSize().
From this, I managed to make such solution:
// Creating the panel
JPanel lPanel = new JPanel();
//lPanel.setSize(1000, 1000); //default size, not needed anymore
lPanel.setLayout(new BoxLayout(lPanel, BoxLayout.PAGE_AXIS));
//Adding the content
lPanel.add(new JLabel("Blah"));
// etc...
//Adjust the panel to its preferred size
lPanel.setSize(lPanel.getPreferredSize());
//Call the layout method
//(this will adjust the content components to their correct size and position)
lPanel.doLayout();
This method works correctly, and adjusts the panel and its content to the correct size (and answers my question in a simplistic way: "how to find the preferred size? getPreferredSize()").
However, it requires to set the initial size to a large enough size, so that the content fits in, or they won't be put on the layout. This is a bit pity, and not really "clean", but I can't find a way to avoid that, for now.
Edit: Actually, the default size was not necessary, because getPreferredSize() returns the correct value, even before calling doLayout(). As such, the panel can be set to its proper size before calling the layout method.
The direct answer is to call Window#pack(). This method will automatically set the size of all underlying children to thier preferred sizes(ofcourse this depends on layouts of child containers, for e.g. BorderLayout doesent give a damn about preffered sizes).
So as long as you have set preferred sizes(or min/max sizes in case layouts are like BorderLayout) of your child components, pack() method will be all you need.
[UPDATE]One way is to do is add a HierarchyListener to your jpanel and check for HierarchyEvent#DISPLAYABILITY_CHANGED events. This event is called when your panel is realized that is ready to be shown(and a parent is available), at this moment you can do:
SwingUtilities#getWindowAncestor(myPanel).pack();