Solved

Java Swing : Alignment Customization

Posted on 2006-11-26
13
424 Views
Last Modified: 2013-11-23
Dear experts:

Hi again!

I have a small, nice-working Java applet code.

I would like to make some changes to its layout or alignment in such a way that "rankList" combo box is vertically aligned with "Rnaks" sub-heading with some space from the left margin and more buffer space is placed between topPanel and centerPanel and between centerPanel and bottomPanel like as below:


________________________  My wished Costumized Layout _________________________________
 
                                            Payroll Calculator

                                            (More space between Payroll Calculator and the subheadings below)

         Ranks                                    Marriage Status                         Residency Status
        ComboBox                             @ Married                                 @ Resident
                                                    @ Single                                   @ Nonresident


<--->  an inch from the left margin
                                                   ______________________
      Years in Current Position:       |_____________________|
                                             (more space between "years in current position" and "Hours worked this month"
                                                  ______________________
      Hours   Worked This Month:   |_____________________|
                                            (more space between the above and the below )
                                                    ______________________
      Your Total Pay This Month:       |_____________________|

                                             (more space  between the above and the below )
                                             ___________                     ______________
                                            | Calculate   |                    |  Reset             |
                                            |__________|                   |_____________|
                             

________________________________________________________________________________

I would like to learn how to customize from you experts.

Thanks a lot!

duta



0
Comment
Question by:duta
  • 7
  • 6
13 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
Comment Utility
I suggest you use TableLayout: https://tablelayout.dev.java.net/
IMHO its the best layout manager I've ever seen, just read its tutorial to get start
0
 

Author Comment

by:duta
Comment Utility
Thanks a lot, hoomanv.

I am checking the site you kindly gave me. I will be back after trying it.

Thanks again!

duta
0
 

Author Comment

by:duta
Comment Utility
TO: hoomanv:

I downloaded four  .jar files, and do not know what do with them.
There are no directions available regarding the four files.

Do I need to place them in a folder in the directory of  jdk 1.5 or jre 1.5? Do I need to register to use them?

Thanks a lot!

duta
0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
TableLayout.jar should be included in your classpath
0
 

Author Comment

by:duta
Comment Utility
Thanks again, hoomanv.

In C:\Program Files\Java\jre1.5.0_07\lib, I found several .jar files.  Do you mean "C:\Program Files\Java\jre1.5.0_07\lib" as the classpath?

duta
0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
mindprod.com/jgloss/classpath.html
CLASSPATH is an environment variable
You can set/view it through the command line
type
> set classpath
to view it
and to add new paths to it type
> set classpath=%classpath%;new-path
the new-path in your case is the absolute path to TableLayout.jar
for example:
> set classpath=%classpath%;c:\TableLayout.jar
now you successfully installed this library
read the tutorial to learn how to this layout manager works
https://tablelayout.dev.java.net/articles/TableLayoutTutorialPart1/TableLayoutTutorialPart1.html
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:duta
Comment Utility
To: hoomanv:
Thanks a lot for your kind tips.

By the way, I wonder whether I may ask you one more quick question:

I put "TableLayout.jar" in the directory of  "c:\Program Files\Java\tableLayout".

Do I need to go to run > type "set classpath = %classpath%; c:\Program Files\Java\tableLayout\TableLayout.jar"?

Thanks

duta

0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
If you put it in C:\Program Files\Java\jre*****\lib\ext
Then there is no need to set classpath
0
 

Author Comment

by:duta
Comment Utility
hoomanv, thanks a lot for your kind, patient tips.

I will take time to read all the instructions in the site.

Thanks again!
0
 

Author Comment

by:duta
Comment Utility
TO: hoomanv:

Thanks for your kind help.

By the way, when trying to compile, I got an error message:

ERROR: Package layout does not exist.

___________ Here are my imports _______________
import  java.awt.*;
import  javax.swing.*;
import  java.awt.event.*;
import  layout.TableLayout;
_______________________________________________
0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
This one is correct
import info.clearthought.layout.TableLayout;
0
 

Author Comment

by:duta
Comment Utility
TO: hoomanv:

Hi again!

I found a simple TableLayout tutorial at a website, and tried the sample code (below). I got an error message:

"canot find symbol constructor TableLayout".

Can you kindly find why?  I am trying the code because it looked really  simple.

Thanks!

_________________________  Sample code _____________


import  java.awt.*;
import  javax.swing.*;
import  java.awt.event.*;
import info.clearthought.layout.TableLayout;




public class Example1
{


public static void
main(
    String[] args)
{
    new Example1();
}


public
Example1()
{
    JFrame frame1 = createGridBagLayout();
    JFrame frame2 = createTableLayout();
    frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame1.pack();
    frame2.pack();
    frame1.setLocation(10, 100);
    frame2.setLocation(400, 100);
    frame1.setVisible(true);
    frame2.setVisible(true);
}

//**********************************************************************
// Private
//**********************************************************************

/**
 * Create the layout using GridBagLayout. This code comes from the API
 * page for GridBagLayout.
 *
 * @return The JFrame containing the layout.
 */

public JFrame
createGridBagLayout()
{
    JFrame frame = new JFrame("GridBagLayout");

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    JPanel panel = new JPanel(gridbag);
    frame.getContentPane().add(panel);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    makebutton(panel, "Button1", gridbag, c);
    makebutton(panel, "Button2", gridbag, c);
    makebutton(panel, "Button3", gridbag, c);

    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    makebutton(panel, "Button4", gridbag, c);

    c.weightx = 0.0;   //reset to the default
    makebutton(panel, "Button5", gridbag, c); //another row

    c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
    makebutton(panel, "Button6", gridbag, c);

    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    makebutton(panel, "Button7", gridbag, c);

    c.gridwidth = 1;   //reset to the default
    c.gridheight = 2;
    c.weighty = 1.0;
    makebutton(panel, "Button8", gridbag, c);

    c.weighty = 0.0;   //reset to the default
    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    c.gridheight = 1;   //reset to the default
    makebutton(panel, "Button9", gridbag, c);
    makebutton(panel, "Button10", gridbag, c);

    return frame;
}

/**
 * Helper method for createGridBagLayout().
 *
 * @param panel The panel to add the button to.
 * @param name The button's label.
 * @param gridbag The GridBagLayout to use.
 * @param c The constraints to use.
 */

private void
makebutton(
    JPanel panel,
    String name,
    GridBagLayout gridbag,
    GridBagConstraints c)
{
    JButton button = new JButton(name);
    gridbag.setConstraints(button, c);
    panel.add(button);
}

/**
 * Create the layout using TableLayout.
 *
 * @return The JFrame containing the layout.
 */

public JFrame
createTableLayout()
{
    JFrame frame = new JFrame("TableLayout");

    JPanel panel = new JPanel(new TableLayout("cols=4"));
    frame.getContentPane().add(panel);

    panel.add(new JButton("Button1"));
    panel.add(new JButton("Button2"));
    panel.add(new JButton("Button3"));
    panel.add(new JButton("Button4"));
    panel.add(new JButton("Button5"), "cspan=4");
    panel.add(new JButton("Button6"), "cspan=3");
    panel.add(new JButton("Button7"));
    panel.add(new JButton("Button8"), "rspan=2");
    panel.add(new JButton("Button9"), "cspan=3");
    panel.add(new JButton("Button10"), "cspan=3 rweight=1");

    return frame;
}

}
0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
These are the constructors

TableLayout()
TableLayout(double[][] size)
TableLayout(double[] col, double[] row)

There is no TableLayout(String s) !!! thats why you get error

Read the tutorial https://tablelayout.dev.java.net/articles/TableLayoutTutorialPart1/TableLayoutTutorialPart1.html
Everything is explained there
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now