I'm doing a simple java application that essentially shows a certain amount of letters (ABCDE etc) from an array, each one displayer in a portion of a grid. There are two buttons, one that will shift the letters to the left (so that one shift will become BCDEA and the right shift will go EABCD).
I've got the shifting and everything else working, as I've tested using a System output. But how do I get the window to refresh and show me the updated JLabels? They stay the same (ABCDE) after I shift them.
I've tried revalidate() and repaint() both inside the buttons' ActionListeners and on the shift method that they call, but nothing happens. Any tips on this?
I've tried revalidate() and repaint()
You only use those methods when you create a new component and add the component to a visible GUI. So it sounds like you are trying to remove/add the labels in the new order you want the labels to be displayed.
Maybe an easier approach is to leave the label in the same order but just change the text on each label. Then all you need to do is
label.setText();
and the label will repaint itself automatically without you invoking revalidate() and repaint().
Related
I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.
So for the sake of simplicity I set up a little test code just to figure out this problem. Basically I have a JFrame and I added 'this' to it (I just extended my main class from JComponent to save time). this component fills in a red background. Then I have it sleep for 2 seconds and then type this.
f.remove(this);
thing t = new thing();
f.add(t);
f.setVisible(true);
f being my JFrame object and 'thing' is just another class extending JComponent that paints a blue background..
when I comment out setvisible() it no longer changes to blue.. I've tried using t.setVisible(true) but it seems I have to make the frame visible again, not the component
does anyone know why I have to call that... or if there is another way to change components within a single frame?
"Basically I have a JFrame and I added 'this' to it (I just extended my main class from JComponent to save time). this component fills in a red background. Then I have it sleep for 2 seconds and then type this."
Don't "sleep" your program. Instead use a java.swing.Timer to perform repeated tasks on the GUI or for animation. See more at How to Use Swing Timers. You can see a bunch of Timer examples here and here and here and here and here
Instead of trying to add and remove panels look into using a CardLayout which allows you to switch between views. It will help you avoid a lot of problems that come with with adding and removing components/containers. See more at How to Use CardLayout. Also see a simple example here.
To answer your main question, whenever you remove and add components from your frame, you need to revalidate() it. setVisible() takes care of that for you.
Side Note
Seems like a lot adding an removing background panels) just to change the background. Why not just setBackround()? You can switch colors with the use of the Timer
Calling setVisible(true) makes the frame appear onscreen. Sometimes you might see the show method used instead. The two usages are equivalent, but we use setVisible(true) for consistency's sake.
I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information
I am trying to make a slideshow-like application in Java using NetBeans.
I have a JFrame (the main window) and inside it are two buttons (back and next) for navigation, and a JPanel (mainPanel) which acts as holder of the 'slides' of the app. I am also using custom JPanel (SlidePanel) objects which are then placed inside the mainPanel. Some of these SlidePanel objects have JPanels within them.
Now, I've been able to place these SlidePanel objects inside the mainPanel and also navigate through them (for example, if I click the next button, SlidePanel 1 would be replaced by SlidePanel 2, or if I click the back button, SlidePanel 2 would be replaced by SlidePanel 1.) I've done this by using the removeAll(), revalidate(), and repaint() methods.
The problem starts here. The third SlidePanel object contains a JPanel inside it. What happens when you get to the third SlidePanel, it works fine. But from there, if you click the back button (which takes you back to SlidePanel 2), the JPanel from SlidePanel 3 remains on the screen, with the components inside SlidePanel 2 adjusted to fit the mainPanel.
What should I do to fix this?
EDIT: Thank you very much to all those who answered. I tried CardLayout on a dummy project and it worked fine. Now, all I have to do is add it to the original one. Thanks again. :)
I suggest to use CardLayout for that, rather as add and remove JPanels on the Runtime example for CardLayout and together with your idea could be here
I suggest you use a different layout manager (e.g. CardLayout); let the layout manager worry about juggling it's components.
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.