I'm creating a GUI for a project but I'm a newbie of swing so I obviously got problems:
My AppFrame is formed by:
JFrame --> ContentPane --> SplitPane
The splitpane left component is used for a menu, the right component should host differents JPane according to the menu's button.
I press the "Products" button? I wish to see MyProductsJPanel and so on.
The splitpane is created and added in the Jframe's constructor, how I can access and modify it at runtime?
Is a wrong layout?
PS: I use WindowBuilder plugin for eclipse, wich generates most part of the code of course.
Ok, thanks to #MadProgrammer contribution I'm able to give a solution:
In the JFrame constructor I added a listener for every button of the menu.
In the listener:
public MouseClicked(MouseEvent arg0){
splitPane.remove(splitPane.getRightComponent()); //Useless?
mySplitPane.setRightComponent(new OneOfMyButtonRelatedPane());
}
Works and solve my question
Related
Whenever I run my application, Java automatically sets focus on the first component on the frame.
Here's a screenshot of the problem:
As you can see, Java has set focus on the first button (Add a new word).
I've tried this solution but the problem is not solved :(
Mainframe mainframe = new Mainframe();
mainframe.requestFocusInWindow();
mainframe.setVisible(true);
Now, How can I disable auto-focus in my application?
You can work around this by setting your initial focus to the JFrame's Content Pane. Just add following code to the JFrame after you add all other elements to your JFrame.
getContentPane().requestFocusInWindow();
So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.
I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.
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!
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.