• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

adjust the location of a JLabel

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
xenia27
Asked:
xenia27
  • 14
  • 11
  • 11
2 Solutions
 
objectsCommented:
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
 
objectsCommented:
you'll also need to set the size of your labels if you aren't using a layout manager.
0
 
objectsCommented:
you also need to set the size and location of Detail1Panel.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
xenia27Author Commented:
how big is a letter?  how can I know the values to assign the size??
0
 
objectsCommented:
thats why layout managers are used :)
why don't u want to use one?
0
 
xenia27Author Commented:
mMmm...if I use layout manager...can I set the location the JLabels should be??
0
 
objectsCommented:
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
 
xenia27Author Commented:
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
 
objectsCommented:
Learning how to use LayoutManagers will save you heaps of time:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
0
 
objectsCommented:
Absolute position should only be used in rare cases where there is a specific need for it.
0
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
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
 
objectsCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
xenia27Author Commented:
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
 
xenia27Author Commented:
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
 
zzynxSoftware engineerCommented:
>>Detail1Panel = new JPanel();
>>...
>>Detail1Panel.add(Detail1, BorderLayout.WEST);

The last instruction is useless unless you first:

Detail1Panel.setLayout(new BorderLayout());
0
 
objectsCommented:
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
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
You see, me and objects agree, at least at that point ;°)
0
 
zzynxSoftware engineerCommented:
btw.

        Detail1Panel = new JPanel(new BorderLayout());

equals to
     
        Detail1Panel = new JPanel();
        Detail1Panel.setLayout(new BorderLayout());
0
 
zzynxSoftware engineerCommented:
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
 
xenia27Author Commented:
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
 
objectsCommented:
> 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
 
zzynxSoftware engineerCommented:
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
 
xenia27Author Commented:
I wanna do something like this...

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

How can I do this with Layout manager?
0
 
zzynxSoftware engineerCommented:
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
 
xenia27Author Commented:
sorry~  I'm going to try now...^^
0
 
zzynxSoftware engineerCommented:
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
 
xenia27Author Commented:
why I don't have this function??  ->  demo.setLocationRelativeTo(null);
0
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
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
 
xenia27Author Commented:
Ok...I got what I wanna do now...thanks!!!
0
 
zzynxSoftware engineerCommented:
Thanks xenia.
Till the next
0
 
xenia27Author Commented:
^^
0
 
objectsCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 14
  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now