Make buttons split JPanel - Java Swing - java

I've got a BorderLayout going on, and am working on the North panel. Inside the North panel, I'd like to have 3 components: a picture that is on the left, and two buttons that split the remaining width of the Frame. Right now I'm attempting to accomplish this with another BorderLayout.
The Frame is resizable.
The picture is assigned to BorderLayout.WEST, and with the following code I attempt to add another panel that contains only buttons. The panel is then added to the CENTER of the Frame's NORTH layout component.
//create panel to hold buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BorderLayout());
JButton btnMatrix = new JButton("Matrix View");
btnPanel.add(btnMatrix);
JButton btnList = new JButton("List View");
btnPanel.add(btnList);
add(btnPanel);
however, the buttons both try to take up the entire panel. If I leave it to a flow layout (I don't use btnPanel.setLayout(new BorderLayout()); in the above code), the buttons sit nicely in the center, but do not expand and share the btnPanel.
Thoughts? I'm new enough to Java that I could be going about this the wrong way from the start.

btnPanel.setLayout(new BorderLayout());
You didn't specify a constraint when you added the buttons to the panel. So both buttons are added to the CENTER. However, only one component can be added to the CENTER, so only the last one added is displayed.
You can try a different layout:
btnPanel.setLayout( new GridLayout(0, 2));
Then each button will be the same size and both buttons will fill the space available.

Related

How can I change the size of a Panel in Java?

I'm trying to create a simple login screen consisting of different panels. I want help in resizing panels. The Panel I want resized is coloured in Green. I want to make it a bit smaller. The Panel in Green is the North Panel and is set to Border Layout. I want to make the green panel smaller since i feel its too big
I tried northPanel.setSize(150,150); but I got no result
This is my code:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
You are using a BorderLayout which has five positions to put components in the layout. The five positions are PAGE_START, PAGE_END, LINE_START, LINE_END, and CENTER. Here is a diagram of these positions:
For this layout, you want to put the component that should take the remaining space of the frame in the CENTER position. For this reason, the northPanel is probably better suited in the PAGE_START position while the JPanel that houses the login labels and submit button should be in the CENTER position. Using this positioning will allow you to resize the northPanel and allow the panel housing the login labels and submit button to take up the remaining frame space.
I want to make the green panel smaller
Looks to me like the two panels are the same size which tells me you are using a GridLayout for your frame.
Don't use a GridLayout, instead keep the default BorderLayout of the frame.
Then your code would be something like:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
Now the green panel will only be as big as the JLabel. If you want the panel to be bigger, then add an EmptyBorder to the northPanel. Read the section from the Swing tutorial on How to Use Borders for more information.

JFrame doesn't show the Jpanel's right

I'm using a JFrame with the size of 800x600.
what i'm trying to do is make this:
the black Panel has 2 other panels inside of him with the size of 300x300 each.
the result is that the black panel is to the left (as suposed) and the red panel in in the centre with a gap on top between the frame and the panel. also, if i remove the black panel the right panel is filling the whole frame...
this is the code:
//create the left part of the screen
JPanel leftPanels = new JPanel();
leftPanels.setLayout(new GridLayout(2,1));
leftPanels.setSize(new Dimension(300,600));
// just to illustrate the 2 panels inside of the black panel.
//leftPanels.add(new JPanel());
//leftPanels.add(new JPanel());
//create the right part
JPanel rightPanel = new JPanel();
rightPanel.setSize(new Dimension(500,600));
rightPanel.setBackground(Color.red);
this.add(leftPanels);
this.add(rightPanel);
this.validate();
this.repaint();
is there an easy way to fix this?
I also tried a Gridlayout on the JFrame but that gives me 2 panels of 400X600 each
First, use FlowLayout like this
setLayout(new FlowLayout(FlowLayout.LEFT));
Secondly, kindly use setPreferedSize rather than setSize for the JPanels
leftPanels.setPreferredSize(new Dimension(300,600));
I don't know what is cashRegister, but it looks like you are not adding the rightPanel to JFrame so make sure you add it.
Try to set the layout of the frame to null. Then use setBounds to position the panel.
If you are trying to set the panel relatively one from another set the frame layout to null
this.getContentPane().setLayout(null);
Then you will be able to place them absolutely.
For more info : Doing Without a Layout Manager (Absolute Positioning)

Filling a JPanel

I have a few items in a Jpanel which is then pushed to the top and used as a toolbar for a basic search engine. I'm having an issue where my last combobox isn't displaying as there isn't enough room. However, there's a lot of empty space on the left side and I need everything to move across to fill the JPanel so then this can display. So my question is how would I make these items start from the far left and go to right, thanks.
//Labels for combo boxes
JLabel Bookmarklbl = new JLabel("Bookmarks:");
JLabel Historylbl = new JLabel("History:");
FlowLayout flowLayout = new FlowLayout();
MainBrowser.toolBar.setLayout(flowLayout);
//Adding items to Panel
MainBrowser.toolBar.add(Bookmarklbl);
MainBrowser.toolBar.add(BookmarkList);
MainBrowser.toolBar.add(bookmarkbtn);
MainBrowser.toolBar.add(back);
MainBrowser.toolBar.add(forward);
MainBrowser.toolBar.add(MainBrowser.addressbar);
MainBrowser.toolBar.add(home);
MainBrowser.toolBar.add(reload);
MainBrowser.toolBar.add(Historylbl);
MainBrowser.toolBar.add(historyList);
//Set the things added from left to right
MainBrowser.main.setComponentOrientation(
ComponentOrientation.LEFT_TO_RIGHT);
//Add Panel to main frame
MainBrowser.main.add(MainBrowser.toolBar,BorderLayout.NORTH);
How the bar looks:http://postimg.org/image/l314iw6eh/
Assuming toolbar is JPanel and is using FlowLayout, this code might help you,
JPanel panel = new JPanel(); // your toolbar panel
FlowLayout flowLayout = (FlowLayout) panel.getLayout(); // flowlayout
flowLayout.setAlignment(FlowLayout.LEFT); // alignment to left
contentPane.add(panel, BorderLayout.NORTH); // adding this panel to original frame
Hope this helps
The default for a FlowLayout is CENTER. If there is not enough space to display all the components, then the components are wrapped to the next line. Changing the alignment to LEFT won't fix this problem (just the default alignment of components).
showing the combobox bar is very large is there anyway I can limit the width?
You can limit the width of the combo box by using:
comboBox.setPrototypeDisplayValue( "XXXXXXXXXX" );
This will limit the preferred size of the combo box so it can display on your toolbar.
However, you will still want to see the full text of the items when the popup is displayed. For this you can use the Combo Box Popup.
You can try to use BoxLayout, like:
toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).
Maybe this will work.
MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));
EDIT
Sorry it is MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

