Solved

adjust the location of a JLabel

Posted on 2004-04-13
36
372 Views
Last Modified: 2013-11-23
Hi,
I got this problem...I try to use "setBounds" or "setLocation" to move my JLabels to the places I want but nothing works...what's wrong with my codes??

JTabbedPane         MainPane;
  JTabbedPane       ATabPane;
    JPanel               A1Tab;
      JPanel             Detail1Panel;
        JLabel           Detail1;
        JLabel           Detail2;
      JPanel             Detail2Panel;
    JPanel               A2Tab;
  JTabbedPane       BTabPane;

public void init()
{
  MainPane = new JTabbedPane();
  ATabPane = new JTabbedPane();
  BTabPane = new JTabbedPane();

  MainPane.add(ATabPane);
  MainPane.add(BTabPane);
  InitATabPane();
  getContentPane().add(MainPane);
  setSize(610, 600);
}

public void InitATabPane()
{
  A1Panel = new JPanel();
  A2Panel = new JPanel();
  ATabPane.add("Tab A.1", A1Panel);
  ATabPane.add("Tab A.2", A2Panel);

  A1Panel.setLayout(null);
  A1Panel.setPreferredSize(new Dimension(600, 200));
  A1Panel.setSize(600, 200);
  A1Panel.setLayout(null);
  A1Panel.setPreferredSize(new Dimension(600, 200));
  A1Panel.setSize(600, 200);
  InitA1Tab();
}

public void InitA1Tab()
{
  Detail1Panel = new JPanel();
  Detail2Panel = new JPanel();
  Detail1  = new JLabel("Detail");
  Detail2  = new JLabel("Info");

  Detail1Panel.add(Detail1);
  Detail1Panel.add(Detail2);
  Detail1.setLocation(0, 0);
  Detail2.setLocation(0, 10);

  A1Panel.add(Detail1Panel);
}

"Detail' and "Info" does not show..@@
Anything goes wrong in my codes???  Help please~~
0
Comment
Question by:xenia27
  • 14
  • 11
  • 11
36 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10819781
That cause you are using a layout manager, if you want to specify absolute position then use a null layout manager:

 Detail1Panel = new JPanel();
 Detail1Panel.setLayout(null);
 Detail2Panel = new JPanel();
 Detail2Panel.setLayout(null);
0
 
LVL 92

Expert Comment

by:objects
ID: 10819782
you'll also need to set the size of your labels if you aren't using a layout manager.
0
 
LVL 92

Expert Comment

by:objects
ID: 10819851
you also need to set the size and location of Detail1Panel.
0
 

Author Comment

by:xenia27
ID: 10819857
how big is a letter?  how can I know the values to assign the size??
0
 
LVL 92

Expert Comment

by:objects
ID: 10819901
thats why layout managers are used :)
why don't u want to use one?
0
 

Author Comment

by:xenia27
ID: 10819914
mMmm...if I use layout manager...can I set the location the JLabels should be??
0
 
LVL 92

Expert Comment

by:objects
ID: 10819951
Not directly, the layout manager would handle the placement.

If you want to not use layout managers then you need to handle the positioning and sizing of *all* components that are not managed by a layout manager.
0
 

Author Comment

by:xenia27
ID: 10820447
so how can the layout manager know a particular JLabel or JButton should be placed next line or something like that??
or I need to handle all the location stuff by myself instead of using layout manager?  which one is better? ^^
0
 
LVL 92

Expert Comment

by:objects
ID: 10820476
Learning how to use LayoutManagers will save you heaps of time:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
0
 
LVL 92

Expert Comment

by:objects
ID: 10820479
Absolute position should only be used in rare cases where there is a specific need for it.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10820693
>> which one is better?
Using the Layoutmanagers is definitely the way to go!
Then you don't have to ask yourself questions like
>> how big is a letter?  how can I know the values to assign the size??

>> How can the layout manager know a particular JLabel or JButton should be placed next line
You have to tell. It's not because you use layoutmanagers that you loose control.

If with "next line" you mean under the other label, you could use the GridBagLayout.
(http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridBagLayout.html)
See it as some kind of matrix where you can decide where a component is placed by setting it's X (column) and Y (row) value

e.g.
                X=0                  x=1               x=2


y=0       Detail1


y=1       Detail2


Knowing this, do you have a more concrete question?
Just ask. We'll help.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 10820831
To give you an idea I made a simple GridBagLayoutDemo:

/*
 * GBDemo.java
 */

public class GBDemo extends javax.swing.JFrame {

    private javax.swing.JLabel Label00;
    private javax.swing.JLabel Label01;
    private javax.swing.JLabel Label10;
    private javax.swing.JLabel Label11;
    private javax.swing.JLabel Label22;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
   
    public GBDemo() {
        initComponents();
    }
   
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        Label00 = new javax.swing.JLabel();
        Label01 = new javax.swing.JLabel();
        Label10 = new javax.swing.JLabel();
        Label11 = new javax.swing.JLabel();
        Label22 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridBagLayout());

        Label00.setText("Label at 0,0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label00, gridBagConstraints);

        Label01.setText("Label at 0,1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label01, gridBagConstraints);

        Label10.setText("Label at 1,0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label10, gridBagConstraints);

        Label11.setText("Label at 1,1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label11, gridBagConstraints);

        Label22.setText("label at 2,2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label22, gridBagConstraints);

        jButton1.setText("Button at 1,3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jButton1, gridBagConstraints);

        getContentPane().add(jPanel1);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        GBDemo demo = new GBDemo();
        demo.setLocationRelativeTo(null);
        demo.show();
    }
   
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10820882
personally would suggest never using GBL, I find it way too cryptic and difficult to get things right.
There isn't really a best layout manager, they all serve different purposes and selection is based on how you want your components laid out.
And remember you don't need to use just one (and would find it difficult to any), every component has its own layout manager.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821049
>> personally would suggest never using GBL, I find it way too cryptic and difficult to get things right.
I use it rather frequently without any problems

>> they all serve different purposes
That's why sometimes I do use GBL.
0
 

Author Comment

by:xenia27
ID: 10821059
Not sure what's wrong...but I tried to use Layout Manager but not working...@@

Here is what I did..

public void InitATabPane()
{
  A1Panel = new JPanel();
  A2Panel = new JPanel();
  ATabPane.add("Tab A.1", A1Panel);
  ATabPane.add("Tab A.2", A2Panel);

  A1Panel.setPreferredSize(new Dimension(600, 200));  <-- I took off "setLayout(null)"
  A1Panel.setSize(600, 200);
  A1Panel.setLayout(null);
  A1Panel.setPreferredSize(new Dimension(600, 200));
  A1Panel.setSize(600, 200);
  InitA1Tab();
}

public void InitA1Tab()
{
  Detail1Panel = new JPanel();
  Detail2Panel = new JPanel();
  Detail1  = new JLabel("Detail");
  Detail2  = new JLabel("Info");

  Detail1Panel.add(Detail1, BorderLayout.WEST);
  Detail1Panel.add(Detail2, BorderLayout.AFTER_LAST_LINE);

  A1Panel.add(Detail1Panel);
}


what's wrong with my codes??
0
 

Author Comment

by:xenia27
ID: 10821068
Detail1 and Detail2 are shown right in the middle of the JPanel...@@
Also, why I don't have BorderLayout.PAGE_START or LINE_START...etc??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821104
>>Detail1Panel = new JPanel();
>>...
>>Detail1Panel.add(Detail1, BorderLayout.WEST);

The last instruction is useless unless you first:

Detail1Panel.setLayout(new BorderLayout());
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 10821105
try somethging like this, though i have no idea what layout u want to achieve :)

public void InitA1Tab()
{
  Detail1Panel = new JPanel(new BorderLayout());
  Detail2Panel = new JPanel(new BorderLayout());
  Detail1  = new JLabel("Detail");
  Detail2  = new JLabel("Info");

  Detail1Panel.add(Detail1, BorderLayout.WEST);
  Detail1Panel.add(Detail2, BorderLayout.NORTH);

  A1Panel.add(Detail1Panel);
}
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 37

Expert Comment

by:zzynx
ID: 10821113
Explanation:

If you create a new JPanel it has by default the FlowLayout (Center)

It's not because you perform

            Detail1Panel.add(Detail1, BorderLayout.WEST);

that it gets the BorderLayout.

You have to set it:
            Detail1Panel.setLayout(new BorderLayout());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821123
You see, me and objects agree, at least at that point ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821134
btw.

        Detail1Panel = new JPanel(new BorderLayout());

equals to
     
        Detail1Panel = new JPanel();
        Detail1Panel.setLayout(new BorderLayout());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821151
Java Help says about AFTER_LAST_LINE:
"Synonym for PAGE_END. Exists for compatibility with previous versions. PAGE_END is preferred."

And about PAGE_END:
"The component comes after the last line of the layout's content.
For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH."
0
 

Author Comment

by:xenia27
ID: 10821194
Can I use "setLocation" and "setBounds" when I use Layout manager?  or there are other ways to do so?

still confused on how exactly I can move the positions with Layout Manager...@@..Sorry~~
0
 
LVL 92

Expert Comment

by:objects
ID: 10821207
> Can I use "setLocation" and "setBounds" when I use Layout manager?

no

> till confused on how exactly I can move the positions with Layout Manager

why do u want to move positions?

0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821221
The idea of a LayoutManager is that you don't specify the exact position of your components.
You specify how they will "behave" when your dialog/window/frame is re-sized.
Will they grow? Will they keep their initial size (you can specify)?
Will they always stay at the bottom of your window? (BorderLayout.SOUTH)
What components will take the extra space if your window grows and so on.
0
 

Author Comment

by:xenia27
ID: 10821237
I wanna do something like this...

************************************************
Name:                        SomeName
Address:                    Some Address
Deatil1                       Detail3
Detail2                       Detail4
************************************************

How can I do this with Layout manager?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821265
Did you copy/paste my GBL example?
It fits exactly in the GBLayout's matrix:

          |       x=0                           x=1
------------------------------------------------------------
y=0    |       Name:                        SomeName
y=1    |       Address:                    Some Address
y=2    |       Deatil1                       Detail3
y=3    |       Detail2                       Detail4
0
 

Author Comment

by:xenia27
ID: 10821272
sorry~  I'm going to try now...^^
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821309
Here's the adapted code (I took JLabels (with awful names) for everything, but of course it could be whatever component)

/*
 * GBDemo.java
 *
*/

public class GBDemo extends javax.swing.JFrame {

    private javax.swing.JLabel Label00;
    private javax.swing.JLabel Label01;
    private javax.swing.JLabel Label10;
    private javax.swing.JLabel Label11;
    private javax.swing.JLabel Label22;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
   
    public GBDemo() {
        initComponents();
    }
   
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        Label00 = new javax.swing.JLabel();
        Label01 = new javax.swing.JLabel();
        Label10 = new javax.swing.JLabel();
        Label11 = new javax.swing.JLabel();
        Label22 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridBagLayout());

        Label00.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(Label00, gridBagConstraints);

        Label01.setText("Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(Label01, gridBagConstraints);

        Label10.setText("Some name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(Label10, gridBagConstraints);

        Label11.setText("some address");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(Label11, gridBagConstraints);

        Label22.setText("Detail1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(Label22, gridBagConstraints);

        jLabel1.setText("Detail2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Detail3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Detail4");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel3, gridBagConstraints);

        getContentPane().add(jPanel1);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        GBDemo demo = new GBDemo();
        demo.setLocationRelativeTo(null);
        demo.show();
    }
   
   
}
0
 

Author Comment

by:xenia27
ID: 10821395
why I don't have this function??  ->  demo.setLocationRelativeTo(null);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821410
That's just to center the demo program on the screen when it starts up.
You can comment it out, and you'll see that the demo program appears at the upper left corner
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821414
Sets the location of the window relative to the specified component.
If the component is not currently showing, or is null, the window is centered on the screen.
0
 

Author Comment

by:xenia27
ID: 10821419
Ok...I got what I wanna do now...thanks!!!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10821433
Thanks xenia.
Till the next
0
 

Author Comment

by:xenia27
ID: 10821437
^^
0
 
LVL 92

Expert Comment

by:objects
ID: 10821465
:)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

706 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

18 Experts available now in Live!

Get 1:1 Help Now