jtextfield query

Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

jtextfield query

 
0
  #1
Jan 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: jtextfield query

 
0
  #2
Jan 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jtextfield query

 
0
  #3
Jan 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: jtextfield query

 
0
  #4
Jan 9th, 2008
hmmm, it would appear that it gets it source from an HTML format? Not really what I had in mind. My system is producing strings and I'd liked them to be placed on a canvas. I've attempted to add JLabels to it and, it works but doesnt look that nice. Also adding JLabels isn't that safe.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jtextfield query

 
0
  #5
Jan 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: jtextfield query

 
0
  #6
Jan 9th, 2008
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)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jtextfield query

 
0
  #7
Jan 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: jtextfield query

 
0
  #8
Jan 10th, 2008
ok I've gone down the route of making it a JTextPane, I'm lost in how to append text to the pane?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jtextfield query

 
0
  #9
Jan 10th, 2008
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
  1. private JTextPane createTextPane() {
  2. String[] initString =
  3. { "This is an editable JTextPane, ", //regular
  4. "another ", //italic
  5. "styled ", //bold
  6. "text ", //small
  7. "component, ", //large
  8. "which supports embedded components..." + newline,//regular
  9. " " + newline, //button
  10. "...and embedded icons..." + newline, //regular
  11. " ", //icon
  12. newline + "JTextPane is a subclass of JEditorPane that " +
  13. "uses a StyledEditorKit and StyledDocument, and provides " +
  14. "cover methods for interacting with those objects."
  15. };
  16.  
  17. String[] initStyles =
  18. { "regular", "italic", "bold", "small", "large",
  19. "regular", "button", "regular", "icon",
  20. "regular"
  21. };
  22.  
  23. JTextPane textPane = new JTextPane();
  24. StyledDocument doc = textPane.getStyledDocument();
  25. addStylesToDocument(doc);
  26.  
  27. try {
  28. for (int i=0; i < initString.length; i++) {
  29. doc.insertString(doc.getLength(), initString[i],
  30. doc.getStyle(initStyles[i]));
  31. }
  32. } catch (BadLocationException ble) {
  33. System.err.println("Couldn't insert initial text into text pane.");
  34. }
  35.  
  36. return textPane;
  37. }
  38.  
  39. protected void addStylesToDocument(StyledDocument doc) {
  40. //Initialize some styles.
  41. Style def = StyleContext.getDefaultStyleContext().
  42. getStyle(StyleContext.DEFAULT_STYLE);
  43.  
  44. Style regular = doc.addStyle("regular", def);
  45. StyleConstants.setFontFamily(def, "SansSerif");
  46.  
  47. Style s = doc.addStyle("italic", regular);
  48. StyleConstants.setItalic(s, true);
  49.  
  50. s = doc.addStyle("bold", regular);
  51. StyleConstants.setBold(s, true);
  52.  
  53. s = doc.addStyle("small", regular);
  54. StyleConstants.setFontSize(s, 10);
  55.  
  56. s = doc.addStyle("large", regular);
  57. StyleConstants.setFontSize(s, 16);
  58.  
  59. s = doc.addStyle("icon", regular);
  60. StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
  61. ImageIcon pigIcon = createImageIcon("images/Pig.gif",
  62. "a cute pig");
  63. if (pigIcon != null) {
  64. StyleConstants.setIcon(s, pigIcon);
  65. }
  66.  
  67. s = doc.addStyle("button", regular);
  68. StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
  69. ImageIcon soundIcon = createImageIcon("images/sound.gif",
  70. "sound icon");
  71. JButton button = new JButton();
  72. if (soundIcon != null) {
  73. button.setIcon(soundIcon);
  74. } else {
  75. button.setText("BEEP");
  76. }
  77. button.setCursor(Cursor.getDefaultCursor());
  78. button.setMargin(new Insets(0,0,0,0));
  79. button.setActionCommand(buttonString);
  80. button.addActionListener(this);
  81. StyleConstants.setComponent(s, button);
  82. }
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.
Last edited by Ezzaral; Jan 10th, 2008 at 6:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC