I'm tryint to get my label to align top left and it aligns top center.
JLabel label1 = new JLabel("Text here", JLabel.LEFT); label1.setVerticalAlignment(JLabel.TOP jp1.add(label1);
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I'm tryint to get my label to align top left and it aligns top center.
JLabel label1 = new JLabel("Text here", JLabel.LEFT); label1.setVerticalAlignment(JLabel.TOP jp1.add(label1);
Code you post should be in SSCCE form, that way we know exactly what you're doing. For example, what layout are you using?
Until I see an SSCCE, I can only point you towards this tutorial: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I'm sorry, but I don't know how to post in SSCCE. I'm not getting any errors when I run it, it just doesn't align properly. Would it be better to post all of the code?
Thanks
No. If you don't know how to post an SSCCE, that's why I provided a link that explains exactly how to do it. Check it out and let me know if you have any questions.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I did check it out. I'm not sure how to do it.
What do you mean? You eliminate any code that doesn't have to do with your problem, and you put it in a single compilable/runnable class so other people can simply copy and paste it into their own IDE and run it.
In your case, we'd just need a single JFrame holding a single JLabel in exactly the way you're trying to do it in your program.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
import javax.swing.*; import javax.swing.JLabel; import javax.swing.plaf.metal.MetalIconFactory; public class Alignment { protected JTabbedPane tabPane; public Alignment() { tabPane = new JTabbedPane(); tabPane.add(new JLabel("One", JLabel.LEFT), "First"); Icon warnIcon = MetalIconFactory.getTreeComputerIcon(); JLabel label1 = new JLabel(warnIcon); label1.setHorizontalTextPosition(JLabel.LEFT); label1.setVerticalTextPosition(JLabel.TOP); } public static void main(String[] a) { JFrame f = new JFrame("Tab Demo"); f.getContentPane().add(new Alignment().tabPane); f.setSize(400, 200); f.setVisible(true); } }
Yeah, the problem is that you aren't really using layouts how you should be. Which JLabel are you trying to align? You don't even add label1 at all.
This uses a BorderLayout to get your JLabel to the top of a JPanel:
import java.awt.BorderLayout; import javax.swing.*; public class Alignment { protected JTabbedPane tabPane; public Alignment() { tabPane = new JTabbedPane(); JLabel labelOne = new JLabel("One", JLabel.LEFT); JPanel panel = new JPanel(new BorderLayout()); panel.add(labelOne, BorderLayout.NORTH); tabPane.add(panel); } public static void main(String[] a) { JFrame f = new JFrame("Tab Demo"); f.getContentPane().add(new Alignment().tabPane); f.setSize(400, 200); f.setVisible(true); } }
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
fride360 (March 10th, 2011)
Thanks, that's what I was trying to do. How do you name the tab?
You had that part right the first time.
tabPane.add(panel, "Tab One");
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
fride360 (March 10th, 2011)