Swing: non input text field - java

I'm new in Swing, and don't want to learn it. I just want to write a simple frame divided into two equal parts: top and bottom. The top part of frame should be a simple immutable text field.
Problem: As I understand, to show text with beautiful font I should use JTextPane. But JTextPane:
Doesn't support vertical text alignment; (I haven't got any desire to write something like that)
I don't know how to switch off editing.
Quedtion I believe there is a simpler solution for my purpose. Is there?

The top part of frame should be a simple immutable text field.
Use a JLabel. It supports HTML which might help with your formatting.
I don't know how to switch off editing.
setEditable( false );

Related

Dynamical Font-size in TextArea - JavaFX

I am currently trying to get into JavaFx and started writing a program similar to Microsoft Word or Pages on OS X. I had to realize that the TextArea is really limited when it comes to style, so I was wondering if there is a component that is editable (as in you can write on it) and is able to change font size/color dynamically (size and color are defined with a choicebox on the UI.
I read a little but about binding style-types or setting css rules, but as far as I know there is no way to access Java components (choicebox) or even variables from the css style sheet, so it wouldn't be dynamical.
What I mean by dynamically change font size/color:
Type a character into the TextArea.
Double the font size.
Type another character, that now appears twice as big as the first one, that is still displayed in its original size.
I'm having a hard time explaining myself, if you have any questions, please ask and I'll try going into further detail. Thanks
EDIT: Something that might be important is the fact that I am using Scene builder to create my UI.
EDIT 2: Could I cheat my way around it using a Highlighter with the same color as my background?

Format an editable textarea

At the moment I try to find a method to create a TextArea that I can edit and simply (without cheating) apply syntax highlighting to. Is that possible without the need of a custom Component? I already managed to format a text using a JEditorPane, but I am not sure how to implement that the text is dynamicly highlighted... And that efficiently. Is that possibly without an enormous amount of coding?
You can't use a JTextArea since it does not support text attributes.
Instead you can use a JTextPane. Read the section from the Swing tutorial on Text Component Features for more information and examples.

How to give different - 2 color to text written in JTextArea?

I want to change color of text written in JTextArea. For e.g. I am pasting Java code in JTextArea.
I want to give the different-2 color to Java keywords, variables and classes written in code.
Is it possible?
See Rob Camick's Message Console. It seems perfect for this use case.
Actually '2 color' might not be enough for this.
See How to Use Editor Panes and Text Panes for more details on the formatted text components that might replace JTextArea.
it's not possible
if you want to set different colors in the same area, i think JTextPane could help you
i'm not sure how to use that, but i found this
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

Java coloring characters GUI

I am making a GUI dictionary program and have an input of letters that need to be highlighted in each words that is display. I was wondering how to do this, it doesn't seem very hard to color my entire JTextArea, but coloring only certain characters seems to be a little more difficult. I have read about JTextPane where I can use styled fonts, but even that doesn't seem straightforward. I need to parse every dictionary word's character and then change that characters color.
Yes, you'd want to use JEditorPane or JTextPane to do this sort of thing. You can either use styles, as you've seen, or you could create the contents as HTML and display that, which would be a lot simpler, if less flexible.
The relevant section of the Java Tutorial starts here.
You can add your own Highlights. See DefaultHighlighter and HighlightPainter.
See the http://java-sl.com/tip_vertical_selection.html example of custom highlights using. You can define any desired colors.

Scroll through JLabel held in JScrollPane

I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.

Categories

Resources