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());
Related
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();
}
}
So I want the button Select to be above button Back and I don't want them overlapping each other. But when I set them both the PAGE_END, they overlap. How do I get around this?
Here is the code for the problem:
public void methodName() {
JPanel controls = new JPanel(new BorderLayout(5,5));
final CardLayout cl = new CardLayout();
final JPanel panel = new JPanel(cl);
controls.add(panel);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
list = new JList<Object>(objectName);
list.setVisibleRowCount(7);
select = new JButton("Select");
back = new JButton("Back");
select.addActionListener(this);
controls.add(new JScrollPane(list));
controls.add(select, BorderLayout.PAGE_END);
controls.add(back, BorderLayout.PAGE_END);
controls.setBorder(new EmptyBorder(25,25,0,0));
add(controls);
refreshFrame();
}
Here is what it looks like when they are both added in but are overlapping:
This is what I want it to look like:
Any ideas?
Thanks in advance!
Place the 2 JButtons on a new JPanel using GridLayout like this
JPanel basePanel = new JPanel(new GridLayout(0, 1, 0, 3));
basePanel.add(select);
basePanel.add(back);
controls.add(basePanel, BorderLayout.PAGE_END);
GridLayout can provide a vertical gap between buttons in its constructor as shown in your question
create a new jpanel with gridLayout(2,1) then add the two buttons to the jpanel then add the Jpanel to the Jframe
Write a program that displays two buttons labeled “Green” and “Orange”.
If the user clicks on the green button, the background of the window changes to green. If the user clicks on the orange button, the background of the window changes to Orange.
Create a JFrame for this GUI. The GUI employs the default layout manager. A JPanel is needed.
Place the two buttons inside the panel and add the panel to the south region of the border layout.
Notice the text in the title bar. The green button should have white text and a green background. The orange button should have black text with an orange background.
Below is what I have so far, it doesn't seem to work.
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I have used BorderLayout for the JFrame and FlowLayout for the ButtonPanel. ButtonPanel is the bottom panel of the frame.
frame = new JFrame();
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));
middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));
frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. So there is no need for this statement.
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. So the following statements do nothing:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers. There is a section on using a BorderLayout and on using a FlowLayout. I don't know if you are supposed to use just panels with a BorderLayout or panels with a combination of BorderLayout and FlowLayout. I'll let you fix the code to meet your requirement.
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.
I'd like to use a JScrollPane for a panel which has an arbitrary list of labels in it using box layout. I'm trying to get it so that the scrollbar would appear if there were too many items (labels) to display.
I tried adding a JScrollPane to the panel and then add the labels but then I don't see any scroll bar.
Any ideas?
TIA
For this kind of thing, you'd normally use a JList or JTable (if you need custom rendering).
Make sure that you call validate() or revalidate() on the JScrollPane after adding an item, to force the preferred size of the panel to be recalculated.
Here's how I did it.
JPanel midPanel = new JPanel();
midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
midPanel.add(new JLabel("<html><u>Label</u>"));
Box box = Box.createVerticalBox();
for (Item item : data.getInventory()) {
inventory.add(box.add(new JLabel(item.getName())));
}
JScrollPane jscrlpBox = new JScrollPane(box);
midPanel.add(jscrlpBox);
add(midPanel, BorderLayout.CENTER);
From:
http://www.java2s.com/Code/Java/Swing-JFC/JScrollPanetoholdscrollablecomponent.htm
Did you remember to set the preferred size of the content panel?
final JFrame frame = new JFrame("Scroll Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
final Box textArea = Box.createVerticalBox();
final JScrollPane textAreaScroll = new JScrollPane(textArea);
textAreaScroll.setPreferredSize(new Dimension(80,150)); /* essential! */
JButton addButton = new JButton("ADD");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.add(new JLabel("abc"));
textArea.revalidate();
}
});
frame.getContentPane().add(textAreaScroll, BorderLayout.SOUTH);
frame.getContentPane().add(Box.createRigidArea(new Dimension(10,10)), BorderLayout.CENTER);
frame.getContentPane().add(addButton, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
In this example, the scroll bar works correctly, but if you remove the line marked as "essential", it will not work anymore.