JLabel automatically centers it self inside GridBagLayout
I'm trying to make a custom JTextField that hosts a JLabel, So I have wrapped the JTextField and the JLabel in a custom Class that extends JPanel
The JPanel layout is set to GridBagLayout since I only need place 2 components vertically. Here is how it looks like

This is the code, I can't spot where I have instructed the GridBagLayout to center the JLabel.
setLayout(new GridBagLayout());
jLabel = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridy = 1;
add(jLabel, gbc);

I want to have the JLabel the same position at shown in the GIF. How can I achieve this? Would also be nice if anyone can point what the exact text size would look good on the JLabel.
java swing
|
show 1 more comment
I'm trying to make a custom JTextField that hosts a JLabel, So I have wrapped the JTextField and the JLabel in a custom Class that extends JPanel
The JPanel layout is set to GridBagLayout since I only need place 2 components vertically. Here is how it looks like

This is the code, I can't spot where I have instructed the GridBagLayout to center the JLabel.
setLayout(new GridBagLayout());
jLabel = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridy = 1;
add(jLabel, gbc);

I want to have the JLabel the same position at shown in the GIF. How can I achieve this? Would also be nice if anyone can point what the exact text size would look good on the JLabel.
java swing
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
layout is set to GridBagLayout since I only need place 2 components vertically.- or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.
– camickr
Nov 16 '18 at 15:27
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30
|
show 1 more comment
I'm trying to make a custom JTextField that hosts a JLabel, So I have wrapped the JTextField and the JLabel in a custom Class that extends JPanel
The JPanel layout is set to GridBagLayout since I only need place 2 components vertically. Here is how it looks like

This is the code, I can't spot where I have instructed the GridBagLayout to center the JLabel.
setLayout(new GridBagLayout());
jLabel = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridy = 1;
add(jLabel, gbc);

I want to have the JLabel the same position at shown in the GIF. How can I achieve this? Would also be nice if anyone can point what the exact text size would look good on the JLabel.
java swing
I'm trying to make a custom JTextField that hosts a JLabel, So I have wrapped the JTextField and the JLabel in a custom Class that extends JPanel
The JPanel layout is set to GridBagLayout since I only need place 2 components vertically. Here is how it looks like

This is the code, I can't spot where I have instructed the GridBagLayout to center the JLabel.
setLayout(new GridBagLayout());
jLabel = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridy = 1;
add(jLabel, gbc);

