Re-painting of JPanel on hover/click of a JMenuItem - java

My desktop Java application has a JMenubar with several JMenuitems, and underneath it is a JPanel which I re-render when an item in the dropdown menu is clicked.
It all works okay, but my JPanel is re-rendering (my overridden paintComponent is being called) when I hover or click on my JMenuitems.
That is a problem, because on the JPanel are programmatically constructed images (randomly seeded), and the construction takes a while, so my program hangs if i hover over the menu too much..
Why is this and how do I fix it?
Edit: Even if I seed the random values and get the same image, the program does too many unecessary calculations and it becomes slow.

… (my overridden paintComponent is being called) when I hover or click on my JMenuitems. That is a problem, … Why is this …
It is expected behavior. The toolkit will repaint a panel whenever it determines it is necessary to do so: E.G.s
A menu appearing or disappearing over it
Another window or dialog dis/appearing over it
The user resizing the window …
… on the JPanel are programmatically constructed images (randomly seeded), and the construction takes a while, …
To avoid having to recreate a complex paint, draw the details to a BufferedImage then either paint the image in the paint method, or (simpler) display it in a label.

Related

Add Buttons and Graphics in the Same Frame?

I am creating a program in which users choose options (which are buttons) and ultimately the program should display a graphics output based on their options. I have created the program with the buttons (which works fine) but I was having trouble with the graphics. I was wondering if it is possible to have both graphics (ex: a black rectangle) and the buttons on the same frame. Or, do I need to exit the frame with the buttons and create a new frame with the graphics? Thank you for any help.

Sliding hidden sections of a JPanel into view

I want to display a JFrame ( made with the Netbeans GUI Editor ) that has an enclosed panel ( the panel encovers the entire JFrame ). The panel is twice as wide as the frame, so I want it so that when a button is pressed inside of the panel, the panel's visible area slides over ( over about 2 seconds) to the hidden area of the JPanel and the previously visible section of the JPanel becomes invisible. I couldn't find any function how to set the currently visible section of a JPanel, so the function and/or a different solution to this would be helpful.
I suggest that you put the JPanel in a JScrollPane, one that if you wish does not show its scrollbars. Then you could easily use the scrollpane's model and a Swing Timer to create an animation that shows the JPanel sliding.
The solutions is CardLayout based http://java-sl.com/tip_slider.html
You can add 2 (or more) panels into container and rotate them.

how to draw a line between two labels in a panel where labels are added during runtime

A panel contains two labels which are generated by mouse clicks on the panel. I want to connect these two labels by drawing a line between them probably by dragging the mouse pointer from one label to another.
There are two events here - one is clicking on the panel to generate the labels and the second is connecting these two labels (mouse pressed, mouse dragged and mouse released events). Both these event need to call the repaint() method but for different purposes. But there can be only one paint() method. The problem is when I connect these two labels, the line comes up but the rest of the components on the panel disappear.
That is probably due to the fact that you're overriding the panels paint() method.
Override paintComponent() / paintComponents() instead. No matter if you're using paint or paintComponent, do not forget to call super.paint() or super.paintComponents() respectively.
You can use a JLayeredPane instead of JPanel to draw multiple objects above eachother.
You can add your original JPanel to the JLayeredPane, and then add another one, with a higher Z-index and the opaque property set to true. Then the highest panel can be easily repainted without the other, lower, panel to show weird things.

How to disable the minimize button in JFrame?

Can I disable the minimize button in JFrame?
I have already tried setUndecorated() and setResizable() but both did not work.
I'm trying to add images to a panel at a random location (which works) but when the JFrame is minimized by clicking at the minimize button (not when frame minimizes by clicking the background window) images assemble at the top in a row.
Can someone help?
Thanks!
If you also want to disable the maximize button then you can use a JDialog instead of a JFrame... as far as I know you cannot disable the minimize button in a JFrame. JDialog only has a close button. Hope this helps
i m trying to add imags to a panel at
a random location which i m able to
do) bt wen frame is minimized by
clicking at the minimize button (not
wen frame minimizes by clicking at the
background window) images assemble at
the top in a row.
Well, it sounds like you are adding labels to a panel and using the setLocation() method to position the labels.
The problem is that by default a JPanel uses a FlowLayout so whenever you do anything to the frame like minimize, maximize, iconify or resize the frame the layout manger is invoked and the labels are arranged according the the rules of the layout manager.
If your requirement is to have random positioning, then you need to use a "null layout".
Read the section from the Swing tutorial that explains how Absolute Positioning works for more information and a working example.
Use JDialog instead of JFrame it has only the Close button on the top.

Removing a JPanel from a JFrame

I am currently working on an intro screen for a game designed using JOGL. I want the intro to be a JPanel with a few buttons to alter options before starting the game.
So, I have JFrame which I add a GLCanvas to. The GLCanvas also contains a GLEventListener. Finally, I add the JPanel.
I have overridden the JPanel paintComponent method to set a background image. I have a few buttons within the panel. Whenever you click the 'play' button, it calls a function which does:
frame.remove(JPanel);
frame.repaint();
animator.start();
What happens is that my JPanel goes away correctly, but when repaint is called, my frame is just filled with grey. I know that the animator is starting correctly as the display method in my GLEventListener is getting called.
Does anyone know what the problem is there?
What is your LayoutManager? If you didn't specify one, then by adding the JPanel you replace the GLCanvas.
I would make your code do this:
frame.remove(JPanel); frame.add(glcanvas); animator.start();
you may need to throw in a frame.revalidate(). I'm not up on how all that works right now.

Categories

Resources