Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default Need help with JPanels and JLabels

    Hey,

    Just a quick question about working with JLabels inside JPanels. I have a 2D array of JPanels (called squares) and would like to put a JLabel with text in some of them. However, when the text is too long to fit in one line of the box, it cuts off. I tried to put in "\n" in order to create a new line but when I ran the program java seemed to completely ignore it. Does anyone know how to either create new lines inside a JLabel or to make it start a new line once it has reached the edge of its display area? I have posted one example of a method in my program below just to clarify. Any help/advice is much appreciated!

    public void display(){
    squares[2][8].removeAll();
    playerMoney = new JLabel(playerOne.getName() + " has: $" + playerOne.getCash() + "\n" + playerTwo.getName() + " has: $" + playerTwo.getCash());
    squares[2][8].add(playerMoney);
    squares[2][8].validate();
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You can use HTML is a JLabel. Then you can use the "br" tag instead of a "\n" to control where the line splits.

    Otherwise you could use a JTextArea which supports wrapping. You can play with the text area properties to make it look like a label.

  3. #3
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  2. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  3. can i have a JList of Jlabels?
    By chayan in forum AWT / Swing
    Replies: 3
    Last Post: 07-04-2010, 12:19 AM
  4. Overlapping JLabels
    By techbossmb in forum AWT / Swing
    Replies: 3
    Last Post: 09-21-2009, 03:21 PM
  5. problem with JLabels
    By geork in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 02:30 PM

Tags for this Thread

Posting Permissions

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