I want to have the JLabel the same position at shown in the GIF. How can I achieve this? Would also be nice if anyone can point what the exact text size would look good on the JLabel.
java swing
java swing
asked Nov 16 '18 at 15:07
SamzSakerzSamzSakerz
1
1
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
layout is set to GridBagLayout since I only need place 2 components vertically.- or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.
– camickr
Nov 16 '18 at 15:27
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30
|
show 1 more comment
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
layout is set to GridBagLayout since I only need place 2 components vertically.- or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.
– camickr
Nov 16 '18 at 15:27
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
layout is set to GridBagLayout since I only need place 2 components vertically. - or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.– camickr
Nov 16 '18 at 15:27
layout is set to GridBagLayout since I only need place 2 components vertically. - or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.– camickr
Nov 16 '18 at 15:27
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30
|
show 1 more comment
2 Answers
2
active
oldest
votes
the JLabel does not start from line start, nor is it center. It's kinda in between
I'm not sure what your exact code is and how you nest the custom panel in another panel.
But as I mentioned above when you have weird alignment you need to make sure each component has the same x alignment so they can be aligned the same way.
I can't reproduce your problem using the simple example below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
SSCCE()
{
JPanel box = new JPanel();
box.setLayout( new BoxLayout(box, BoxLayout.Y_AXIS) );
box.setBorder( new LineBorder(Color.RED) );
JTextField textField = new JTextField(10);
//textField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
System.out.println( textField.getAlignmentX() );
box.add( textField );
JLabel label = new JLabel("Some text");
System.out.println( label.getAlignmentX() );
box.add( label );
add( box );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
But as you can see from the output by default a text field has center alignment and the label left alignment. Try making them both left aligned.
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making thelynxTextFieldleft fixed. I was trying to set thejLabelto left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results
– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, butsetLocationhas no effect on the JLabel.
– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
|
show 3 more comments
Thanks to VGR in the comments, I have solved this issue by setting the anchor to LINE_STARTS
gbc.anchor = GridBagConstraints.LINE_STARTS;
Edit: Ended up using BoxLayout thanks to camickr for answering with an example.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340443%2fjlabel-automatically-centers-it-self-inside-gridbaglayout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
the JLabel does not start from line start, nor is it center. It's kinda in between
I'm not sure what your exact code is and how you nest the custom panel in another panel.
But as I mentioned above when you have weird alignment you need to make sure each component has the same x alignment so they can be aligned the same way.
I can't reproduce your problem using the simple example below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
SSCCE()
{
JPanel box = new JPanel();
box.setLayout( new BoxLayout(box, BoxLayout.Y_AXIS) );
box.setBorder( new LineBorder(Color.RED) );
JTextField textField = new JTextField(10);
//textField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
System.out.println( textField.getAlignmentX() );
box.add( textField );
JLabel label = new JLabel("Some text");
System.out.println( label.getAlignmentX() );
box.add( label );
add( box );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
But as you can see from the output by default a text field has center alignment and the label left alignment. Try making them both left aligned.
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making thelynxTextFieldleft fixed. I was trying to set thejLabelto left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results
– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, butsetLocationhas no effect on the JLabel.
– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
|
show 3 more comments
the JLabel does not start from line start, nor is it center. It's kinda in between
I'm not sure what your exact code is and how you nest the custom panel in another panel.
But as I mentioned above when you have weird alignment you need to make sure each component has the same x alignment so they can be aligned the same way.
I can't reproduce your problem using the simple example below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
SSCCE()
{
JPanel box = new JPanel();
box.setLayout( new BoxLayout(box, BoxLayout.Y_AXIS) );
box.setBorder( new LineBorder(Color.RED) );
JTextField textField = new JTextField(10);
//textField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
System.out.println( textField.getAlignmentX() );
box.add( textField );
JLabel label = new JLabel("Some text");
System.out.println( label.getAlignmentX() );
box.add( label );
add( box );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
But as you can see from the output by default a text field has center alignment and the label left alignment. Try making them both left aligned.
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making thelynxTextFieldleft fixed. I was trying to set thejLabelto left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results
– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, butsetLocationhas no effect on the JLabel.
– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
|
show 3 more comments
the JLabel does not start from line start, nor is it center. It's kinda in between
I'm not sure what your exact code is and how you nest the custom panel in another panel.
But as I mentioned above when you have weird alignment you need to make sure each component has the same x alignment so they can be aligned the same way.
I can't reproduce your problem using the simple example below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
SSCCE()
{
JPanel box = new JPanel();
box.setLayout( new BoxLayout(box, BoxLayout.Y_AXIS) );
box.setBorder( new LineBorder(Color.RED) );
JTextField textField = new JTextField(10);
//textField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
System.out.println( textField.getAlignmentX() );
box.add( textField );
JLabel label = new JLabel("Some text");
System.out.println( label.getAlignmentX() );
box.add( label );
add( box );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
But as you can see from the output by default a text field has center alignment and the label left alignment. Try making them both left aligned.
the JLabel does not start from line start, nor is it center. It's kinda in between
I'm not sure what your exact code is and how you nest the custom panel in another panel.
But as I mentioned above when you have weird alignment you need to make sure each component has the same x alignment so they can be aligned the same way.
I can't reproduce your problem using the simple example below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JPanel
{
SSCCE()
{
JPanel box = new JPanel();
box.setLayout( new BoxLayout(box, BoxLayout.Y_AXIS) );
box.setBorder( new LineBorder(Color.RED) );
JTextField textField = new JTextField(10);
//textField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
System.out.println( textField.getAlignmentX() );
box.add( textField );
JLabel label = new JLabel("Some text");
System.out.println( label.getAlignmentX() );
box.add( label );
add( box );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
But as you can see from the output by default a text field has center alignment and the label left alignment. Try making them both left aligned.
answered Nov 16 '18 at 15:52
camickrcamickr
275k15127239
275k15127239
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making thelynxTextFieldleft fixed. I was trying to set thejLabelto left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results
– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, butsetLocationhas no effect on the JLabel.
– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
|
show 3 more comments
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making thelynxTextFieldleft fixed. I was trying to set thejLabelto left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results
– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, butsetLocationhas no effect on the JLabel.
– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
Ah I see, I am having the same problem as that.
– SamzSakerz
Nov 16 '18 at 15:56
making the
lynxTextField left fixed. I was trying to set the jLabel to left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results– SamzSakerz
Nov 16 '18 at 15:57
making the
lynxTextField left fixed. I was trying to set the jLabel to left, But I don't think I can achieve the animation shown in the GIF using a BoxLayout? Or what Layout would be the best for the exact results– SamzSakerz
Nov 16 '18 at 15:57
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I'm not sure why the animation would be any different? Neither layout manager supports animation. So you need to fake the layout by manually setting the location until the component reaches its final position. Based on your gif I thought you had that process working, so it should be the same.
– camickr
Nov 16 '18 at 16:01
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, but
setLocation has no effect on the JLabel.– SamzSakerz
Nov 16 '18 at 16:03
I don't have the animation working, But I am assuming I would need to start the label at the center of the JTextField and work my way down, but
setLocation has no effect on the JLabel.– SamzSakerz
Nov 16 '18 at 16:03
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
I have accepted your answer since I am going on with the box layout, I will ask about the animation in a different question after my attempts.
– SamzSakerz
Nov 16 '18 at 16:09
|
show 3 more comments
Thanks to VGR in the comments, I have solved this issue by setting the anchor to LINE_STARTS
gbc.anchor = GridBagConstraints.LINE_STARTS;
Edit: Ended up using BoxLayout thanks to camickr for answering with an example.
add a comment |
Thanks to VGR in the comments, I have solved this issue by setting the anchor to LINE_STARTS
gbc.anchor = GridBagConstraints.LINE_STARTS;
Edit: Ended up using BoxLayout thanks to camickr for answering with an example.
add a comment |
Thanks to VGR in the comments, I have solved this issue by setting the anchor to LINE_STARTS
gbc.anchor = GridBagConstraints.LINE_STARTS;
Edit: Ended up using BoxLayout thanks to camickr for answering with an example.
Thanks to VGR in the comments, I have solved this issue by setting the anchor to LINE_STARTS
gbc.anchor = GridBagConstraints.LINE_STARTS;
Edit: Ended up using BoxLayout thanks to camickr for answering with an example.
edited Nov 16 '18 at 16:10
answered Nov 16 '18 at 15:25
SamzSakerzSamzSakerz
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340443%2fjlabel-automatically-centers-it-self-inside-gridbaglayout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Set the anchor in your GridBagConstraints.
– VGR
Nov 16 '18 at 15:22
@VGR, I tried to make anchor use PAGE_START but it did not work. Forgot to put it in the post. Will edit Thanks! :)
– SamzSakerz
Nov 16 '18 at 15:24
Ah alright I see, Theres a LINE_STARTS attribute @VGR thanks.
– SamzSakerz
Nov 16 '18 at 15:26
layout is set to GridBagLayout since I only need place 2 components vertically.- or you could use a vertical BoxLayout. You would also need to set the alignmentX to 0.0 to make sure each component is left justified in the box.– camickr
Nov 16 '18 at 15:27
@camickr I have tried to use BoxLayout after your suggestion but the JLabel does not start from line start, nor is it center. It's kinda in between, How would I resolve this?
– SamzSakerz
Nov 16 '18 at 15:30