this title may not best describe my problem. I'm using Netbean GUI builder to create a JFrame and several JPanels. I create each JPanel in a seperate class, then I drag the JPanel class to JFrame. The problem is after dragging the JPanel to JFrame, if I add components to JPanel, it does not show the additional components in the JPanel contained in JFrame. I tried "clean and build" but the new component still not showing in JFrame -> JPanel.
Matisse keeps a cached copy of any component you add to the palette. So, subsequent changes are not automatically picked up. To pick up the changes:
save and close your JFrame source
Make sure the JPanel component is compiled.
Click Tools > Palette > Swing/AWT Components
Right click on the tree and choose Refresh
Open your JFrame component. It should now be showing the updated components
I know what you mean; Matisse (NetBeans' GUI editor) lets you drag and drop self-assembled Containers (e.g. JPanels) into other Containers (e.g. JFrames), but any subsequent changes in the former will not be reflected in the latter. As far as I know, that's just the way it works, unfortunately.
It's one of the reasons I think Matisse can only be used for quick prototyping and toying around. For anything serious, the GUI must be hand-coded.
Had the same problem, all you have to do is:
1) re-compile the new JPanel subcomponent
2) close the form associated with the parent JPanel
3) re-open the parent Jpanel again from the Project tree
The refreshed subpanel should now be shown. Matisse only renders the subpanel when you either open a form or add the new subpanel. So, the only way to refresh the display without removing and re-adding the subpanel is to close and reopen it.
Related
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 would like to add components (JButton and JSpinner) to a JPanel which was created using Netbeans GUI builder. This panel uses GroupLayout, and I can't seem to use add() to add a component. Is there any way to either add something to a panel which has a GroupLayout, or change this panel to a FlowLayout?
It's definitely not an easy thing to do. It depends on where you want to add those components. I usually reserve an empty placeholder JPanel with GUI builder, and then add components to that panel, using whatever layout I want. However, this only works when you want to add components in one place. If they are scattered around the GUI, it may be not that easy.
Another options is to migrate to manual GUI creation, possibly using Netbeans-generated code as a starting point. But depending on how complex your GUI is, it may be a tedious work.
There is an option if you right-click the JPanel to change it to a FlowLayout (Set Layout). This fixed all the problems I was having.
I have a JTabbedPane (say myTabPane) having one tab (lets take only one tab for clarity sake). While creating the JTabbedPane, I added a JPanel (say panel_A) to this tab. I have a button on this JPanel. The tab displays my JPanel perfectly with the button on it. So far so good.
I have defined a listener on the button which creates an instance (say panel_B) of another class extending JPanel. This JPanel has got a different set of components on it. I want panel_B to super-impose panel_A. That is, JTabbedPane's tab should show panel_B and hide panel_A.
Please note that I am able to display panel_A OR panel_B when I "bind" the respective panel (one of them) to the tab during creation of the JTabbedPane. However, I want a selective display (or binding, whichever is possible) of only one of the panels with a button-click (ie. at runtime).
How can this be achieved?
Thank you!
This will do what you need:
myTabPane.removeTabAt(0);
myTabPane.addTab("B", panelB);
i'm using Windowbuilder in Eclipse indigo.
I have a main JPanel, and I want to add a new Panel which will popup when I click a button.
How can I see the new Panel insted of the main panel in the Design frame?
thanks!
There should be a window that is part of the Structure View called "Components". Click on the new JPanel from there. I did this a while ago but I do not currently have windowbuilder installed so I am not 100% positive. I will confirm it when I can.
Edit: Ok i took a look at it. Here's how it works. If you made a new Application Window, make sure the palette view is open (Window->Show View->Palette). Now I am pretty sure the best way to switch between panels is by using a card layout. So drag the card layout from the palette to the window. Now if you drag JPanels from the palette to getContentPane() in the components window, you can switch between those panels with buttons and such.
Each panel should be created as its own subclass of JPanel in its own file. It is a very bad idea to use inner classes for something like this. WB intentionally does not support that style.
I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful
Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.
you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.
The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!