To get a color text on the TextArea

ehmaster

Mitglied
Hello dear Java community,


i have a little problem again. So i have simply actionperformed for my buttons and want to write text on the TextArea which i have in my Java swing App. It is working but when i want to get some parts of my text a color then this color code do not works.
Please if someone knows how to solve this problem i will be very grateful.:-(




Thanks!!!!




Java:
addBt.addActionListener(new ActionListener() {


			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				
				
				
				


				String text = "\t" + "Information " + " \n" + "Password is: "
						+ " " + "" + "5555" + "    " + "/   " + "Username is:"
						+ " " + " " + "ehmaster" + "" + "\n";


				fireDetailEvent(new DetailEvent(this, text));


			}


		});
 
Danke Klaus!!!!!
i deleted because of it was not working. I read how to get color when you have actionperformed. I have JTextPane but i do not know how to use it. For example how can i get color just on this text part not whole text which i have in this code. I think this code is solution for my problem but i do not know how to use it. If you know and have an idea please show me(((

Like this :

StyledDocument doc = textPane.getStyledDocument();SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, myColor);
doc
.setParagraphAttributes(0, doc.getLength(), attrs, false);
textPane
.setDocument(doc);
 
I've had some time and tried to create a simple way to use TextPane. I haven't tested it fully, though. So there could be some bugs. Anyway here it is:

Java:
import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;


public class JColoredTextPane extends JTextPane
{
	private static final long serialVersionUID = 4505277032433992163L;
	private static final String LINE_BREAK = "\n";
	private static final String SPACE = " ";
	
	private Color textColor;
	private boolean justColorize;
	
	public JColoredTextPane()
	{
		this(Color.BLACK);
	}
	
	public JColoredTextPane(Color textColor)
	{
		this.textColor = textColor;
		justColorize = false;
	}
	
	
	public void setTextColor(Color textColor)
	{
		this.textColor = textColor;
	}
	
	public Color getTextColor()
	{
		return textColor;
	}
	
	
	public void append(String text)
	{
		append(text, textColor);
	}
	
	public void append(String text, Color color)
	{
		replace(text, 0, 0, color, true);
	}
	
	public void replace(String text, int start)
	{
		if(text == null)
			return;
		
		replace(text, start, textColor);
	}
	
	public void replace(String text, int start, Color color)
	{
		int end = start + text.length();
		replace(text, start, end, color);
	}
	
	public void replace(String text, int start, int end)
	{
		replace(text, start, end, textColor);
	}
	
	public void replace(String text, int start, int end, Color color)
	{
		replace(text, start, end, color, false);
	}
	
	private void replace(String text, int start, int end, Color color, boolean append)
	{
		justColorize = false;
		
		AttributeSet aset = getNewAttributeSet(color);
		
		if(append)
		{
			int len = getDocument().getLength();
			setCaretPosition(len);
		}
		else
		{
			setCaretPosition(start);
			moveCaretPosition(end);
		}
		
		setCharacterAttributes(aset, !append);
		replaceSelection(text);
	}
	
	public void colorize(int start, int end, Color newTextColor)
	{
		justColorize = true;
		
		AttributeSet aset = getNewAttributeSet(newTextColor);
		
		setCaretPosition(start);
		moveCaretPosition(end);
		setCharacterAttributes(aset, true);
		
		int len = getDocument().getLength();
		setCaretPosition(len);
	}
	
	public void appendLineBreak()
	{
		append(LINE_BREAK);
	}
	
	public void appendSpace()
	{
		append(SPACE);
	}
	
	
	@Override
	public void setCharacterAttributes(AttributeSet attr, boolean replace)
	{
		if(!justColorize)
		{
			MutableAttributeSet inputAttributes = getInputAttributes();
			if(replace)
			{
				inputAttributes.removeAttributes(inputAttributes);
			}
			inputAttributes.addAttributes(attr);
		}
		else
		{
			int p0 = getSelectionStart();
			int p1 = getSelectionEnd();
			StyledDocument doc = getStyledDocument();
			doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
		}
	}
	
	private AttributeSet getNewAttributeSet(Color textColor)
	{
		StyleContext sc = StyleContext.getDefaultStyleContext();
		AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, textColor);
		
		aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
		aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
		
		return aset;
	}
}

You can use it like this:
Java:
public static void main(String[] args)
{
	SwingUtilities.invokeLater(new Runnable() {
		
		@Override
		public void run()
		{
			JFrame frame = new JFrame("JColoredTextArea Frame");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			JPanel panel = new JPanel();
			
			//A few examples
			JColoredTextPane c = new JColoredTextPane();
			
			//You could just colorize certain characters
			c.append("Just some normal boring black text");
			c.appendLineBreak();
			c.append("And more in another line");
			c.appendLineBreak();
			
			c.colorize(1, 7, Color.MAGENTA);
			
			
			//Or you could add text in a specific color
			c.append("My Text Green", Color.GREEN);
			c.appendLineBreak();
			c.append("Some Orange Text", Color.ORANGE);
			c.appendSpace();
			c.append("Other Text Blue", Color.BLUE);
			c.appendLineBreak();
			
			
			//Or change the default text color
			c.setTextColor(Color.GRAY);
			
			c.append("From now on");
			c.appendLineBreak();
			c.append("every text is GRAY until");
			c.appendLineBreak();
			
			c.setTextColor(Color.GREEN);
			
			c.append("The default color");
			c.appendLineBreak();
			c.append("is changed again.");
			
			
			panel.add(c);
			frame.add(panel);
			
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
		}
	});
}
 

Zurück
Oben