-2

My task is to create a Java char client/server application. I always have huge problems with GUI in java.

I'm currently struggling to create a simple chat log that will be able to display both colored text (specifically administrator users should appear red) and GUI elements (button to download a file that has been sent).

I have tried lots of things including HTML, but everything has a flaw.

For the messages, my latest solution are s filled with s of different color and it's all contained in . It's the best solution so far, but it's still really bad.

  1. JPanels are trying to fit in centre both vertically and horizontally. If there's only one message, it's in the middle of the log.
  2. Text in JPanels cannot be highlighted - so it cannot be copied out.

screenshot of the chat window

Is there any XXXPanel that has been designed for purpose of displaying formated text? The panel with HTML did not accept either CSS or <font> tag attributes - so I doubt it had the capability of being colored.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 1
    http://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea might help – Harry Blargle May 22 '14 at 19:03
  • Looks promising - however my attempt with JTextPane led to editable text. – Tomáš Zato May 22 '14 at 19:13
  • If only there was a method like `setEditable(false)` to work around that. And if there only was a thing like `javadoc` where you can find those methods... We're here to help, but we're not here to hold your hand. – Kayaman May 22 '14 at 19:15
  • When I use the solution from the proposed question and I `setEditable(false)` no text appears (otherwise it works the same my old solution did. But it seems that I can toggle setEditable for the moment when I'm adding the text. – Tomáš Zato May 22 '14 at 20:12

1 Answers1

1

Use a JTextPane in a JScrollPane with HTML formatting/coloring. It works perfectly (especially for something simple like this), unless you configure it wrong (such as not displaying HTML).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Thaks for telling me how to configure it *right*. Especially when it seems I have already configured it wrong... – Tomáš Zato May 22 '14 at 19:07
  • If I remember correctly, you have to explicitly configure it *not* to render HTML. So if you can't get it to work, you might want to read the Swing tutorial again. You also don't seem to know about `Layouts` based on your question (saying that the text is centered, as it is normally in a `FlowLayout`). – Kayaman May 22 '14 at 19:11
  • Might I remind you, that people ask questions because they don't know something? – Tomáš Zato May 22 '14 at 19:12
  • May I point you to my answer where I told you exactly what to use? Way to be thankful for other people helping you. – Kayaman May 22 '14 at 19:13
  • 1
    I told you the most straightforward way to do what you wanted. You replied passive aggressively to me. I don't get money from helping people here, I get worthless points, and you show your thanks by scowling at me for not writing the code for you. Rethink your attitude please. – Kayaman May 22 '14 at 19:21
  • I'm sorry. I was just dissapointed - My question could be interpreted like "*I want a colored text. JPanels do not work for me. Neither did the HTML formated panel*". Your answer was just "*Use HTML formated panel*". – Tomáš Zato May 22 '14 at 19:30
  • Well, I did forget that you have to start with the `` tag to make sure it's considered html, and set the content type to `text/html` (not sure if that was the default or not). Here's a question that contains exactly what you're looking for http://stackoverflow.com/questions/9071389/setting-jtextpane-to-content-type-html-and-using-string-builders – Kayaman May 22 '14 at 19:38
  • It appears that I have failed to close the `` tag. – Tomáš Zato May 22 '14 at 19:57