xenia27
asked on
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(MainP ane);
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(n ew Dimension(600, 200));
A1Panel.setSize(600, 200);
A1Panel.setLayout(null);
A1Panel.setPreferredSize(n ew 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~~
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(MainP
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(n
A1Panel.setSize(600, 200);
A1Panel.setLayout(null);
A1Panel.setPreferredSize(n
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~~
you'll also need to set the size of your labels if you aren't using a layout manager.
you also need to set the size and location of Detail1Panel.
ASKER
how big is a letter? how can I know the values to assign the size??
thats why layout managers are used :)
why don't u want to use one?
why don't u want to use one?
ASKER
mMmm...if I use layout manager...can I set the location the JLabels should be??
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.
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.
ASKER
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? ^^
or I need to handle all the location stuff by myself instead of using layout manager? which one is better? ^^
Learning how to use LayoutManagers will save you heaps of time:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
Absolute position should only be used in rare cases where there is a specific need for it.
>> 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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
>> 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.
I use it rather frequently without any problems
>> they all serve different purposes
That's why sometimes I do use GBL.
ASKER
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(n ew Dimension(600, 200)); <-- I took off "setLayout(null)"
A1Panel.setSize(600, 200);
A1Panel.setLayout(null);
A1Panel.setPreferredSize(n ew 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_LI NE);
A1Panel.add(Detail1Panel);
}
what's wrong with my codes??
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(n
A1Panel.setSize(600, 200);
A1Panel.setLayout(null);
A1Panel.setPreferredSize(n
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_LI
A1Panel.add(Detail1Panel);
}
what's wrong with my codes??
ASKER
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??
Also, why I don't have BorderLayout.PAGE_START or LINE_START...etc??
>>Detail1Panel = new JPanel();
>>...
>>Detail1Panel.add(Detail1 , BorderLayout.WEST);
The last instruction is useless unless you first:
Detail1Panel.setLayout(new BorderLayout());
>>...
>>Detail1Panel.add(Detail1
The last instruction is useless unless you first:
Detail1Panel.setLayout(new
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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());
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
You see, me and objects agree, at least at that point ;°)
btw.
Detail1Panel = new JPanel(new BorderLayout());
equals to
Detail1Panel = new JPanel();
Detail1Panel.setLayout(new BorderLayout());
Detail1Panel = new JPanel(new BorderLayout());
equals to
Detail1Panel = new JPanel();
Detail1Panel.setLayout(new
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."
"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."
ASKER
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~~
still confused on how exactly I can move the positions with Layout Manager...@@..Sorry~~
> 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?
no
> till confused on how exactly I can move the positions with Layout Manager
why do u want to move positions?
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.
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.
ASKER
I wanna do something like this...
************************** ********** ********** **
Name: SomeName
Address: Some Address
Deatil1 Detail3
Detail2 Detail4
************************** ********** ********** **
How can I do this with Layout manager?
**************************
Name: SomeName
Address: Some Address
Deatil1 Detail3
Detail2 Detail4
**************************
How can I do this with Layout manager?
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
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
ASKER
sorry~ I'm going to try now...^^
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.GridBagConstraint s 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.a wt.FlowLay out.LEFT)) ;
addWindowListener(new java.awt.event.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridBagLayout());
Label00.setText("Name:");
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label00, gridBagConstraints);
Label01.setText("Address:" );
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label01, gridBagConstraints);
Label10.setText("Some name");
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label10, gridBagConstraints);
Label11.setText("some address");
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label11, gridBagConstraints);
Label22.setText("Detail1") ;
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
jPanel1.add(Label22, gridBagConstraints);
jLabel1.setText("Detail2") ;
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
jPanel1.add(jLabel1, gridBagConstraints);
jLabel2.setText("Detail3") ;
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
jPanel1.add(jLabel2, gridBagConstraints);
jLabel3.setText("Detail4") ;
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
jPanel1.add(jLabel3, gridBagConstraints);
getContentPane().add(jPane l1);
pack();
}
private void exitForm(java.awt.event.Wi ndowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
GBDemo demo = new GBDemo();
demo.setLocationRelativeTo (null);
demo.show();
}
}
/*
* 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.GridBagConstraint
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
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridBagLayout());
Label00.setText("Name:");
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraint
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label00, gridBagConstraints);
Label01.setText("Address:"
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraint
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label01, gridBagConstraints);
Label10.setText("Some name");
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraint
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label10, gridBagConstraints);
Label11.setText("some address");
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraint
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
jPanel1.add(Label11, gridBagConstraints);
Label22.setText("Detail1")
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint
jPanel1.add(Label22, gridBagConstraints);
jLabel1.setText("Detail2")
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint
jPanel1.add(jLabel1, gridBagConstraints);
jLabel2.setText("Detail3")
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint
jPanel1.add(jLabel2, gridBagConstraints);
jLabel3.setText("Detail4")
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
gridBagConstraints.anchor = java.awt.GridBagConstraint
jPanel1.add(jLabel3, gridBagConstraints);
getContentPane().add(jPane
pack();
}
private void exitForm(java.awt.event.Wi
System.exit(0);
}
public static void main(String args[]) {
GBDemo demo = new GBDemo();
demo.setLocationRelativeTo
demo.show();
}
}
ASKER
why I don't have this function?? -> demo.setLocationRelativeTo (null);
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
You can comment it out, and you'll see that the demo program appears at the upper left corner
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.
If the component is not currently showing, or is null, the window is centered on the screen.
ASKER
Ok...I got what I wanna do now...thanks!!!
Thanks xenia.
Till the next
Till the next
ASKER
^^
:)
Detail1Panel = new JPanel();
Detail1Panel.setLayout(nul
Detail2Panel = new JPanel();
Detail2Panel.setLayout(nul