Java GUI: Display Area and 8 buttons - java

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 );

Related

JSplitPane Setting Resize Weight not Working

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.

How can I create the following layout in Swing?

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

Center text vertically inside label

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();
}

Add a JScrollPane to avoid distortion of components

I am trying to make a variable size interface. However, currently, if the size of all components is greater than the dimension of the window, then those begin to
distort ! Therefore, I would like to add a JScrollPane to be displayed when the window size is no longer sufficient, in order to avoid this distortion.
I just started by adding a JScrollPane (as shown on the code bellow), but it does not seem to work, and I am not sure what to change now.
JScrollPane myScrollPane = new JScrollPane();
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER);
EDIT 1: here is a simple example of what I have tried:
static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){
int p;
int length = ari.length;
Border cadre = BorderFactory.createLineBorder(Color.black);
Font police = new Font("Monospaced 13", Font.BOLD, 18);
Font police1 = new Font("Monospaced 13", Font.BOLD, 15);
Font font = new Font("Monospaced 13", Font.ITALIC, 13);
for (p=0;p<length;p++){
Box boxz = Box.createVerticalBox();
JLabel Rate = new JLabel("Rating Group "+r);
JLabel rg_reser = new JLabel();
JLabel switsh = new JLabel("1");
JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked
JLabel consump = new JLabel(); //in this label will be stocked the consumption
//----------------------------------------------------
//choice of MB or kB
JComboBox combo = new JComboBox();
combo.addItem("Megabytes");
combo.addItem("Bytes");
combo.addItem("kilobytes");
combo.addItem("Gigabytes");
JLabel upload = new JLabel("Upload consumption:");
JTextField upload_entry = new JTextField("7.5");
JLabel download = new JLabel("Download consumption:");
JTextField download_entry = new JTextField("7.5");
JTextField total_entry = new JTextField();
JLabel total = new JLabel("Total consumption:");
JButton rg = new JButton("Next");
JLabel update = new JLabel("Result here");
boxz.add(Rate);
boxz.add(total);
boxz.add(total_entry);
boxz.add(upload);
boxz.add(upload_entry);
boxz.add(download);
boxz.add(download_entry);
boxz.add(combo);
boxz.add(rg);
boxz.add(update);
boxy.add(boxz);
scrollPane1.add(boxy);
}
EDIT 2: here is a new test code, but it also does not work:
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
content.add(imad);
content.add(textArea);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
A small white scare appears (I think it is the scroll bar or scroll panel?) and the components in the window exceeds the windows size but nothing happens when I try to scroll (using the mouse or by clinking on the white scare). I don't know what is wrong.
Don't use scrollPanel.add(boxy) - you need to add boxy to the scrollPanel's viewport, not to the scrollPanel itself. Use scrollPanel.setViewportView(boxy) instead, or better yet create the ScrollPanel AFTER you create boxy and use the constructor that takes a Component argument.

What layout on Java I need?

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

Categories

Resources