Laying out JPanels to make a simple GUI - java

first of all, this is more or less my first GUI and ive learned Java for not more then a week, so it might contain some serious programming errors.
What i have right now is:
Buttons and labels are part of OptionPanel and are on the left, DrawingPanel is about 5x5 px in size and is on the right.
What I am trying to do is a simple test, to get me more familiar with the GUI. The rectangle should be movable and re-sizeable by the user when clicking the respective buttons:
http://www.upload.ee/image/612005/JFrame2.jpg
Right now i have:
JFrame MainFrame - Makes JFrame (Not using the setSize function. using .pack() instead. not sure about it)
JPanel MergedPanel - FlowLayout - Adds JPanel OptionsPanel and JPanel DrawingPanel together and gets injected to JFrame MainFrame
JPanel DrawPanel - This JPanel is responsible of drawing the rectangle.
JPanel OptionPanel - FlowLayout - This JPanel is responsible of the buttons.
Help please.

You should never call setSize() in your code. In Java, you use layout managers to do the layout (read that tutorial).
Subclassing JPanel to implement different parts of which the UI is composed is a good practice, but should not be overdone (it's fine to have an UI class that adds 3 other plain JPanel instances to itself for layout purposes).

Check out MiG Layout : one can make pretty easily java layouts with that.

Related

Do I need JPanel always?

I am now writing code simple GUI that's for start the game window. I only need Do you want to start game message and start button on the window. But I have a confusing concepts for the JFrame and JPanel. Actually, I thought I need to add JPanel to JFrame to add the other components such as JLabel, JButton,...etc. But I realized I don't actually need JPanel. I can just add the components simply use add(button), add(label) to JFrame. So why I need JPanel. And I think JFrame doesn't need JPanel but JPanel need JFrame. Am I understand correctly?
No, not always. A simple graphical user interface may be implemented by just adding components "directly" to a JFrame. But in order to get more flexibility, you would always use JPanels. For example, to employ different layouts in different parts of the GUI, to group certain components together, etc.
A JFrame is backed by a JRootPane, a part of which is a contentPane.
(image from Oracle Javadoc)
When you add components to a JFrame, you are really adding them to the content pane, e.g.: frame.getContentPane().add(Component).
A JFrame is a common starting scene of a Swing GUI application, while a JPanel is intended to be put in another scene (container). Since both content pane and a JPanel inherit from the same class (Container) you may use them in a similar manner, as far as adding components to them goes.
Do I need JPanel always?
No. Well, unless you need a Swing GUI. Then yes.
Another answer replied words to the effect. "No, you can add components direct to a frame" What they missed was that components added to a JFrame are added to the content pane (automatically). The content pane is a JPanel.
Having said that:
I (and many others) would recommend designing an app based around a main content panel, then adding that panel to a top-level container as needed. The top level container might be a JFrame, JWindow, JDialog, JOptionPane ..
What prompted the question? A JPanel is a very 'light weight' container (in more ways than one). A GUI can contain 1000s and not be burdened by doing so. Of course, that's a rare requirement, but just saying .. use panels as needed and don't worry about it.

Is it possible to have two gridbag layouts side-by-side inside a frame?

I'm using an extended JFrame class, called MFrame, to initialize two objects that extend JPanel. One JPanel has buttons, called ButtonPanel, and the other has a tree generated via reading in XML file, called TreePanel. I'd like to have them in two separate classes because I'll be adding a lot of functionality and I want to have them be as minimal as possible.
Inside both ButtonPane and TreePanel, gridbag is used to establish layout. I'm trying to display both within the same JFrame, but whichever one is added last, via ex. this.add(ButtonPanel), covers up the JPanel class behind it.
Has anyone been able to display two JPanel classes with Gridbag side-by-side within a JFrame? Any help would be appreciated.
Here is how:
Create a MPanel and add it to MFrame
Set its Layout to BoxLayout (horizontal in your case)
Add the TreePanel and ButtonPanel MPanel
You have to add a Panel because the default for JFrame is BorderLayout so add(...) is essentialy the same as add(..., BorderLayout.CENTER) thus only the last componet you add is visible.

Adding content to a JPanel

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.

Why does only the last JPanel I add to a JFrame resize?

So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas

How do I add a bunch of JPanels to my JFrame using Java Swing?

I generate a bunch of JPanels and then pass them into a class that extends JFrame. How do I add an indefinite number of JPanels to this JFrame. I was also reading about JScrollPane should I incorporate this somehow into the design?
Example Code:
class foo extends JPanel
{
//generate JPanels
}
class bar extends JFrame
{
//grab some amount of foo classes and put them into this JFrame and show it
}
Also is there anything I need to watch out for when showing this JFrame?
Thanks
How do I add an indefinite number of JPanels to this JFrame?
CardLayout, JDesktopPane/JInternalFrame, JTabbedPane, JScrollPane - there are a number of options.
Also is there anything I need to watch out for when showing this JFrame?
(shrugs)
Construct and show GUI components on the EDT.
pack() the GUI before setting the position and calling setVisible(true).
Don't rely on the default layouts of content panes.
Don't implement custom painting in a top level container.
..
JFrame -> JScrollPane -> fathers JPanel then you'll decide which of LayoutManager will lay your bunch of JPanels, by defalut FlowLayout, don't forget to play with PreferedSize for childsPanels

Categories