Zeilenumbruch im Jlabel

  • Themenstarter Themenstarter ash34
  • Beginndatum Beginndatum
A

ash34

Gast
Hy,

ich würde gerne einen Zeilenumbruch in einem JLabel vornehmen. Leider klappen die sonst üblichen HTML Varianten wie

Java:
JLabel label = new JLabel("<html><body>Zeile1<br>Zeile2</body></html>"

bei mir nicht.

Vielleicht noch als Hintergrund:

Ich lese den Text aus dem JLabel mit
Code:
getText()
, füge dann etwas hinzu und will den hinzugefügten Text in eine neue Zeile schreiben...

Hat jemand eine Idee?

VG
 
Und warum sollte die HTML Formatierung nicht gehen? Alten Text auslesen und vor "</body></html>" deinen neuen Text einfügen?
 
Naja, selbst ohne hinzufügen, nur wenn ich den Text des Labels setzen will, klappt das schon nicht...

Java:
JLabel box = new JLabel("<html><body>Textzeile1<br>Textzeile2</body></html>");
 
Klappt bei mir hiermit:
Java:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class HtmlLabel {
	public static void main(final String... args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				final JFrame f = new JFrame();
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.add(new JLabel("<html><body>Textzeile1<br>Textzeile2</body></html>"));
				f.pack();
				f.setLocationRelativeTo(null);
				f.setVisible(true);
			}
		});
	}
}
Bei dir nicht?
 
Hmm..das funktioniert...

Hier mal meine Klasse:

Java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 
 * BoxPanel contains all boxes that can be set out of the upper list of boxes in the frame
 * @author florian
 *
 */

public class BoxPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	
		protected GUI gui;
		
		private List<JLabel> boxList;
	    private Color color;
	    
	    private final int standardWidth;
	    private final int standardHeight;
	    private final int fourBitWidth;
	    private final int eightBitWidth;
	    private final int sixteenBitWidth;
	    private final int twentyfourBitWidth;
	    private final int thirtytwoBitWidth;
	    private final int fourtyeightBitWidth;
        
	        /**
	         * Constructor
	         * 
	         */
	        
	public BoxPanel(GUI gui) {
		
		this.gui = gui;
		this.color = Color.WHITE;

	    boxList = new ArrayList<JLabel>();
	    
	    standardWidth = (int) gui.frameDim.getWidth()/15;
	    standardHeight = (int) gui.frameDim.getHeight()/19;
	    fourBitWidth = standardWidth;
	    eightBitWidth = (int) (fourBitWidth*1.5);
	    sixteenBitWidth = (int) (eightBitWidth*1.5);
	    twentyfourBitWidth = (int) ((sixteenBitWidth + eightBitWidth)*0.75);
	    thirtytwoBitWidth = (int) (sixteenBitWidth*1.5);
	    fourtyeightBitWidth = (int) ((thirtytwoBitWidth + sixteenBitWidth)*0.75);

	}
	               
	public void fillBoxPanel(){
	    	
		String[] boxText = {"Checksum ","Code ","Data ","HdrLen ","HdrChecksum ",
							"FOS ","Identifier ","TotalLength ",
							"Length/Type ","Options ","Pad ","PacketID ",
							"Protocol ","Präambel ","SeqNumr ",
							"SourAddr(Eth) ","SourAddr(IP) ","TarAddr(Eth) ",
							"TarAddr(IP) ","TOS ","TTL ","Type ","Vers "};
		
	    	
		int n = 0;						// horizontal gap between boxes
		int m = 40; 					// vertical gap between boxes
		int rowCount = 12; 				// amount of boxes in one row 
		int x = 45;						// vertical gap for the first box (related to (0,0))
		int y = 5;						// horizontal gap for the first box (related to (0,0))
		int width = standardWidth;		// width of a normal box
		int height = standardHeight;	// height of a normal box

//		creating boxes, setting color and text to each of the boxes
	    		
		for(int i=0; i<boxText.length; i++){
	    			
			if(i < rowCount){					
				JLabel box = new JLabel("<html><body>Textzeile1<br>Textzeile2</body></html>");
				box.setName(boxText[i]);
				box.setSize(width, height);
				box.setLocation(new Point(x+n,y));
				box.setForeground(color);
				box.setFont(new Font("Dialog", 0, 10));
				this.boxList.add(box);
				n = width*(i+1)+(i+1)*4;
	   			}
	    			
			if(i == rowCount){
	    		n=0;
	    		JLabel box = new JLabel(boxText[i]);
	    		box.setName(boxText[i]);
				box.setSize(width, height);
				box.setLocation(new Point(x+n,y+m));
				box.setForeground(color);
				box.setFont(new Font("Dialog", 0, 10));
				this.boxList.add(box);
	    		n = width*(i-11)+(i)*4;
	    		}
	    			
	    	if(i > rowCount){
	    		JLabel box = new JLabel(boxText[i]);
	    		box.setName(boxText[i]);
				box.setSize(width, height);
				box.setLocation(new Point(x+n,y+m));
				box.setForeground(color);
				box.setFont(new Font("Dialog", 0, 10));
				this.boxList.add(box);
	    		n = width*(i-11)+(i)*4;
	    		}
	    }
	}
	
	public int getStandardHeight(){
		return this.standardHeight;
	}
	        
	public List<JLabel> getBoxList(){
	  	return this.boxList;
	}
	
	public int getFourBitWidth(){
		return this.fourBitWidth;
	}
	
	public int getEightBitWidth(){
		return this.eightBitWidth;
	}
	
	public int getSixteenBitWidth(){
		return this.sixteenBitWidth;
	}
	
	public int getTwentyFourBitWidth(){
		return this.twentyfourBitWidth;
	}
	
	public int getThirtytwoBitWidth(){
		return this.thirtytwoBitWidth;
	}
	
	public int getFourtyeightBitWidth(){
		return this.fourtyeightBitWidth;
	}
	        
	public void paintComponent(Graphics g) {
	   super.paintComponent(g);
	   Graphics2D g2 = (Graphics2D)g.create();
	   if(!boxList.isEmpty()){
		   for(int i=0; i<boxList.size(); i++){
			   JLabel label = boxList.get(i);
		
		   g2.setColor(Color.BLACK);
		   g2.drawRect(label.getX(), label.getY(), label.getWidth(), label.getHeight());
		   g2.setColor(label.getForeground());
		   g2.fillRect(label.getX(), label.getY(), label.getWidth(), label.getHeight());
		   g.setFont(label.getFont());
		   g.drawString(label.getText(), label.getX()+2, label.getY()+11);
//		   g2.dispose();
		   }
	   }
	}
}