How to make scrollable to jPanel

I am making swing application. And there is too much height of my jPanel. So I want to make this panel as scrollable.:
Following is my description of my requirement.
I have four jpanel in one jpanel I mean:
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
I am adding p2, p3, p4 inside p1 like following output:
like above showing panel has more height than computer screen height. So I want to display all content of my panel on computer screen by scrolling.
I searched here and found the following questions:
How to make a JPanel scrollable?
How do i get vertical scrolling to JPanel?
However, the answers did not solve myproblem.
Without seeing your code, my guess is that you don't have a JScrollpane to provide the scrollable behaviour you want.
JPanel mainPanel = new JPanel(); //This would be the base panel of your UI
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel newPanel = new JPanel();
newPanel.add(p1);
newPanel.add(p2);
newPanel.add(p3);
newPanel.add(p4);
JScrollPane pane = new JScrollPane(newPanel);
mainPanel.add(pane);
Since you use NetBeans, add a JScrollpane from the palette in which you'll add a panel to contain the 4 others. I think you could also just add the 4 panel into the JScrollpane.
Add your panel to a JScrollPane. Assumed that you want vertical scrolling only:
JScrollPane scrollPane=new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
For fine-tuning the scroll amounts, you can optionally implement the Scrollable interface.
See also How to Use Scroll Panes (The Java Tutorial)
It is easy to design scroll pane using Netbeans IDE. Below given are the steps I followed to add a scroll pane:
1. In Netbeans GUI editor, select all panels which requires scroll pane using CTRL+left click
2. Right click on the hilighted panels, select the option 'Enclose in' -> Scroll Pane. This will add a scroll pane for the selected panels.
3. If there are other elements than Panel(say JTree), select all the elements ->Enclose in ->Panel. Then enlose the new parent panel to scroll pane
4. Make sure that 'Auto Resizing' is turned on for the selected parent panel(Right click on panel -> Auto resizing -> Tick both Horizontal and vertical)

Why is my panel not positioned correctly even after setting the boundaries?

I'm trying to make a simple GUI with radio buttons and I grouped them into one panel. I wanted it positioned on the leftmost side so I used the setBounds method. Whatever numbers I put on the parameters, the panel won't move. Are panels not affected by the setBounds method? Or is there another way to position my panel. Here's the snippet of my code:
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3,1));
JRadioButton Rbutton1 = new JRadioButton("Credit Card");
JRadioButton Rbutton2 = new JRadioButton("E-Funds");
JRadioButton Rbutton3 = new JRadioButton("Check");
Rbutton3.setSelected(true);
ButtonGroup Bgroup = new ButtonGroup();
Bgroup.add(Rbutton1);
Bgroup.add(Rbutton2);
Bgroup.add(Rbutton3);
radioPanel.add(Rbutton1);
radioPanel.add(Rbutton2);
radioPanel.add(Rbutton3);
radioPanel.setBounds(10,50,50,40); //this is where I'm trying to position the panel with the radio buttons
paymentPanel.add(radioPanel);
contentPane.add(paymentPanel); //contentPane is the frame
contentPane.setVisible(true);
Set layout for the frame. For example:
contentPane.setLayout(new BorderLayout());
contentPane.add(paymentPanel, BorderLayout.LINE_START);
More info about layout managers you can find here: A Visual Guide to Layout Managers
You should read about Layout Managers which will do this for you. And I would suggest using a GUI Builder Tool, but that might not be allowed for your homework.
You can set layout as null layout for your contentPane.
contentPane.setLayout(null);
Then your setBounds() will work exactly as you designed.
Note:
Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Categories