[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Formatting a java applet

Posted on 2005-04-04
7
Medium Priority
?
220 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
  • 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

590 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