I'm tryint to get my label to align top left and it aligns top center.
Code :JLabel label1 = new JLabel("Text here", JLabel.LEFT); label1.setVerticalAlignment(JLabel.TOP jp1.add(label1);
Printable View
I'm tryint to get my label to align top left and it aligns top center.
Code :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)
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.
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.
Code :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:
Code java: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); } }
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");