i'm new to swing. i've created a jpanel form using the swing palette design. It has jlabels, jtextfields and a jbutton. when the button is clicked, the data entered in the jtextfields is inserted in the database through the model.
i don't want the the jpanel to be always displayed so i would like to add another button that displays this jpanel form when i click on it. is it possible to perform this using the palette design knowing that the source code is locked in the initComponent private function, or do i have to re-code all the components and layout myself?
thanks for your time!
"knowing that the source code is locked in the initComponent private function"
You can edit the auto-generated source code, as see here in this answer.
"that displays this jpanel form when i click on it. is it possible to perform this using the palette design"
Sounds like you want to swap views. You can do that using CardLayout and yes, it can be done from the design view. You can see more at How to Use CardLayout with Netbeans GUI Builder
Related
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.
i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes
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 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.
Can anyone tell me on how the swing components drawn from the palette and put into the Jfame can work together? For example If a button pressed can the result show on the textArea, Or Can I drag some listed objects from the Jlist in my JFrame onto the JtextArea/JTabpanel?
Thank you!
You will need to add an ActionListener to your button and in the actionPerformed method you can write the code to display a result in your JTextArea:
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
More info on buttons:
http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
You can drag objects from a JList to another component (such as a JTextArea or JTable), but you would probably not drag them directly onto a JTabbedPane. You'll have to code this behavior with the DnD API:
http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html
Rob has a pretty good answer.
Here's my 2 cents,
"For example If a button pressed can the result show on the textArea"
If you are using Netbeans, this is pretty quick and easy to do:
Double click your button.
The IDE will jump to the actionPerformed method. (and put in the required boiler-plate code)
Place your code in the generated actionPerfored method
e.g "jTextArea1.setText("hello world");"
I'd advise reading the swing tutorial on the sun website:
http://java.sun.com/docs/books/tutorial/uiswing/