What is the best practice for loading a slow JPanel for the first time?
I have a JButton, that, when clicked, loads a new JPanel that contains 52 JLabels with each containing an image (it's a card game).
But clicking this JButton freezes the GUI for around 5 seconds as the JPanel loads for the first time.
I know I should use a SwingWorker, but I tried initializing the JPanel, adding it to the JFrame, and calling its repaint() all within a SwingWorker, to no avail.
How should I properly handle loading the JPanel in the background so that the GUI doesn't freeze while it loads? (Side note, If I quit back to the main menu and click the button again, it takes a split second since it has already been loaded once)
Thanks!
Related
I have a question. I couldn't solve it for 4 days. I'm making a stopwatch using gui swing in Java. There are 1 jframe and 1 jdialog. I used threads in Jframe. It has a start stop and reset button, it works very well. The problem is; I had to use the jdialog like this (this is the job situation) so when I press the start stop and reset button that I created in jdialog, it triggers the start, stop and reset button in the jframe. There is no problem with triggering either. The main problem is this: While the thread works as it should in the jframe, it does not work when I call it from jdialog. I searched a lot, I made a logic, but I couldn't find it, can anyone help?
I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.
Ok anyways moving on.
I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.
What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.
Any advice?
EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.
Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.
I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.
That is my issue.
See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.
I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it's not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this: getWindowAncestor(Component c). Then you could call dispose() on the window returned.
i.e.,
Window win = SwingUtilities.getWindowAncestor(myJPanel);
win.dispose();
Note that this is fine if you're using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.
The button is already on the frame (on the upper right, here). Just call JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) when constructing it and it will work as the user expects.
Other tip
Don't extend frame or panel, but instead just create and use them.
I have several JPanels that contain buttons, labels, etc. that I want to switch between from a main JFrame. Currently I am trying to use the this.add(JPanelname); method and this.remove(JPanelname); with the validate(); and repaint(); methods
The problem is it will add the panel to the JFrame but it will not remove it. I am not sure how exactly to go about this.
Maybe you should be using a Card Layout.
Or maybe you should be using modal JDialogs. So whenever you click on the "widjet" a new window is displayed. Then when you close the dialog you are back on your main frame.
If you are constantly switching between JPanels, then a JTabbedPane may be the right thing to use. It should not be necessary to call "validate" or "repaint" when you add or remove a JPanel. Do you have a layout manager installed? Do you make sure to call add/remove only within the UI event thread? Also, typically one does not call "validate()" but rather "invalidate()" to invalidate the container for updates.
I'm creating a simple java game Applet that has multiple Panels, The main game Panel has 4 JButtons that lead to the rest of the Panels when they are clicked.
when the program runs, the four Panels are initialized 1st inside the init(), and inside each Panel initialization, I made all the Jcomponents invisible but only the main applet.
lets say there is a JButton in the main Applet Called start, when it's pressed, i need to set all the main JButtons to invisible, and set the sub Panel to visible but it's not working for me, I used everything I could think of, like repaint() or UpdateUI() but still not working.
any suggestions would be much appreciated.
Cheers
First, ensure all creation is performed not in init(), but in the EDT, see the tutorial. If you have an ampty start() method I'd recommend you use invokeLater in the init() (instead of the tutorial recommendation of invokeAndWait).
To hide buttons simply call setVisible on the JButton. There should be no need to ask for a repaint afterwards.
Further analysis is difficult without seeing the code.