| | |
jtextfield query
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
Hey guys I've got a JText field set up and i append strings to the JTextField but im wondering how to invidually set the color of each string that i append? I've taken a look at the JLabel class and I did come up with the idea that I could just make a new JLabel and set the color and string and then paint that onto a JTextField. However this doesnt appear to work. Nothing gets display if I attempt to do it this way.
Is there a work around? I know the JTextField has its own setForegroundColor(); but this sets it for the whole text thats present.
Is there a work around? I know the JTextField has its own setForegroundColor(); but this sets it for the whole text thats present.
AFAIK, you can't. Use a JLabel or JTextArea.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Yes, JTextField only supports single-styled text. For styled text you need to use JEditorPane or JTextPane http://java.sun.com/docs/books/tutor...ditorpane.html
or HTML markup within a JLabel.
or HTML markup within a JLabel.
I'm not quite following you. You mentioned placing them in a JTextField, but here you mention placing them on a canvas. Which is the case? One involves styled-text within an editing component, whereas painting them on a canvas evokes an expectation of custom 2D graphics rendering. Also, the comment about JLabels not being safe is a bit unclear.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
At the minute I've got a JTextField which I write Strings to (my system produces String messages which I wish to show to the user via my gui window).
Certain messages come through and at the minute they are all the same color. I wish to seperate out the different messages and make them a specfic color accordingly.
I've had a look at the JTextPane and EditorPane they both from what I understand construct themselves via a HTML markup document ? Please correct me if I'm wrong.
I read somewhere that its bad to place a label in a JTextPane / EditorPane (Will dig it out and post it)
Certain messages come through and at the minute they are all the same color. I wish to seperate out the different messages and make them a specfic color accordingly.
I've had a look at the JTextPane and EditorPane they both from what I understand construct themselves via a HTML markup document ? Please correct me if I'm wrong.
I read somewhere that its bad to place a label in a JTextPane / EditorPane (Will dig it out and post it)
You do not have to use HTML markup to created styled text in JTextPane. In addition to the tutorial link I posted above, perhaps these examples will help:
http://www.exampledepot.com/egs/java...iteWords2.html
As far as the JLabel, you're right, you don't place it in a JTextPane - but then that wasn't really the nature of the suggestion. The intent was that you could use a JLabel in place of JTextField or JTextPane in your container and use HTML markup to control the style and formatting of the displayed text.
http://www.exampledepot.com/egs/java...iteWords2.html
As far as the JLabel, you're right, you don't place it in a JTextPane - but then that wasn't really the nature of the suggestion. The intent was that you could use a JLabel in place of JTextField or JTextPane in your container and use HTML markup to control the style and formatting of the displayed text.
The TextSamplerDemo from Sun has some example code on adding styled text to a JTextPane. It's a bit different than working with JTextField because the text pane utilizes a styled document. Since there is a lot of clutter from other text component code in that demo, I'll paste the two relevant methods here They are basically concatenating the various strings from their array to end of the document with the desired style with the insertString() method.
Hope that helps a bit.
java Syntax (Toggle Plain Text)
private JTextPane createTextPane() { String[] initString = { "This is an editable JTextPane, ", //regular "another ", //italic "styled ", //bold "text ", //small "component, ", //large "which supports embedded components..." + newline,//regular " " + newline, //button "...and embedded icons..." + newline, //regular " ", //icon newline + "JTextPane is a subclass of JEditorPane that " + "uses a StyledEditorKit and StyledDocument, and provides " + "cover methods for interacting with those objects." }; String[] initStyles = { "regular", "italic", "bold", "small", "large", "regular", "button", "regular", "icon", "regular" }; JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); addStylesToDocument(doc); try { for (int i=0; i < initString.length; i++) { doc.insertString(doc.getLength(), initString[i], doc.getStyle(initStyles[i])); } } catch (BadLocationException ble) { System.err.println("Couldn't insert initial text into text pane."); } return textPane; } protected void addStylesToDocument(StyledDocument doc) { //Initialize some styles. Style def = StyleContext.getDefaultStyleContext(). getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "SansSerif"); Style s = doc.addStyle("italic", regular); StyleConstants.setItalic(s, true); s = doc.addStyle("bold", regular); StyleConstants.setBold(s, true); s = doc.addStyle("small", regular); StyleConstants.setFontSize(s, 10); s = doc.addStyle("large", regular); StyleConstants.setFontSize(s, 16); s = doc.addStyle("icon", regular); StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER); ImageIcon pigIcon = createImageIcon("images/Pig.gif", "a cute pig"); if (pigIcon != null) { StyleConstants.setIcon(s, pigIcon); } s = doc.addStyle("button", regular); StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER); ImageIcon soundIcon = createImageIcon("images/sound.gif", "sound icon"); JButton button = new JButton(); if (soundIcon != null) { button.setIcon(soundIcon); } else { button.setText("BEEP"); } button.setCursor(Cursor.getDefaultCursor()); button.setMargin(new Insets(0,0,0,0)); button.setActionCommand(buttonString); button.addActionListener(this); StyleConstants.setComponent(s, button); }
Hope that helps a bit.
Last edited by Ezzaral; Jan 10th, 2008 at 6:42 pm.
![]() |
Similar Threads
- Needs Brainstorming!!! (Java)
- /Code: Hello, Please Help Me! I need help in java Project (Java)
- parent/child (Java)
- Applet and i/o (Java)
Other Threads in the Java Forum
- Previous Thread: Homework help
- Next Thread: Parser
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