Komisch..eigentlich mach ich das ja genauso..
 
Dieses Thema gam es vor einiger Zeit schon mal hier im Forum:

Java:
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
 
public class JLabelTest {
 
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(2,2));
        JLabel lab1 = new JLabel ("1. 2. 3.");
        JLabel lab2 = new JLabel ("<html>1. Labelzeile<br>2. Labelzeile<br>3. Labelzeile</html>");
        JLabel lab3 = new JLabel ("<html>Zeile1<br>Zeile2<br>Zeile3<br>Zeile4<br>Zeile5<html>");
        frame.add(lab1);
        frame.add(lab2);
        frame.add(lab3);
        
        frame.setVisible(true);
    }
}

Pentalon
 
Hi,

soweit ich das gerade erkenne, hast Du nur in dieser Bedingung einen HTML Text:

Java:
if(i < rowCount){                   
                JLabel box = new JLabel("<html><body>Textzeile1<br>Textzeile2</body></html>");
                box.setName(boxText[i]);
                box.setSize(width, height);
                box.setLocation(new Point(x+n,y));
                box.setForeground(color);
                box.setFont(new Font("Dialog", 0, 10));
                this.boxList.add(box);
                n = width*(i+1)+(i+1)*4;
                }

ansonsten sind es nur "normale" Strings.

Hier wird der Text eines JLabels in mehreren Zeilen untereinander Zentriert.

Gruß

tschero
 
Zuletzt bearbeitet:
Hi,

soweit ich das gerade erkenne, hast Du nur in dieser Bedingung einen HTML Text:

...

ansonsten sind es nur "normale" Strings.

Das ist mir bewusst und auch beabsichtigt. Wenn es in diesem Fall klappt, übernehm ich es für die anderen Fälle 😉

welche Farbe haben die Label (Hintergrund) und der Text darauf?

Die Initialfarbe ist weiß, beim draufklicken werden die Labels Gelb (Foreground). Der Background ist nicht gesetzt..

Könnte es vielleicht dran liegen, dass ich die JLabel nicht zum Panel hinzufüge, sondern sie in eine Liste schreibe und sie dann zeichne?

EDIT: Genau daran liegt es... 😱
Kann ich das irgendwie umgehen..??
 
Zuletzt bearbeitet von einem Moderator:
[STRIKE]Daran hatte ich auch schon gedacht..
Das Problem ist nur, dass dann sämtliche Parameter (Größe, Position, etc) verloren gehen...[/STRIKE]

Hat sich soeben erledigt 😳

Danke an alle für die Hilfe :toll:
 
Wenn Du eine Liste von JLabels hast, geht Dir doch kein Parameter verloren. ???:L

z.B.

Java:
private LinkedList<JLabel> labelList = new LinkedList<JLabel>();
labelList.add("eines Deiner JLabel");

Was genau geht Dir denn hierbei verloren?


EDIT: zu spät
 

Zurück
Oben