Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Creating an advanced text log...  (Read 1243 times)
0 Members and 1 Guest are viewing this topic.
Offline 8ballj

Senior Newbie





« Posted 2008-06-17 21:57:44 »

Hello everyone, I am trying to create a text log that updates throughout gameplay with meaningful information.  I've already done this before using a JTextArea, but this time I want the log to display different fonts/colors/sizes, not just one font for the whole thing.  I've spent hours trying to figure out how to do this with the JTextPane or JEditorPane but I am getting confused and frustrated trying to figure out if I need to have a seperate document etc...  is there a simple way to do this???

Thanks!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-06-17 22:19:58 »

This should get you started:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      JTextPane textPane = new JTextPane();
      StyledDocument doc = textPane.getStyledDocument();

      Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

      Style style1 = doc.addStyle("someCode", regular);
      StyleConstants.setFontFamily(style1, "courier");
      StyleConstants.setFontSize(style1, 12);
      StyleConstants.setBold(style1, true);

      Style style2 = doc.addStyle("plainText", style1); // inherits from style1
     StyleConstants.setFontFamily(style2, "arial");
      StyleConstants.setFontSize(style2, 12);
      StyleConstants.setItalic(style2, true);


      doc.insertString(0, "hello world");
      doc.setCharacterAttributes(0, 11, doc.getStyle("plainText"), false);
//or  doc.setCharacterAttributes(0, 11, style1, false);
     doc.setCharacterAttributes(3, 5, doc.getStyle("someCode"), false);
//or  doc.setCharacterAttributes(3, 5, style2, false);


"hello world" will be bold
"lo wo" will be both bold and italic


Be sure to run ALL CODE from the EDT, because JTextPane is VERY unreliable when accessed from other threads.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline 8ballj

Senior Newbie





« Reply #2 - Posted 2008-06-17 23:58:20 »

Thanks, this will work fine!  If the document object gets very large (like 1000+ lines of text), will the program slow down noticeably each time i add text and reset the document in the textPane?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-06-17 23:59:52 »

Try persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline woogley
« Reply #4 - Posted 2008-06-18 03:15:41 »

For this case I would actually just set up JList (or something similiar) to take HTML code. Then you can use simple <b> tags to bold things out, set colors with the <font> tag.. bla bla bla

You have to remember that the parser is compatible up to HTML 3.2, hence having to use <font> to set the color..

code snippet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
Stack logData;
JList log;
JScrollPane logScroll;
public Constructor() {
  logData = new Stack();
  log = new JList();
  log.setEnabled(false);
  logScroll = new JScrollPane(log);
}
public void log(String msg) {
  logData.push(msg);
  log.setListData(logData);
  Rectangle r = log.getBounds();
  if (r.height >= logScroll.getBounds().height) {
    /* scrolls the log down to the newest item */
    logScroll.getViewport().setViewPosition(new Point(0,r.height));
  }
}
public void clear() {
  logData.clear();
  log.setListData(logData);
}


Then you can just log HTML code..
1  
log("<html><font color=blue>This will be blue");


That way you don't have to worry about using those Style objects and such..

The above code is taken from a template GUI I use whenever I make a server-client app. The result looks decent:

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (52 views)
2014-04-15 18:08:23

BurntPizza (49 views)
2014-04-15 03:46:01

UprightPath (64 views)
2014-04-14 17:39:50

UprightPath (46 views)
2014-04-14 17:35:47

Porlus (63 views)
2014-04-14 15:48:38

tom_mai78101 (88 views)
2014-04-10 04:04:31

BurntPizza (147 views)
2014-04-08 23:06:04

tom_mai78101 (244 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!