Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to set a background image for my JPannel which contains many JTextField and JButton (I used g.drawImage()), but the the components won't appear unless the mouse passes by.
I can't make a JPanel for every component because I have too many of them.
Can anyone please help or point me into the right direction?
It sounds like either you've overridden paint or failed to call super.paintComponent when painting your image.
Make sure that:
You override paintComponent of your JPanel
You call super.paintComponent before you perform any custom painting
For example
Also take a look at Painting in AWT and Swing and Performing Custom Painting for more details about painting in Swing
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I have the following, code which almost does what I want except it doesn't automatically stretch to the right, so the whole label can be seen. I've tried different constrains on it, but it doesn't change, setting the JFrame bigger works, but I would like it to resize depending on the label length, so it doesn't occupy more space on the screen than needed.
The UI Window
Run JFrame.pack() to resize the frame according to it's components.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have a menu, that whenever I click on one of the options, I want to clear the screen (to clear all my JLabels and text areas). I use the code
getContentPane().removeAll();
getContentPane().repaint();
add(comboBoxOptions);
to clear the screen. After that I try to add a combo box, which adds but it doesn't show up. I can click on the options but it's hidden somehow I guess. How could I fix this?
How could I fix this?
Use a CardLayout, see How to Use CardLayout for more details
Swing's layout management API is lazy, it won't update the layout's automatically, it waits till you tell it to. This is a good thing.
You need to use revalidate to force the container hierarchy to be relaid out and repaint to schedule a repaint of the view, for example
getContentPane().removeAll();
add(comboBoxOptions);
revalidate();
repaint();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm using gridbaglayout and I want my window to be 700 x 500 always. I also was my JButtons to be the exact same size but if the word inside them is longer they get longer and I can't have this. How can I do this? Please help me with good code I been trying for hours.
I tried to do frame.setSize and panel.setSize but neither work and I need this fast! Please help!
The basic answer is, you don't. That's not the point of any layout manager.
A layout manager simple makes decisions about how best to layout it's children based on the sizing hints that they provide.
Remember, while it might look great on your screen, the next computer you run it on may make it look like crap.
If you "must" define the size of anything, then you need to override the getPreferredSize method of your component and return an appropriate size hint.
Having said that, I wouldn't do this for components like JButton (or actually anything other than JPanel and JComponent), the way they calculate their sizes are complicated and best left alone.
You can modify the size of components through the use of Borders and, in the case of GridBagLayout, Insets and modifying the GridBagConstraints properties.
Have a closer look at How to Use GridBagLayout for some more ideas
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm doing simple ATM transaction application using java swing. I'm using Jframe form as the front end design. All i need is to create a custom jframe window like avast antivirus window having different close, maximize and minimize buttons on the top.
Thats all thank you.
create undecorated JFrame, put there JPanel with Borders, change the LayoutManager to BorderLayout
put another JPanel to NORTH area, with three undecorated JButtons and to set setIcons, setRolloverIcon, setPressedIcon with desired Icons
have to use proper LayoutManager for JPanel with three JButtons, but, probably, maybe FlowLayout (default LayoutManager for JPanel) is proper for this job, and/or you would need to set alignment for FlowLayout
for every those JButtons is required to override proper event from WindowListener, the same event as for standard decorated JFrame
every of my steps (that I'm talking about) are described in Oracles tutorials
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am.working. on a Netbeans Java project in JFrame form (a GUI application) where I want to move a JLabel into circular path. Can any tell or help to how to do that?
It sounds like you haven't done anything yet, and when that happens and you need to get started, then you should try to break the main problem into little steps, and then try to solve each little step one at a time, including looking at references for each of these steps such as using a Timer, doing animation, positioning components, etc...
So general recommendations:
Look up using a Swing Timer (or just click on link)
Use the Timer to drive your animation.
You can move a JLabel if the layout is null, but this is generally to be avoided.
Consider instead using a custom layout if you absolutely need to move a JComponent (the JLabel) along a prescribed path.
Or if you just want to move an image, then draw the image inside of a JPanel's paintComponent(...) method, setting its position with two int fields that are changed by the Timer. This JPanel of course will need to be displayed in your GUI. There are lots of examples on how to do this on this site, some written by me (for example), that simple searching can help you find.
But most important, take the first steps, do something, anything, that moves you forward with this project.
Then when you try this if it doesn't work, show your code and we'll be much better able to help.