Getting the text in one line

Page 1 of 1

11 Replies - 1872 Views - Last Post: 22 February 2012 - 10:22 PM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Getting the text in one line

Posted 21 February 2012 - 08:54 PM

private JLabel label1, label2;

    public WindowsBox() {
        super("Testing Label");
        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        
        label1 = new JLabel("Label with text");
        label1.setToolTipText("This is label1");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(label1);

        Icon icon = new ImageIcon("http://www.nfcrumors.com/wp-content/uploads/2011/07/hi-256-4-0d7facef5753bf69b74de8ccb0144f2cf94062fa.png");
        label2 = new JLabel("Label with Text and Icon", icon, SwingConstants.LEFT);
        label2.setToolTipText("This is Label2");
        container.add(label2);

        setSize(275, 180);
        
        setVisible(true);

    }


This is the output i get:

----------------------------------------
Testing Labels
----------------------------------------
Label with text Label with Text and Icon





----------------------------------------


I want "Label with Text and Icon" below.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting the text in one line

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:00 PM

Where do you print[ln] that output ?
Was This Post Helpful? 0
  • +
  • -

#3 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Re: Getting the text in one line

Posted 21 February 2012 - 09:03 PM

View Postpbl, on 21 February 2012 - 11:00 PM, said:

Where do you print[ln] that output ?


Oh! Yeah. This is GUI! I'm getting a window.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:07 PM

So what do you want your JLabel to look like ? Your post is quite obscur.

Quote

I want "Label with Text and Icon" below.

below what ? The Text betwen "" or the label itself ?

This post has been edited by pbl: 21 February 2012 - 09:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:21 PM

I think he doesn't want all the labels to show on one line

FlowLayout lines up 5 elements by default next to each other, you can call a different constructor to specify the max horizontal alignment like new FlowLayout(2)
Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:23 PM

FlowLayout by default stores all the possible components in one line

http://docs.oracle.c...FlowLayout.html

You will have to use another layout
May be Box b = Box.createVerticalBox()
and add your labels to that Box b

This post has been edited by pbl: 21 February 2012 - 09:23 PM

Was This Post Helpful? 2
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:24 PM

or you can just set the layout to null and manually place the components as you wish.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Getting the text in one line

Posted 21 February 2012 - 09:25 PM

View Postmostyfriedman, on 21 February 2012 - 11:24 PM, said:

or you can just set the layout to null and manually place the components as you wish.

:v: Not a very good practice, your layout will screw up if the user resie the Frame
Was This Post Helpful? 4
  • +
  • -

#9 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Re: Getting the text in one line

Posted 22 February 2012 - 05:40 AM

View Postpbl, on 21 February 2012 - 11:07 PM, said:

So what do you want your JLabel to look like ? Your post is quite obscur.

Quote

I want "Label with Text and Icon" below.

below what ? The Text betwen "" or the label itself ?


Sorry for the confusion! I want "Label with text and icon" below "Label with text".

View Postdeprosun, on 22 February 2012 - 07:35 AM, said:

View Postpbl, on 21 February 2012 - 11:07 PM, said:

So what do you want your JLabel to look like ? Your post is quite obscur.

Quote

I want "Label with Text and Icon" below.

below what ? The Text betwen "" or the label itself ?


Sorry for the confusion! I want "Label with text and icon" below "Label with text".


Yes I want label 2 below label 1
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,479
  • Joined: 27-December 08

Re: Getting the text in one line

Posted 22 February 2012 - 06:08 AM

pbl's Box suggestion is the one you will want to use.
Was This Post Helpful? 2
  • +
  • -

#11 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Re: Getting the text in one line

Posted 22 February 2012 - 05:54 PM

View Postpbl, on 21 February 2012 - 11:23 PM, said:

FlowLayout by default stores all the possible components in one line

http://docs.oracle.c...FlowLayout.html

You will have to use another layout
May be Box b = Box.createVerticalBox()
and add your labels to that Box b


So will i be adding that box to the container?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Getting the text in one line

Posted 22 February 2012 - 10:22 PM

Thread continued in

http://www.dreaminco...vived-question/

Please avoid double posting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1