Using setContentPane() method correctly - java

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.

Related

How to add another button using JButton, without resizing the frame to make it visible?

I'm still new at Java and having trouble at adding buttons using another JButton, the problem is that I can able to add them but right after I resize the form only. It means that the frame did not shown any JButton added to the panel unless I resize it.
right after I posted it, they already recommended related topics here and it was resolved now thanks!
just need to add 'revalidate();' at the component for it to dynamically change along the actionListener.

Update JFrame size and extended State

Hello i want to set the size of JFrame, when the user changes the screen resolution.
this.setSize(myWidth, myHeight);
This is working fine and the window gets smaller/bigger. But if I set the Extended State, it will not affect. I can still see the Menu Bar.
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setUndecorated(true);
Must I call a function, when I'm changing the ExtendedSate and the JFrame is running? In the initialisation, ExtendedState is working fine without calling a function, which rebuild or reload the JFrame.
I have found no really simple native way, without a lib.

java JFrame setSize

I am loading a JFrame showing the company logo and credits etc before starting the main application. I am having some problems. Fist of all, the size of my new JFrame can never be set. The JFrame looked fine when I previewed it under netBean but came out smaller every time. I tried to do it with a new constructor and setSize(), but still not working. Second, the JFrame has been loaded very slowly. No images and everything could be loaded and the JFrame stays blank for at least five seconds, really kind of annoying. Do it have anything to do with where I put the image files?
Thanks alot.
I am loading a JFrame showing the company logo and credits etc before starting the main application. I am having some problems. Fist of all, the size of my new JFrame can never be set. The JFrame looked fine when I previewed it under netBean but came out smaller every time. I tried to do it with a new constructor and setSize(), but still not working.
It is very difficult to suss out what is wrong without seeing your code, but having said that, your comment about this being a NetBeans-generated GUI suggests that the code will be very large and hard to read and interpret. It is for this and many other reasons that I am not a fan of using NetBeans to generate GUI's, especially for newbies who are just learning how to use Swing. I suggest that you write out your GUI code by hand with some user-friendly layout managers, nested by nesting JPanels if necessary. If you do it this way, you'll have some greater flexibility and control in the construction of your GUI, and you'll also have readable and debuggable code that you can post here for our assessment and help should it not work out right for you.
Second, the JFrame has been loaded very slowly. No images and everything could be loaded and the JFrame stays blank for at least five seconds, really kind of annoying. Do it have anything to do with where I put the image files? Thanks alot.
This sounds like a threading issue. I'd just load the images for the intro GUI first, then show the intro window, and then in a background thread, load any other resources that the program needs.
Having said all this, you probably want to look into using Java's own splash screen as this may do all that you're trying to cobble together on your own. The tutorials can help you with this (please click on link above or here).
Much unclear question description though I want to point some tips...
Do it have anything to do with where I put the image files?
If you mean your JFrame title image so you can use its setIconImage() method
Fist of all, the size of my new JFrame can never be set.
It is quite strange :( because you can always write code like a
JFrame aFrame=new JFrame();
aFrame.setSize(x,y);
... to control your frame scale
Second, the JFrame has been loaded very slowly. No images and
everything could be loaded and the JFrame stays blank for at least
five seconds
It may be caused by single thread overloaded but still to analyze the problem show the main class code or its exceptions stack trace
previewed it under netBean but came out smaller every time
what component do you use to paint your logo? And how do you paint it? Show the snippet
You can set the size by right click on the jFrame and
Set Default size
(netbeans design view)
I hope this is what you want
this may be useful for you?
JPanel aa = new JPanel(new GridBagLayout());
aa.setMaximumSize(new Dimension(500,500));
aa.setMinimumSize(new Dimension(490,490));
aa.setBorder(BorderFactory.createLineBorder(Color.black));
I opened the properties dialog for my JFrame and set "minimum size" field to [500,500] but that did not work.
What did work for me was selecting the "..." button at the right of the "minimum size" property. In the "JFrame Mininumsize" dialog as follows:
set dropdown (Set forms minimum size property using: ) = "Custom Code"
set code in (Form.setMinimumSize( ) ) = "new Dimensiom(500,500)"

Getting the correct sizes for JComponent after using GroupLayout

I wanted to ask whether it is possible to get the correct sizes of an JPanel after it has been placed in another JPanel that uses GroupLayout as Layout Manager. I have already tried to use:
.getPreferredSize(): this results in the Preferred Size that has been set by me, not the actual size that is drawn on the JPanel in the frame (if frame get's resized, the element will expand horizontally; which is not seen in the values).
.getSize(): it returns 0.
.getHeight(): it returns 0.
.getWidth(): it returns 0.
Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.
To force it to do that after it is shown on screen, maybe I can use EventQueue, but I'm not sure how.
Thank you for your answers!
You can get the "correct" size of the component only after it has been rendered, either by calling pack or setVisible(true) on the top level container.
Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.
Then something's not right. Are you sure that you're calling these methods on the visible components and not some variables that shadow them? Without code it's hard to tell where your error lies.

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories