Swing behavior on splitPane - java

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPane , scrollPane2);
I want to do very trivial action and I it doesn't work.
Whenever I add label to either of scrollPanes - they simply don't display anything. I can display tables within panes but these are added through the constructor. Pisses a lot.
Also if splitPane only allows two components - what if I want to separate window into four parts? SplitPane of SplitPane?
Thanks for help,

You might compare your implementation to this example that adds a JLabel instance to a JPanel on each side of the JSplitPane.
Although JSplitPane only admits two components, you can add another JSplitPane to either pane in order to subdivide either side in either direction, ad libitum.

Don't forget to call updateUI() after you've added your components. JSplitPane is really only supposed to be used for splitting into two components. If you require more, consider using a TableView.

Related

SIze of Jpanel inside JSplitPanel

I was trying to add JsplitPane into my project.Requirement is i need to add two Jpanel inside right panel of JSplitPane.
so what i had done is first add Jpanel say it panel1 to right panel and set BoxLayout.Y-AXIS and than add two panel inside panel1.
now in that two panel first panel have BoxLayout and i want the width of this panel to be of size of panel1 but i am not able to do it.
anyone have idea how to do it?
I was trying to add JsplitPane into my project.Requirement is i need
to add two Jpanel inside right panel of JSplitPane.
When using JSplitPane, we should remember that it only divides the pane into two components say Left and Right or Top and Bottom. So when we again want to add more than one components in a single side of that JSplitPane, say in our case Right, it is better to use Nesting Split Panes. That means creating Split Panes inside Split Panes.
so what i had done is first add Jpanel say it panel1 to right panel
and set BoxLayout.Y-AXIS and than add two panel inside panel1.
now in that two panel first panel have BoxLayout and i want the width
of this panel to be of size of panel1 but i am not able to do it.
If you use Nesting Split Panes, you may not have to create an extra Parent JPanel what you said as panel1. Actually the Split Pane is used to divide the Pane into two segments. So, by using Nesting Split Panes you are creating another Split Pane in stead of what you were creating as panel1 and then put your two child panels inside the two Panes created by new JSplitPane which is nested. So, you don't have to think about the size issue, too. I hope I could make you clear and it solved your issue.
A simple way to achieve that by using:
Declaration:
private JSplitPane jSplitPane1;
private JSplitPane jSplitPane2;
private JPanel jPanel1;
private JPanel jPanel2;
In Constructor:
jSplitPane1 = new JSplitPane();
jSplitPane2 = new JSplitPane();
jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
jSplitPane1.setRightComponent(jSplitPane2);
jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
jSplitPane1.setTopComponent(jPanel1);
jSplitPane1.setBottomComponent(jPanel2);
The above described method is the simplest to achieve what you wanted. However, without nesting the Split Pane, it is possible to use Multi Split Panes which may not be so handy. Still you can have a look at this old article at Oracle:
https://community.oracle.com/docs/DOC-983539

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.

Make buttons unresizable

So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.

Placing components on Jpanel

Can i combine Java layouts in same JPanel. I'm stuck with with placing my components on JPanel. It shoudl be like this: JLabel, JButton, JButton , JLabel and new line and same. I used BorderLayout but it wont go to the next row, keep adding components to same row and I need a new row. Ideal sit combined with cardlayout or some other good solution.
EDIT: Solved with GridLayout (0,4) It will do the job till i learn to use GridBaglayout. Thank you for trying to help me.
Yes you can combine layouts.
Using a JPanel you are able to embed other JPanels:
JPanel back = new JPanel(new BorderLayout());
JPanel rows = new JPabel(new GridLayout(3,3));
back.add(rows, BorderLayout.CENTER);
Without seeing your code though it's difficult to know exactly what you are trying to achieve!
Yes you can combine java layouts.
A common pattern I use is BorderLayout first on a frame. The central component expands out, while the other components shrink in. Inside these panels I might have a Flowlayout to show buttons evenly spaced horizontally on top.
Another common approach for forms is using a Gridbaglayout, then adding all the form elements at gridX and gridY positions. I then later can stretch and teak these cells using other constraints in the Gridbaglayout repetoire.
Can you add a screenshot so that we can see what you want to do?

Java multiple GUI windows creation

I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().

Categories