I am new to Swing and I don't understand how to do layouts properly. I need to create the following layout
I have tried to use a grid layout and a border layout but I just can't get it to look the way I designed it in the picture. Can anyone help me?
Attempt
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame
{
public Test()
{
//Make a content frame
Container contentPane = getContentPane();
Container contentPane2 = getContentPane();
Container contentPane3 = getContentPane();
//Create a grid layout - This will go to the left
contentPane.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
//Button 1
contentPane.add ( new JButton ( "Button 1" ) );
//Button 2
contentPane.add ( new JButton ( "Button 2" ) );
//Button 3
contentPane.add ( new JButton ( "Button 3" ) );
//Button 4
contentPane.add ( new JButton ( "Button 4" ) );
//Create a border layout - This will go in the middle.
contentPane2.setLayout ( new BorderLayout() );
//Label - Welcome to my application
contentPane2.add ( new JLabel ( "Welcome to my application" ) );
//Image 1
contentPane2.add ( new ImageIcon("img/button.png" ) );
//Change background colour
//Create a grid layout - This will go to the right
contentPane3.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
//Button 5
contentPane3.add ( new JButton ( "Button 5" ) );
//Button 6
contentPane3.add ( new JButton ( "Button 6" ) );
//Button 7
contentPane3.add ( new JButton ( "Button 7" ) );
//Button 8
contentPane3.add ( new JButton ( "Button 8" ) );
//Set window parameters
setTitle ( "Test Application" );
setSize ( 200, 200 );
setVisible ( true );
}
public static void main ( String[] args )
{
Test myFrame = new Test();
}//End main
}//End Class
Please read comments :
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Test extends JFrame{
//when posting code make resources available
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
public Test() throws IOException {
//You clearly have three different areas in your design, so start by making:
JPanel left = new JPanel();
JPanel center = new JPanel();
JPanel right = new JPanel();
//left and right panels holds 4 buttons each. GridLayout will make
//them occupy equal space. You could also use other layout managers like
//Box
left.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
//Button 1
left.add ( new JButton ( "Button 1" ) );
//Button 2
left.add ( new JButton ( "Button 2" ) );
//Button 3
left.add ( new JButton ( "Button 3" ) );
//Button 4
left.add ( new JButton ( "Button 4" ) );
//Create a border layout - This will go in the middle.
center.setLayout ( new BorderLayout() );
//Label - Welcome to my application
center.add ( new JLabel ( "Welcome to my application"),BorderLayout.NORTH);
//Image 1
ImageIcon icon= new ImageIcon(ImageIO.read(url));
center.add ( new JLabel(icon), BorderLayout.CENTER);
//Create a grid layout - This will go to the right
right.setLayout ( new GridLayout ( 4, 1 ) ); //4 Rows and 1 Columns
//Button 5
right.add ( new JButton ( "Button 5" ) );
//Button 6
right.add ( new JButton ( "Button 6" ) );
//Button 7
right.add ( new JButton ( "Button 7" ) );
//Button 8
right.add ( new JButton ( "Button 8" ) );
//add JPanel to content pane which uses Borderlayout by default
getContentPane().add(left, BorderLayout.WEST);
getContentPane().add(center, BorderLayout.CENTER);
getContentPane().add(right, BorderLayout.EAST);
//Set window parameters
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle ( "Test Application" );
//setSize ( 200, 200 ); //size set by layout
pack();
setVisible ( true );
}
public static void main ( String[] args ) throws IOException {
new Test();
}//End main
}//End Class
Related
I am trying to set the weight of the splitter to 0.9 yet it does not seem to work. What am I missing and what am i to do? I've checked this post, yet I could not neither understand nor solve the problem of mine. What I want basically is
something like this though the split pane and the table is always %50,%50. So splitter.setResizeWeight( 0.9 ); is not working.
Here's the code of the panel:
public FlightPanel( final SomeOtherClass category, final SomeClass dar2 )
{
this.detailsPanel = new JPanel( new GridLayout( 0, 1 ) );
this.sum = new JPanel( new GridLayout( 0, 1 ) );
this.model =
new FlightPanelTableModel(...);
this.timeTable = new JTable( this.model );
this.timeTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
this.setLayout( new GridLayout( 0, 1 ) );
this.treeView = new FlightPanelTreeView( dar2 );
ToolTipManager.sharedInstance().registerComponent( this.treeView );
this.detailsPanel.add( this.treeView );
final JSplitPane splitter =
new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, new JScrollPane( this.timeTable ),
new JScrollPane( this.detailsPanel ) );
splitter.setResizeWeight( 0.9 );
this.sum.add( splitter );
this.add( this.sum );
}
How could I solve it?
Thanks in advance.
Is it possible to center the text inside a label vertically? I'm using:
message1 = new Label(shell, SWT.VERTICAL | SWT.CENTER);
This centers the text horizontally, but not vertically.
CLabel label = new CLabel(shell, SWT.CENTER);
Maybe you should try using org.eclipse.swt.custom.CLabel (JavaDoc).
The code above exactly centers the text in the label vertically.
The text within a label is always aligned at the top. The VERTICAL style only applies when SEPARATOR is set. In this case it displays a single vertical or horizontal line and the text is ignored.
But you can center the label itself within the parent. For example, this snippet centers a label within the containing shell by using a GridLayout:
public static void main( String[] args ) {
Display display = new Display();
Shell parent = new Shell( display );
Label label = new Label( parent, SWT.NONE );
label.setText( "some text" );
label.setBackground( display.getSystemColor( SWT.COLOR_GREEN ) );
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true ) );
parent.open();
while( !parent.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
I have this strange behavior, look at the following code (or try it out yourself):
public class JListProblem
{
public static void main (String[] args)
{
JFrame frame = new JFrame("JList Problem");
frame.setSize( 300, 500);
JScrollPane sp = new JScrollPane();
DefaultListModel dlm = new DefaultListModel();
for ( int i = 0; i < 10000; i++ )
{
dlm.addElement( i);
}
JList list = new JList(dlm );
sp.setViewportView( list );
frame.add( sp );
frame.setUndecorated( true );
frame.setBackground( new Color( 0.0f, 0.0f, 0.0f, 0.0f ) );
frame.setVisible( true );
}
}
Here's my problem:
When you try to scroll, it does not scroll "smoothly" (sorry, I don't know the correct word for this).
Try selecting an entry after scrolling: After you clicked, another entry is selected.
How can I correct this behavior?
When you decrease the amount of entries (change the value of maximum i to 1000 for example), everything is working fine.
I would like to have a display area and 8 buttons.
Each Button Will display different text in the display Area.
Currently I just have the Display Area, but When I try to add A button the button overlaps the Display area.
So how can I have a display area and 8 buttons.
JPanel middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
// create the middle panel components
JTextArea display = new JTextArea ( 16, 58 );
display.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
//Add Textarea in to middle panel
middlePanel.add ( scroll );
// My code
JFrame frame = new JFrame ();
JFrame btn = new JFrame();
frame.add ( middlePanel );
frame.pack ();
frame.setLocationRelativeTo ( null );
JButton one = new JButton("1");
JPanel panel = new JPanel();
panel.add(one);
//btn.getContentPane().add(BorderLayout.CENTER,panel);
btn.setVisible(true);
frame.setVisible ( true );
Use two containers, one for the text area and one for the buttons, each with their own layout managers...
JPanel middlePanel = new JPanel (new BorderLayout());
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
JTextArea display = new JTextArea ( 16, 58 );
display.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
middlePanel.add ( scroll );
JPanel buttonPane = new JPanel(); // FlowLayout by default...
buttonPane.add(...); // Add your buttons here...
JFrame frame = new JFrame ();
frame.add ( middlePanel );
frame.add(buttonPane, BorderLayout.SOUTH);
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible(true);
This is commonly known as compound layouts ;)
JPanel middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
// create the middle panel components
JTextArea display = new JTextArea ( 16, 58 );
display.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
//Add Textarea in to middle panel
middlePanel.add ( scroll );
JPanel buttonPane = new JPanel(); // FlowLayout by default...
buttonPane.add(new JButton("1")); // Add your buttons here...
buttonPane.add(new JButton("2"));
buttonPane.add(new JButton("3"));
buttonPane.add(new JButton("4"));
// My code
JFrame frame = new JFrame ();
JFrame btn = new JFrame();
frame.add ( middlePanel );
frame.add(buttonPane,BorderLayout.SOUTH);
frame.pack ();
frame.setLocationRelativeTo ( null );
//btn.getContentPane().add(BorderLayout.CENTER,panel);
btn.setVisible(true);
frame.setVisible ( true );
I have a JPanel and for example, if I click on the button "INSERT", I can add a JButton and a JLabel. My problem is I need to insert the JLabel under the JButton. The JLabel text must centred respect the JButton text. After that, I want a space around 10 pixels to use again my "INSERT" button and add horizontally a new pair on JButton and JLabel with the same orientation.
Thanks!
PD: Please, complement your question with an attempt.
Here is a quick example that shows a dynamic (which is what I assume you wanted) setup to allow insertion of an undefined number of panels:
public class AwesomeAnswer {
public static void main(String[] args) {
// please not that this is only an example and not a
// Swing thread safe way of starting a JFrame
JFrame frame = new JFrame();
JPanel content = (JPanel)frame.getContentPane();
// create our top panel that will hold all of the inserted panels
JPanel page = new JPanel();
page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
// add our page to the frame content pane
content.add( page );
// add two button/label panels
page.add( insert( "This is an awesome answer", "Accept" ) );
page.add( insert( "Say thank you", "Thank" ) );
frame.pack();
frame.setVisible( true );
}
public static final JPanel insert( String labelText, String buttonText ) {
// create the label and the button
JLabel lbl = new JLabel( labelText );
JButton btn = new JButton( buttonText );
// create the panel that will hold the label and the button
JPanel wrapPanel = new JPanel( new GridBagLayout() );
wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// tell the grid bag how to behave
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 0;
gbc.gridheight = 2;
// make the button centered
JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
buttonPanel.add( btn );
// make the label centered
JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
labelPanel.add( lbl );
// add our button and label to the grid bag with our constraints
wrapPanel.add( buttonPanel, gbc );
wrapPanel.add( labelPanel, gbc );
return wrapPanel;
}
}
I think that you have something like that
rootPane
+-----panelButton
| +------JButton
|
+-----panelPanels
+-----panel
+---JButton
+---JLabel
The SpringLayout can help you
SpringUtilities.makeGrid(panel,
2, 1, //rows, cols
0, 0, //initialX, initialY
5, 5);//xPad, yPad