Results 1 to 3 of 3
  1. #1
    Kristoffer is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default How to move JLabel around on a panel


    I have some problem with my code. I have not managed to move a JLabel around on my JPanel. I have tried some different approaches in the code, they are in there and it compiles fine but will not move the JLabel. Anyone out there that could tell me what I am missing?

    Java Code:
    package chapter3;
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    public class Panel {
    	public static void main(String[] args) {
    		Color myColor = new Color(255, 0, 120);
    		JPanel subpanel = new JPanel();
    		subpanel.setPreferredSize(new Dimension(150, 150));
    		JLabel label = new JLabel("Some text");
    		label.setAlignmentX(SwingConstants.LEFT);              //not working
    		label.setAlignmentX(0);                                         //not working
    		label.setHorizontalAlignment(SwingConstants.LEFT);  //not working
    		label.setHorizontalAlignment(0);                             //not working
    		//Main panel
    		JPanel panel = new JPanel();
    		panel.setPreferredSize(new Dimension(200, 200));
    		JFrame frame = new JFrame("The Frame");
    		frame.setLocation(200, 200);

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

    Default Re: How to move JLabel around on a panel

    The alignment property is only used by some layout manager to control the position within the panel. You add your label to a JPanel which uses a FlowLayout that centers components by default. So the FlowLayout ignores the alignment property.

    1) Add the label directly to the JFrame, which uses a BorderLayout. Then the width of the label will be the width of the frame.

    2) You only need to use the following property to align the text within its own bounds:

    Java Code:
    Last edited by camickr; 11-11-2011 at 06:12 PM.

  3. #3
    Kristoffer is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Re: How to move JLabel around on a panel

    Thanks for showing me this. Adding BorderLayout to my panel worked perfectly!

    Java Code:
    JPanel subpanel = new JPanel(new BorderLayout());

Similar Threads

  1. How to move a JLabel?
    By Zambash in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2011, 07:47 PM
  2. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 09:29 PM
  3. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 08:43 PM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  5. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts