?
Solved

Formatting a java applet

Posted on 2005-04-04
7
Medium Priority
?
207 Views
Last Modified: 2013-11-23
Hey there. I'm creating a 350X275 sized applet. I want to have 5 labels on the left edge (BorderLayout.WEST) and 4 textboxes and one group of two radio buttons on the right. At the bottom two buttons. Something like this:

label 1        textbox
label 2        textbox2
label 3        textbox3
label 4        radiobtn 1 radiobtn 2
label 5        textbox5
   button1  button2

I can't for the life of me get it format though. I have the base JPanel, another JPanel for the labels, a third and fourth for the textboxes and
buttons respectively. My textboxes always end up being 2-3 textbox heights though and look really bloated.
Any formatting suggestions or tips would be appreciated.
0
Comment
Question by:ViceroyFizzlebottom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13700215
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13700221
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13700551
Yes, use GridBagLayout.. I also wrote a tutorial on it the other week, see if it helps:
http://www.freewebs.com/darkins/tutorials/create_gui_in_java.html

>> IM
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:aozarov
ID: 13700725
InteractiveMind, I actually saw it (nice work) sorry for not mentioning it :-(
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 750 total points
ID: 13700780
Thanks aozarov  :)  hehe.

ViceroyFizzlebottom, here's an example of the layout you require, but in an application (with the use of GridBagLayout):

//---[ Example.java ]---
   
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
   
    public class Example extends JFrame
    {
       
        private JPanel  pane                     = null;
        private GridBagConstraints  constraints  = null;
        private GridBagLayout       gridbag      = null;
       
        public Example()
        {
            super("Layout Example");
            setBounds(200,200,350,275);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
            constraints = new GridBagConstraints();
            gridbag = new GridBagLayout();
           
            pane = new JPanel();
            setContentPane(pane);
            pane.setLayout(gridbag);
           
            JLabel label1 = new JLabel("Label 1");
            JLabel label2 = new JLabel("Label 2");
            JLabel label3 = new JLabel("Label 3");
            JLabel label4 = new JLabel("Label 4");
            JLabel label5 = new JLabel("Label 5");
           
            JTextField text1 = new JTextField();
            JTextField text2 = new JTextField();
            JTextField text3 = new JTextField();
            JTextField text4 = new JTextField();
           
            JRadioButton rad1 = new JRadioButton("RadB 1");
            JRadioButton rad2 = new JRadioButton("RadB 2");
           
            JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
           
            button1.addActionListener(this);
            button2.addActionListener(this);
           
            buildConstraints( label1, 0, 0, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( label2, 0, 1, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( label3, 0, 2, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( label4, 0, 3, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( label5, 0, 4, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( text1, 1, 0, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
            buildConstraints( text2, 1, 1, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
            buildConstraints( text3, 1, 2, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
            buildConstraints( text4, 1, 4, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
            buildConstraints( rad1, 1, 3, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( rad2, 2, 3, 1, 1, 33, 16, GridBagConstraints.NONE, GridBagConstraints.CENTER );
            buildConstraints( button1, 0, 5, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
            buildConstraints( button2, 1, 5, 1, 1, 33, 16, GridBagConstraints.HORIZONTAL );
           
            this.setVisible(true);
        }
       
        private void buildConstraints( Component c, int gx, int gy, int gw, int gh, int wx, int wy, int fill, int anchor )
        {
            constraints.gridx = gx;
            constraints.gridy = gy;
            constraints.gridwidth = gw;
            constraints.gridheight = gh;
            constraints.weightx = wx;
            constraints.weighty = wy;
           
            constraints.fill = fill;
            constraints.anchor = anchor;
           
            gridbag.setConstraints(c, constraints);
            pane.add(c);
        }
       
        private void buildConstraints( Component c, int gx, int gy, int gw, int gh, int wx, int wy, int fill )
        {
            constraints.gridx = gx;
            constraints.gridy = gy;
            constraints.gridwidth = gw;
            constraints.gridheight = gh;
            constraints.weightx = wx;
            constraints.weighty = wy;
           
            constraints.fill = fill;
           
            gridbag.setConstraints(c, constraints);
            pane.add(c);
        }
       
        public static void main ( String [] args )
        {
            new Example();
        }
       
    }
//---[ Example.java ]---

I hope that helps.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13700789
(You could shorten that code if necessary...) ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 13701956
> My textboxes always end up being 2-3 textbox heights though and look really bloated.

What layout are you using for their container panel? A BoxLayout sounds like what you need.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question