I'm wondering how to change screens in a JFrame. For example, changing from the starting screen to a different screen. So you have an assortment of buttons, labels, trees, etc on one screen, as the user clicks a button a different layout appears.
Would the 'setVisible(false) and setVisible(true)' do the trick?
You've got it! Create separate JFrame instances for each of your frames:
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
//populate your frames with stuff
frame1.setVisible(false);
frame2.setVisible(true);
On a side note, you'll want to make sure to use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) on any secondary frames to prevent your application from terminating if a user closes a secondary frame.
All that being said, you can also use multiple JPanel instances inside of the same JFrame instead of creating multiple JFrame instances. This way, all the action of your application will take place in one window.
I would strongly recommend giving this a read through: http://docs.oracle.com/javase/tutorial/uiswing/
Related
I am working on a Java desktop application using javax.swing.
I want make my application border-less and at the same time resizeable.
When I remove the border using the frame.setUndecorated(true); method I can no longer resize the frame using mouse clicks and drags.
How can I hide the border of the frame and still let the user resize it?
Frames allow a user to interact with the JFrame using their mouse. If you remove the frame then you cannot use it to move or resize the frame. You can reimplement this functionality yourself (not sure why you'd want to but of course experimenting is always fun!).
First you must somehow get user mouse events. By creating a custom JComponent, maybe called ResizeGrip, and placing it in the bottom right of your frame you can visually show your user that they can still resize the frame. You can then implement a MouseListener to see when the user has clicked and dragged your ResizeGrip.
Then you need to turn those event detections into instructions to resize the frame pragmatically, that is via someJFrame.setSize(newWidth, newHeight);. You will also need to do something similar if you want to move the frame.
Check out Resizing Component for a general purpose class that will allow you to resize any component.
The class is a MouseListener that installs itself on the components you specify.
The basic code to add it to your frame would be:
JFrame frame = new JFrame("SSCCE");
frame.setUndecorated(true);
ComponentResizer cr = new ComponentResizer();
cr.registerComponent(frame);
You can control which sides can be dragged by specifying the "drag insets". The default value is 5 for all sides which means you can resize any size. You could limit the frame to be resized only horizontally by using:
cr.setDragInsets( new Insets(0, 0, 0, 5) );
I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.
I completed my project which assigned to me by university but now I am trying to create MDI for my project. I used 10 jFrame and one main form which is also jFrame, after that I add one Menu Bar, 10 jButtons for calling jFrame and one jDesktopPane for place calling jFrame. The below code using for calling jFrame place into jDesktopPane in all 10 jButton:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
asd t = new asd();
dskp.add(t);
t.setVisible(true);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
but not working with me and giving below error message:
java.lang.illegalargumentexception: adding a window to a container
How to do this and solve this issue because I didn't used any jInternal Frame. I think at this I am not able to use jInternale Frame because I did all work on jFrame such as full GUI with code and re-doing all work on jInternal Frame its not possible for me coz of short of time to submitting my final project.
If you're desiring to placing windows intp a JDesktopPane, then you need to use JInternalFrames. This is your best solution whether it is appealing to you or not.
A lesson in this is that you should strive to avoid creating classes that extend Swing components, especially top-level components such as JFrames, and instead create classes that produce JPanels, components that are flexible enough to be placed anywhere such as into JFrames, JInternalFrames, JDialogs, JOptionPanes, other JPanels, etc...
Note that a kludge is to get the contentPane from your JFrame, put it into a JInternalFrame and put that into the JDesktopPane, either that or set the JInternalPanes's contentPane with that from the JFrame. i.e.,
asd t = new asd();
JInternalFrame internalFrame = new JInternalFrame();
internalFrame.setContentPane(t.getContentPane());
internalFrame.pack();
// set the internalFrame's location
// ...
internalFrame.setVisible(true);
dskp.add(internalFrame);
But again note that this is a kludge and carries potential traps.
I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?
Use a JInternalFrame
Make the JFrame box a JPanel box.
Your application should only have one JFrame.
JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.
In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.
use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help
I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}