New to GUI, I am trying to create a simple JFrame with two JTextAreas positioned right next to each other and a JPanel at the bottom.
public class Demo extends JFrame
{
JPanel panel;
JTextArea JTextArea1;
JTextArea JTextArea2;
DecisionPanel decisionPanel;
public Demo()
{
super( "Black Jack Server" );
JFrame f = new JFrame();
f.setSize( 400, 400 ); ;
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setVisible( true );
DecisionPanel decisionPanel = new DecisionPanel();
f.getContentPane().add( decisionPanel );
JTextArea1 = new JTextArea();
add( JTextArea1);
JTextArea2 = new JTextArea();
add( JTextArea2 );
}
}
Do I use BorderLayout to get the result that I want? If so, how should I approach?
You could nest JPanels and...
place the JTextAreas in their own JScrollPanes,
place the JScrollPanes into a GridLayout(1, 2) (1 row, two columns) using JPanel
place that JPanel into a BorderLayout using JPanel in the BorderLayout.CENTER position
and place your bottom JPanel in the BorderLayout.PAGE_END position.
Something like:
Related
The primary GUI of my application is composed of a JDesktopPane at the CENTER of a frame's content pane using a BorderLayout. I am hoping to have a component placed in the top right of the screen that still allows the user to drag JInternalFrames within the space to the left and and bottom of this component.
Setting the component to the NORTH or EAST of the BorderLayout seems to fill the entire space. I am thinking BorderLayout may not be the best layout manager for what I am trying to accomplish? Any suggestions on a better approach?
Check out the OverlayLayout. It allows you to stack components on top of one another.
You need to manipulate the setAlignmentX(..) and setAlignmentY(...)` methods to get the layout you want. It is not always intuitive how these alignments work together but setting the component to the top/left is relatively easy.
Here is a little demo for you to play with:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class OverlayLayoutTest extends JPanel
implements ActionListener
{
JPanel green;
JPanel red;
JLabel greenLabel;
JLabel redLabel;
JComboBox greenAlignmentX;
JComboBox greenAlignmentY;
JComboBox redAlignmentX;
JComboBox redAlignmentY;
public OverlayLayoutTest()
{
setLayout( new BorderLayout(10, 10) );
add(createNorthPanel(), BorderLayout.NORTH);
add(createCenterPanel(), BorderLayout.CENTER);
add(createSouthPanel(), BorderLayout.SOUTH);
}
private JPanel createNorthPanel()
{
JPanel panel = new JPanel();
panel.add( new JLabel("Green:") );
greenLabel = new JLabel();
panel.add( greenLabel );
panel.add( new JLabel("Red:") );
redLabel = new JLabel();
panel.add( redLabel );
return panel;
}
private JPanel createCenterPanel()
{
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
panel.setBackground( Color.ORANGE );
panel.setPreferredSize( new Dimension(200, 200) );
red = new JPanel();
red.setBackground( Color.RED );
red.setPreferredSize( new Dimension(50, 50) );
red.setMaximumSize( red.getPreferredSize() );
red.setMinimumSize( red.getPreferredSize() );
panel.add( red );
green = new JPanel();
green.setBackground( Color.GREEN );
green.setPreferredSize( new Dimension(100, 100) );
green.setMaximumSize( green.getPreferredSize() );
green.setMinimumSize( green.getPreferredSize() );
panel.add( green );
JPanel wrap = new JPanel();
wrap.add( panel );
return wrap;
}
private JPanel createSouthPanel()
{
JPanel panel = new JPanel( new GridLayout(1, 0, 10, 10) );
JPanel green = new JPanel(new GridLayout(0, 2, 5, 5) );
green.setBorder( new TitledBorder("Green Alignment") );
green.add( new JLabel("X Alignment:") );
greenAlignmentX = createComboBox();
green.add( greenAlignmentX );
green.add( new JLabel("Y Alignment:") );
greenAlignmentY = createComboBox();
green.add( greenAlignmentY );
panel.add( green );
JPanel red = new JPanel(new GridLayout(0, 2, 5, 5) );
red.setBorder( new TitledBorder("Red Alignment") );
red.add( new JLabel("X Alignment:") );
redAlignmentX = createComboBox();
red.add( redAlignmentX );
red.add( new JLabel("Y Alignment:") );
redAlignmentY = createComboBox();
red.add( redAlignmentY );
panel.add( red );
JButton reset = new JButton("Reset Alignment");
reset.addActionListener( this );
panel.add( reset );
return panel;
}
public void actionPerformed(ActionEvent e)
{
green.setAlignmentX( ((Float)greenAlignmentX.getSelectedItem()) );
green.setAlignmentY( ((Float)greenAlignmentY.getSelectedItem()) );
red.setAlignmentX( ((Float)redAlignmentX.getSelectedItem()) );
red.setAlignmentY( ((Float)redAlignmentY.getSelectedItem()) );
JPanel parent = (JPanel)green.getParent();
parent.revalidate();
/*
System.out.print(green.getAlignmentX() + " : ");
System.out.print(green.getAlignmentY() + " : ");
System.out.print(red.getAlignmentX() + " : ");
System.out.print(red.getAlignmentY() + " : ");
System.out.println();
*/
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
greenLabel.setText( green.getLocation().toString() );
redLabel.setText( red.getLocation().toString() );
}
});
}
private JComboBox createComboBox()
{
JComboBox<Float> comboBox = new JComboBox<Float>();
comboBox.addItem( new Float(0f) );
comboBox.addItem( new Float(0.25f) );
comboBox.addItem( new Float(0.5f) );
comboBox.addItem( new Float(0.75f) );
comboBox.addItem( new Float(1.0f) );
comboBox.setSelectedItem(0.5f);
return comboBox;
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("OverlayLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new OverlayLayoutTest() );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
set the alignment X values to 1.0 for both components
set the alignment Y values to 0.0 for both components
and you should get the layout you want.
Edit:
Missed the part about dragging a JInternalFrame. So this imples you are using a JDesktopPane to support the dragging. A JDesktopPane uses a null layout to allow you to drag components around.
There is no reason you can't add another component (other than a JInternalFrame) to the desktop. You just need to set the size/location of this component to be displayed at the top right of the desktop. You would then need to add a ComponentListener to the desktop pane to listen for the componentResized event. When this event is fired you would need to recalucate the location of the component to reset it to the top right.
I have this piece of code that is basically a JFrame that contains a JSplitPane which contains on the left side a JScrollPane which contains a JPanel. I expected to see the scroll bars since the JPanel inside the JScrollPane is larger that the JScrollPane itself. Why are the scroll bars not displayed?
If I replace setSize() with setPreferredSize() then it works, but I want to use setSize(). Is there any way I can use setSize() and have the scroll bars showing?
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setLayout( new BorderLayout() );
JSplitPane splitPane = new JSplitPane();
frame.add( splitPane, BorderLayout.CENTER );
JPanel panel = new JPanel();
panel.setBackground( Color.red );
panel.setSize( 1920, 1200 );
//panel.setPreferredSize( new Dimension( 1920, 1200 ) );
JScrollPane scrollPane = new JScrollPane( panel );
splitPane.setLeftComponent( scrollPane );
splitPane.setRightComponent( new JPanel() );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
frame.setSize( 960, 600 );
}
}
Edit: I've added a modified version where I use setPreferredSize(). Is there a better solution for dynamically changing the size?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setLayout( new BorderLayout() );
JSplitPane splitPane = new JSplitPane();
frame.add( splitPane, BorderLayout.CENTER );
final JPanel panel = new JPanel();
panel.setBackground( Color.red );
panel.setPreferredSize( new Dimension( 1920, 1200 ) );
JScrollPane scrollPane = new JScrollPane( panel );
splitPane.setLeftComponent( scrollPane );
JButton button = new JButton();
button.addActionListener( new ActionListener() {
#Override
public void actionPerformed( ActionEvent e ) {
panel.setPreferredSize( new Dimension( 3840, 2400 ) );
panel.revalidate();
}
});
splitPane.setRightComponent( button );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
frame.setSize( 960, 600 );
}
}
Your setSize will be ignored by the JSplitPane because the layout of the left/right components is not null, and it tries to fit the internal components in the available space.
The layout manager for the JSplitPane's left/right components honors the preferredSize property(and not the size property) and if it hasn't been set, it just tries to fit the internal component inside the available space of left/right area in JSplitPane.
Use setPreferredSize instead or override the getPreferredSize method for your panel as camickr described in the comment.
That's what I did at first.
public class MyFrame extends JFrame {
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500 ,300));
setResizable(false);
pack();
setLocationRelativeTo(null);
initComponents();
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int i=0; i < 100; i++)
panel.add(new JLabel("some text"));
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel();
contentPane.add(scrollPane);
setContentPane(contentPane);
}
If instead I replace the last 3 lines with this:
getContentPane().add(scrollPane);
everything is ok. But as I did before, the vertical scrollbar is not showing up. What is causing this? Is setting a JPanel as contentPane wrong?
Update:
If contentPane changes to BorderLayout everything work fine.
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(scrollPane);
setContentPane(contentPane);
So the problem is default FlowLayout?
Solved:
The problem is FlowLayout. It wraps around JScrollPane and hides the Toolbars. using
scrollPane.setPreferredSize(new Dimension(500, 400)); // longer space in x-axis
solves it.
Answer:
JSrollPane should not be used inside a Container that uses FlowLayout.
First of all - there is nothing bad in using your own component as content pane. But default content pane is also a JPanel instance so there is actually no point to replace it with your own panel, unless you want to use non-panel content pane or customized panel component.
This is how the default content pane looks like:
/**
* Called by the constructor methods to create the default
* <code>contentPane</code>.
* By default this method creates a new <code>JComponent</code> add sets a
* <code>BorderLayout</code> as its <code>LayoutManager</code>.
* #return the default <code>contentPane</code>
*/
protected Container createContentPane() {
JComponent c = new JPanel();
c.setName(this.getName()+".contentPane");
c.setLayout(new BorderLayout() {
/* This BorderLayout subclass maps a null constraint to CENTER.
* Although the reference BorderLayout also does this, some VMs
* throw an IllegalArgumentException.
*/
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints == null) {
constraints = BorderLayout.CENTER;
}
super.addLayoutComponent(comp, constraints);
}
});
return c;
}
This method is taken from JRootPane. It is basically a simple JPanel with a but customized layout manager as you can see.
Now, you have a few problems in your example.
First is the order of calls - you are sizing frame before adding content into it. Simply change the order and you will see your scrollpane:
public class MyFrame extends JFrame
{
public MyFrame ()
{
super();
// Add components first
initComponents ();
// Setup frame after so it fits its new content
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
setPreferredSize ( new Dimension ( 500, 300 ) );
setResizable ( false );
pack ();
setLocationRelativeTo ( null );
}
private void initComponents ()
{
JPanel panel = new JPanel ();
panel.setLayout ( new BoxLayout ( panel, BoxLayout.Y_AXIS ) );
for ( int i = 0; i < 100; i++ )
{
panel.add ( new JLabel ( "some text" ) );
}
JScrollPane scrollPane =
new JScrollPane ( panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel ();
contentPane.add ( scrollPane );
setContentPane ( contentPane );
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater ( new Runnable ()
{
public void run ()
{
new MyFrame ().setVisible ( true );
}
} );
}
}
It will still look different because your new JPanel () uses FlowLayout by default, instead of BorderLayout used by default content pane component:
Simply set BorderLayout and you will have the result you want to see:
JPanel contentPane = new JPanel ( new BorderLayout () );
Have a look at Rob Camick's WrapLayout, which is an extension of FlowLayout
import java.awt.*;
import javax.swing.*;
public class TestWrapLayout {
public TestWrapLayout () {
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
JPanel panel = new JPanel(new WrapLayout());
for (int i = 1; i <= 250; i++) {
JLabel iconlabel = new JLabel(icon);
iconlabel.setLayout(new BorderLayout());
JLabel textlabel = new JLabel(String.valueOf(i));
textlabel.setHorizontalAlignment(JLabel.CENTER);
textlabel.setForeground(Color.WHITE);
textlabel.setFont(new Font("impact", Font.PLAIN,20));
iconlabel.add(textlabel);
panel.add(iconlabel);
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestWrapLayout();
}
});
}
}
You must update your frame after adding your panel using pack() function. When you do
getContentPane().add(scrollPane);
the function add does that for you (ref)
Firstly, what was the reason to create the second panel? There
is already the first panel that has BoxLayout as the layout
manager set. Simply setting the scroll pane having the first panel
as parent works as expected.
You either call
setContentPane(scrollPane);
or
add(scrollPane);
Now I am going to explain what caused this unexpected behaviour. This
is a quirk that sometimes happens to those who use nesting technique
when building their layouts. When nesting is used, the layouts may
influence each other.
By choosing another layout -- FlowLayout -- as the underlying base layout,
you caused the first panel to be displayed in its preferred size. Instead
of one panel, you have now two panels, the base panel influences the
panel with labels -- it controls how it is sized. The FlowLayout
shows all its children in preferred size; it does not honour mimimum nor
maximum sizes. So the (first) visible panel is sized to show all its labels;
this is how preferred size is calculated -- just big enough to show all its
children. However, with 100 labels, it is very big; the layout is broken. It is
vertically so big that we cannot practically get to the bottom of the window.
So with our visible panel showing all its labels, what's the purpose of showing
a verticall scrollbar? No need for one, since all labels are "visible" (placed on
the window area), though the desing is broken.
So the problem does not lie with the scrollbars; they work normally. If you
set (in your example) the vertical scrollbar policy to VERTICAL_SCROLLBAR_ALWAYS
you will see the scrollbar there but without a slider, since all labels are "visible"
and there is nothing to be scrolled. (Scrollbars show items that are hidden from the
layout.) The issue lies in the fact that FlowLayout shows its components in the preferred size only.
The following is a fixed code example that works as expected:
package com.zetcode;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
setTitle("Scrollbar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 200));
pack();
setLocationRelativeTo(null);
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int i=0; i < 100; i++)
panel.add(new JLabel("some text"));
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setContentPane(scrollPane);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame ex = new MyFrame();
ex.setVisible(true);
}
});
}
}
I'm trying to add a JPanel into another and to fit it to the parent `JPanel size.
I have a JPanel which contains a JTable and a JButton using this code:
JScrollPane attributeTable = new JScrollPane(this);
JPanel attributesPanel = new JPanel();
JPanel textFieldPanel=new JPanel();
JPanel buttonsPanel = new JPanel();
JButton newAttributeButton = new JButton("New Attribute");
attributesPanel.add(textFieldPanel, BorderLayout.CENTER);
attributesPanel.add(buttonsPanel, BorderLayout.SOUTH);
newAttributeButton.addActionListener(AttributesTableController.getInstance());
GroupLayout layout = new GroupLayout(textFieldPanel);
textFieldPanel.setLayout(layout);
// Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true);
// Turn on automatically creating gaps between components that touch
// the edge of the container and the container.
layout.setAutoCreateContainerGaps(true);
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
// Putting the labels in a parallel group along the horizontal axis
// positions them at the same x location.
//
// Variable indentation is used to reinforce the level of grouping.
hGroup.addGroup(layout.createParallelGroup().
addComponent(attributeTable).addComponent(newAttributeButton));
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(attributeTable));
vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER).
addComponent(newAttributeButton));
layout.setVerticalGroup(vGroup);
When I had this panel (named attributesPanel) into a tab of a JTabbedPane, it just display the JTable and the JButton, but in the center of the panel. I would like that the dimension of attributesPanel are the same as the dimension of opened tab.
This is the code I use to add the JPanel into my JTabbedPane:
TabbedPane tabbedPane = new TabbedPane();
tabbedPane.setComponentAt(1, attributesPanel);
I tried using a GridLayout, it fitted the JPanel well but I was not able to resize the button. I tried with a FlowLayout and a GridBagLayout, but I was not able to display them correctly because I had the same problem.
Thank you in advance.
Your example is incomplete sop it is hard to tell what is wrong, but maybe this will help:
JTable table = new JTable (12, 5);
JButton button = new JButton ("Button");
JPanel panel = new JPanel ();
panel.setLayout (new BorderLayout ());
panel.add (table, BorderLayout.CENTER);
panel.add (button, BorderLayout.SOUTH);
JTabbedPane tabbedPane = new JTabbedPane ();
tabbedPane.addTab ("Tab", panel);
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.getContentPane ().add (tabbedPane, BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);
I'm attempting to create a program that allows the user to click a button to place something in the JPanel and allowing them to move this item around. I have already found a good layout to use to allow the moving components (see this link). However, I'm just curious the best way to create a layout like this? My hope is to have something like this:
How can I accomplish this? Would I want to use two JPanel's or something else?
The main panel (or the window content pane) would have to have a BorderLayout as the layout manager.
Then, the buttons panel would be added to BorderLayout.WEST and the drag panel to BorderLayout.CENTER.
There is a Visual Guide to swing layout managers.
Try to use JSplitPane:
Here is a code example:
class SplitPane extends JFrame {
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public SplitPane(){
setTitle( "Split Pane Application" );
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
createPanel1();
createPanel2();
createPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
topPanel.add( splitPaneV, BorderLayout.CENTER );
splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
splitPaneH.setLeftComponent( panel1 );
splitPaneH.setRightComponent( panel2 );
splitPaneV.setLeftComponent( splitPaneH );
splitPaneV.setRightComponent( panel3 );
}
public void createPanel1(){
panel1 = new JPanel();
panel1.setLayout( new BorderLayout() );
// Add some buttons
panel1.add( new JButton( "North" ), BorderLayout.NORTH );
panel1.add( new JButton( "South" ), BorderLayout.SOUTH );
panel1.add( new JButton( "East" ), BorderLayout.EAST );
panel1.add( new JButton( "West" ), BorderLayout.WEST );
panel1.add( new JButton( "Center" ), BorderLayout.CENTER );
}
public void createPanel2(){
panel2 = new JPanel();
panel2.setLayout( new FlowLayout() );
panel2.add( new JButton( "Button 1" ) );
panel2.add( new JButton( "Button 2" ) );
panel2.add( new JButton( "Button 3" ) );
}
public void createPanel3(){
panel3 = new JPanel();
panel3.setLayout( new BorderLayout() );
panel3.setPreferredSize( new Dimension( 400, 100 ) );
panel3.setMinimumSize( new Dimension( 100, 50 ) );
panel3.add( new JLabel( "Notes:" ), BorderLayout.NORTH );
panel3.add( new JTextArea(), BorderLayout.CENTER );
}
public static void main( String args[] ){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible( true );
}
}
You can play with splitPaneH.setOneTouchExpandable true/false
You can confugure divider location for both like:
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int width = d.width;
int height = d.height;
spane.setDividerLocation((width*3)/4);
spanex.setDividerLocation(width/4);