Java Applet Panel Refresh - java

I have a Java Applet that loads a record of a board game from a URL and displays the configuration using a series of panels laid out in a grid and nested in a JPanel. This is a small component of an overall applet with text fields and such for inputs and outputs. When a button is pressed, I want the board to show the next configuration from the record. I figured out a way to do this, but when the button is pressed, the same configuration is shown, but when I scroll up or down, or resize the window, then the configuration changes. Is there a way to "refresh" just the board JPanel when the button is pressed? Currently, when the button is pressed, the new configuration panel is created, the old configuration is removed, and the new configuration is added to the display.
Thank you very much!

If nothing else, you could try hiding and then immediately showing the panel again. That should force it to redraw.

Related

How can I make two or more panels in Java Swing/GUI visible at once without having to press a button?

I am designing a game for a project in Java Swing using a JForm. I know that you have to use the code
jPanel1.setVisible(true);
I used this for all panels, even in the init() method in the source.
I am using the "Layered Panel" option for every panel I add. I placed the image on the first panel:
There are radio buttons in my second panel. I want the user to click on the option while looking at the picture, kind of like clicking on an icon (radio button) on desktop (background image).
Like this:
When I try to look at my design preview, it just shows me Panel2 (only radio buttons), not 1.
Is it possible to fix this (make two panels visible at once), or should I just make a button to switch between the panels?

Button remains focused after clicking

I'm getting some trouble with a toolbar I made. In this I have 4 JButtons and below a JTable, which is set on focus when my windows shows in order to see the JButtons unfilled.
This is how I set each JButton:
btn = new JButton("New");
btn.setFocusPainted(false);
btn.setBackground(SystemColor.window);
btn.setVerticalTextPosition(SwingConstants.BOTTOM);
btn.setHorizontalTextPosition(SwingConstants.CENTER);
btn.addActionListener(new ActionListener() {
...
});
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
toolBar_General.add(btn);
btn.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/file11.png")));
Example of my toolbar at start: http://www.tiikoni.com/tis/view/?id=0ef950b
But after clicking on a JButton, it remains filled after itsactionListener does his work.
Example of my toolbar after clicking a JButton: http://www.tiikoni.com/tis/view/?id=9e88ea4
My question is: how can I mantain my buttons contentAreaFilled property true (so the user can tab between components in my window and see which control he has selected) and unfocus my button after the user click?
The result I would like to have is that of the first photo.
I think the call you are looking for is:
btn.setRequestFocusEnabled(false);
this will stop your buttons from taking over the focus when the mouse clicks them, achieving the goal of not having the keyboard focus painted for mouse users. However it still lets keyboard-centric users "tab over" to them.
If one of your buttons gets the initial focus, however, then the keyboard focus will still render there. In that case, you probably want to select some other sensible component to place the initial keyboard focus onto by calling:
otherComponent.requestFocusInWindow();
Are you talking about the input Focus? (the dotted rectangle a component shows when it has the focus)
You can explicitly disable a component getting the focus by calling setFocusable(false) on the component (e.g. every button). It does not prevent you from clicking on the buttons, but it does prevent you from cycling the focus with the tab-key to it.

show one panel after button is clicked and other panel when second button is clicked in the same frame

I am creating a Swing based application , which actually consist of two buttons as shown below -
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
and when second button is clicked it will show another panel in the same frame as shown below ..
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
There are 2 approaches.
CardLayout based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container with CardLayout (the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();

Java JFrame displaying initialisation content when losing focus

My application uses the BorderLayout to display content in the frame. I also have a menubar at the top so other frames can be toggled to show. When I launch the application, each of these content areas of the frame are given a JPanel with the centre loaded with a 'startup screen' and once some background initialisation has been completed, the panel is then change to start displaying information post-initialisation. This panel is then updated 5 times every second, controlled by a loop.
When I select the menubar the display of the centre panel reverts back to that of startup screen. When I deselect the menubar the panel reverts to that of the correct one to be displayed. Debugging this leads to the entire Eclipse environment crashing.
Does anyone know what may be causing this? I'm not sure what code to show here so if you require more information I will edit this as such.
1.have look at Initial Thread, be sure that GUI is created especially visible wrapped into invokeLater()
2.last code lines in the Swing GUI constructor would be
code
myFrame.pack();
myFrame.setVisible(true);
}
3.for better help sooner post an SSCCE

JFrame in a Java desktop application

I am developing a desktop Java application with GUI implemented through Swing. I have made a JFrame and have added three buttons on it - Add, Edit, Delete.
Now I want that whenever a user clicks on any of the button, the content specific to that button appears besides those three buttons.
So how to implement this? Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPanel?
So far, I have taken a JFrame and have added 3 buttons on it. That's it.
For the Add button, I want to add some buttons and textfields to add information to the database.
For the Delete button, I want to add some buttons to find records in the database based on the information entered through the user in the textfield that appears when the user clicks on the Delete button.
Similar type of content for Edit button.
So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPane
That would be fine. When you push the button, you can call JPanel.removeAll() to remove all the controls currently in the control, and then just do the layout again, specific to whatever button you pushed.
If you have custom swing controls, just add your custom control the JPanel using a BorderLayout and putting in the center.
Another option would be to use a CardLayout, and flipping between the cards when a user presses one of the buttons. If the layouts for the buttons never change, that would probably be a better way to do it. Obviously if the content changes between button presses, you'll need to redo the layout each time.
Either of Chad's or Alex's answers would be fine. You will probably need to call a combination of revalidate() and repaint() on the panel that you've changed, as in the past I've noticed Swing doesn't always like panels being swapped out.
Also, have you considered using a JTabbedPane instead of manually coding the interaction with the add/edit/delete buttons?
I haven't done a lot of Java programming, but I think using 2-3 different JPanel, and make visible the one you need depending on the button that was clicked would do the trick.
I'm not sure if this is the right approach though.
I was using a JFrame to add all buttons and make a new JFrame for a new window and hide a previous one.
gven way are better. I will do that now.

Categories

Resources