I have designed a GUI using Swing components. I have added a Next Button and when someone press it I want to appear another layout of the same GUI. Something like when I pressed proceed in this site in order to write the question.
Sounds like you will want to use a CardLayout. You can define multiple panels to occupy the same space in the frame. You can swap the panel that is displayed based on your requirements.
So based on your question, the "Next" button would just display the next panel added to the CardLayout.
Read the section from the swing tutorial on How to Use CardLayout for more information and working examples.
Related
I have developed a simple music player in Java which can play any given playlist or simple mp3 song.
For now i have worked all things out in plain JPanels. GUI doesn't look neat.
I need to revamp the GUI using tabbed pane. How this can be achieved using existing JPanels without affecting current functionality?
Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
Because a tabbed pane provides its own GUI, using a tabbed pane is simpler than using the CardLayout class.
"Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?"
It really depends on your preference of the look of your program. The two layouts perform very similarly, though CardLayout is a little bit more code, though at all not difficult. If you don't want the tab look, which I don't see why you would, for a game, then go with CardLayout
"How this can be achieved using existing JPanels without affecting current functionality? "
You need to create separate JPanel for each containment of whatever components you want in each tab. Then just add those JPanel to the JTabbedPane. It shouldn't break any functionality, just the look. Components from another panel should not be affected, you just won't be able to see any changes made, unless that other panel is in view.
If you want to go with CardLayout you can look at the tutorial
I have something like this...
Its a bunch of JLabels within JPanels inside a
--JFrame
--JPanel
--Set<JPanel>
--JLabel object contained in each JPanel object cotained in the set
I want to create an external panel/frame so that each time I hover over each individual JPanel, a new frame/panel pops up giving me some data. This will essentially overlap over the JFrame.
Its pretty brief but I just need some guidance as to what I need to look up.
Maybe you can just use JToolTip. When the mouse hovers over the label for a couple of seconds it will display automatically. See the section from the Swing tutorial on How to Use Tool Tips.
Or if you want something more complicated use a MouseListener. On mouseEntered() you can display a JPopupMenu or an undecorated JDialog.
You can start by reading the Swing tutorial on How to Write a Mouse Listener.
I want to put one toolbar type component in which there will be one button which when pressed the toolbar should expand, size of the form increases and it contains some components like text area and label that user can see and once again when the button is pressed the toolbar is minimized and the size of the form decreases.
Can anybody tell me how to accomplish this?
e.g; like in windows 7 in my computer there is one toolbar called "Hard Disk Drives". When we press on it it shows all the drives and when we again press it it hides it.
SwingX has a collapsible panel control that I think should do what you want.
Also take a look at How to have Collapsable/Expandable JPanel in Java Swing.
Some alternatives:
You can add a JButon to a JToolBar. In your button's ActionListener, open a JOptionPane containing your components. This example illustrates some possibilities.
For a more advanced view, consider Outline, illustrated here.
I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help
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.