Add multiple jlabel on jpanel and on same line using box layout - java

Want to add two jlabel with some space on same line to jpanel, japnel layout is set to box layout,due to some constraints i can't change layout to another and property of box layout from Y_AXIS to LINE_AXIS, so please provide me with some solution, so i can put jlabel on same line..
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
So please let me know the solution for the same mentioned above.

Wrap your labels in a JPanel with a Border layout. Add one to the West panel and another to the East panel. Set the alignment of the JLabels as needed. Then add the JPanel to your box layout.

It looks like you believe you can't change the layout because you're dealing with the content pane of a JFrame and you don't want to change the rest of the window.
If that's the case, you can use nested layouts by adding the two JLabels to a separate JPanel (let's call it labelPanel) and adding that to the content pane. It would look something like this:
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
labelPanel.add(leftLabel);
labelPanel.add(Box.createGlue()); //creates space between the JLabels
labelPanel.add(rightLabel);
contentPane.add(labelPanel);

Try this out: JPanel with GridLayout, and JLabels left and right aligned. The frame is a Box still uses a box. What should interest you is the JPanel panel code. That's where I'm adding the labels. All you have to do is nest components and layouts
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TwoLabels extends JFrame{
public TwoLabels(){
Box box = Box.createVerticalBox();
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.setBorder(new LineBorder(Color.black));
JLabel label1 = new JLabel("Hello");
JLabel label2 = new JLabel("World");
label1.setHorizontalAlignment(JLabel.LEADING);
label2.setHorizontalAlignment(JLabel.TRAILING);
panel.add(label1);
panel.add(label2);
box.add(new JPanel(){
public Dimension getPreferredSize(){
return new Dimension(300, 300);
}
});
box.add(panel);
add(box);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new TwoLabels();
}
}

Related

java JPanel How to fixed sizes

I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only.
My code below is almost OK, except the green panel varies in size a little.
How do I do this?
Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel TopPanel = new JPanel();
TopPanel.setPreferredSize(new Dimension(80,150));
TopPanel.setVisible(true);
TopPanel.setBackground(Color.GREEN);
JPanel MainPanel = new JPanel();
MainPanel.setPreferredSize(new Dimension(80,750));
MainPanel.setVisible(true);
MainPanel.setOpaque(true);
MainPanel.setBackground(Color.YELLOW);
Panel.add(TopPanel);
Panel.add(MainPanel);
Your question didn't restrict the solution to a BoxLayout, so I am going to suggest a different layout manager.
I would attack this with a BorderLayout and put the green panel in the PAGE_START location. Then put the yellow panel in the CENTER location without a preferredSize call.
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
Here is an SSCCE example of the solution:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPad extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
JPanel green = new JPanel();
green.setPreferredSize(new Dimension(80, 150));
green.setBackground(Color.GREEN);
JPanel yellow = new JPanel();
yellow.setBackground(Color.YELLOW);
frame.getContentPane().add(green, BorderLayout.PAGE_START);
frame.getContentPane().add(yellow, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
If you make your Panel use BorderLayout instead of BoxLayout and put TopPanel in BorderLayout.NORTH and MainPanel in BorderLayout.CENTER, then they will both resize horizontally, but only the MainPanel will resize vertically.
See the BorderLayout documentation

How to align two JButtons to be right aligned?

So currently my program shows only one of the buttons in the bottom right hand of the GUI. But I want to show both buttons in the bottom right hand corner. Any ideas how to set both buttons to the right corner? Here is my code so far:
import javax.swing.*;
import java.awt.*;
public class Other extends JFrame{
private static final long serialVersionUID = 1L;
public Other() {
super("Buttons");
final Container mainPanel = getContentPane();
mainPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
JButton s = new JButton("first");
JButton l = new JButton("second");
buttonPanel.add(s,BorderLayout.LINE_END);
buttonPanel.add(l,BorderLayout.LINE_END); //<-- not working
mainPanel.add(inputPanel,BorderLayout.PAGE_START);
mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
Other o = new Other();
}
}
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
While the BorderLayout will only accept one component per layout area, FlowLayout will display as many as are added (within viewable bounds).
you can design GUI better and easy with Netbeans 7.1 .. you can align the swing components wherever you like and even make dependent on size of the frame ... you can get it here https://netbeans.org/downloads/index.html

Layout Manager preferredSize Java

I'm still trying to learn how layout managers work. I made a Frame with two JPanels.
The first one contains a textArea with a boxLayout.
The second one contains a flow layout with a button.
I set the preferredSize of each panel accordingly, packed them, but got unexpected results.
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
TableBasic frame = new TableBasic();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setVisible(true);
frame.getContentPane().setLayout(new GridLayout(2,1));
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,20));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.getContentPane().add(controlPane, BorderLayout.NORTH);
frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
frame.setSize(new Dimension(500,500));
frame.pack();
}
}
Whatever I do, if I use a grid Layout, it seems to always allocate half of the available space to each control. I have been told that:
The height of each row is dependent on the height of each component
added in each row.
The buttonpane's height is 20. It's allocating much more than that to it:
What's wrong with this code?
I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).
That's the result of using GridLayout as layout manager. Change it to BorderLayout:
frame.getContentPane().setLayout(new BorderLayout());
For example, this code (I changed a little as possible from the original):
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//frame.setVisible(true);
//frame.getContentPane().setLayout(new BorderLayout());
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,40));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.add(controlPane, BorderLayout.NORTH);
frame.add(buttonPane, BorderLayout.SOUTH);
//frame.setSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
Generates this frame:
I set the preferredSize of each panel accordingly,
That is another problem. You should NOT set the preferred size. That is the job of the layout manager. Just add your components to the panels and let the layout manager do its job.
Most compnents have a default preferred size. For some you need to give it a little tip.
For example when using a text area you would give a "suggested" preferred size by using:
JTextArea textArea = new JTextArea(rows, columns);
If you use LayoutManager, you should not set a size on a component except the frame.
the size for the components is calculated from the different layout managers.
you find more infos at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html
in your code, you can add the panel with the textarea to BorderLayout.CENTER. this should solve your problem. the component in BorderLayout.CENTER takes the whole space, except the space needed for the components in NORTH, EAST, SOUTH and WEST.

Add JTabbedPane with buttons, labels ... in a frame with an absolute layout

I have a code in java.
package interfaces;
import javax.swing.JPanel;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane pane = new JTabbedPane();
JPanel dashboardPanel = new JPanel();
dashboardPanel.add(new JLabel("Dashboard"));
// Add Dashboard Tab
pane.addTab("Dashboard", dashboardPanel);
JPanel transactionPanel = new JPanel();
transactionPanel.add(new JLabel("Transactions"));
// Add Transactions Tab
pane.addTab("Transactions", transactionPanel);
JPanel accountPanel = new JPanel();
accountPanel.add(new JLabel("Account"));
// Add Account Tab
pane.addTab("Account", accountPanel);
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(400, 200));
this.add(pane, BorderLayout.CENTER);
}
public static void main(String[] args) {
JPanel panel = new TabbedPaneDemo();
panel.setOpaque(true);
// panel.setBounds(12, 12, 45, 98);
JFrame frame = new JFrame("JTabbedPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}}
and the output it's !
I want to change in this code to be like this.
I want to use an absolute layout (null).
I want to add menu, buttons and labels with these tabs ...
How can I do??
You should NOT use absolute layout. There is not reason to use absolute layout.
Instead you should use layout managers.
Maybe create a panel and add buttons to the panel. Then add the panel to the NORTH of the content pane. The tabbed pane would be added to the CENTER.
Read the Swing tutorial on Using Layout Managers.

JPanel: how to add buttons above table?

JPanel panel = new JPanel();
bigbutton = new JButton("Big Button");
clearbutton = new JButton("Clear Page");
resetbutton = new JButton("Start Over");
finishbutton = new JButton("Finish");
panel.add(sometable);
return panel;
right now what happens is they sprawl horizontally. I want the four buttons horizontally and above the table.
Set you panel's layout to BorderLayout, add your buttons to a box, add the box to your panel with BorderLayout.PAGE_START constraint, add your table with Borderlayout.CENTER constraint.
right now what happens is they sprawl horizontally.
In addition to #tulskiy's and #Andrew Thompson's suggestions, it may be useful to note that the default layout of JPanel is FlowLayout, which arranges components in a horizontal row.
Addendum:
change JPanel's FlowLayout to BorderLayout or some other layout?
Choosing a layout is partly a matter of taste, but I'd combine the suggested approaches, as shown in How to Use Tool Bars. Note how ToolBarDemo extends JPanel and then invokes the superclass constructor to specify BorderLayout():
public class ToolBarDemo extends JPanel implements ActionListener {
...
public ToolBarDemo() {
super(new BorderLayout());
...
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar);
...
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(sometable, BorderLayout.CENTER);
}
...
}
Absent extension, just use the constructor directly:
JPanel panel = new JPanel(new BorderLayout());
Alternative, use the setLayout() method inherited by JPanel.
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());

Categories

Resources