So I've been developing web applications for 10 years, but have fond memories of working on small swing applications that related to an introductory programming curriculum I was paid to help with before that. I generally like building swing/java UI especially with a decent UI builder. However, in the intervening time I've become very accustomed to providing clear, red error messages of variable length and number when the data on a form is invalid. Sometimes with icons, sometimes with markers on the field etc. Very common stuff that's easy to do in the web world, and is really helpful and improves usability.
Today I was trying to find a way to display a nice list of error messages at the top or bottom of my swing form, icons and field markers can come later, but for now I just want a nice variable sized list of things that need to be corrected, in a red color.
At first blush this seems like it would be easy, but..
JLabel doesn't wrap, so long text is hidden, or you have to make the form ultra wide
JText Area has the same issue unless you define a number of columns, and I want to fill the space available. A set number of columns won't resize.
JTextPane wraps but as far as I can tell it won't turn red unless you get into the whole way over complicated StyledDocument bit. I don't want to do anything more complicated than change the text color so this seems like overkill.
There's also the minor issue of pushing outside the window space, and one can call pack(), but Id like to only increase the height of the form and wrap the text not change the dialog to the width of the message (creating lots of strained layout and dead space if a single long message doubles the width)
I also don't really want to have scroll bars either for the message area or the form as a whole.
This surely is a wheel that's already been invented, so what's the right way to show a list of red (but otherwise unformatted) error messages in a swing form?
Label doesn't wrap, so long text is hidden, or you have to make the
form ultra wide
use HTML
JText Area has the same issue unless you define a number of columns,
and I want to fill the space available. A set number of columns won't
resize.
use word and line wrap
A set number of columns won't resize. and together with "I also don't really want to have scroll bars"
put JTextArea to JScrollPane, to disable JScrollBars, set null for Borders from JScrollPane
JTextPane wraps but as far as I can tell it won't turn red unless you
get into the whole way over complicated StyledDocument bit. I don't
want to do anything more complicated than change the text color so
this seems like overkill.
use HighLighter, but nothing clear from posted description about your issue with StyledDocument
Related
Please read before labelling this as a duplicate.
I am creating an application that calculates how fast a person can type by calculating WPM and CPM. However I have hit a snag with the UI as I found out that you cannot really highlight individual strings in a TextArea. My goal is to compare what the user is typing to the random text that is generated by having it so that the text is being dynamically coloured or highlighted as the user is typing.
See http://10fastfingers.com/typing-test/english to get an idea of what I mean
I recently read the following post Highlighting Strings in JavaFX TextArea
I was trying to achieve the same goal of highlighting individual strings inside a javaFX TextArea until I realised that it pretty much is not possible. So I looked into TextFlow which does allow me to edit individual strings. The problem with TextFlow is that all 200 of the generated words would have to appear at once which is not what I want. With a TextArea not all the text has to be displayed at once.
This is what I have so far just so that you can further get an idea of where I am heading with this.
I am trying to make a "Oregon Trail" like game with JAVA for my Computer Science class. It's all going well so far, but I would like some suggestions on ways of doing the following:
The words at the bottom "Health", "Stats", etc are buttons. I was wondering what the best way of making those buttons present information would be. Is there a way I could show them in that information in one of the bottom squares, and when a different button is clicked change the info to that one? Or would it be best to have popup frames to display the information?
IMHO, I'd go with display the content or info of the button in the panels above.
It keeps the information together in a single place and generally makes it easier to manage, no disappearing windows behind other windows for example.
You have any number of options depending on the information you want to display. You could simply use a none editable JTextArea if the information is just text, or a JList if you want to list items if the data is more structured, a JTable and even a JTree if you want to group the data into some kind of groupable hierarchy.
You could use combinations of each, based on your needs
I want to implement a small tooltip with a scrollbar like the one in eclipse that appears when hovering above a class or member.
The problem I have is finding a way to limit only the width but not the height of a component within the scroll pane I have inside my tooltip. The component should support HTML and also wrap the text correctly when it exceeds the width of the inner bounds, but all components I have tried out have either line wrapping or HTML rendering, but not both
A way to limit only width is also nowhere to be found as every "setXSize" where X is "preferred" "max" "min" etc. all require two arguments and there is no "setMaxWidth" method for components.
Setting "setMaximumSize(new Dimension(256, Integer.MAX_VALUE);" would seem like a solution but it doesnt work as parameters set by "max" and "min" are ignored most of the time which is quite frustrating.
On request a current example of the implementation:
public class MyTooltip extends JTooltip{
private JScrollPane scroll;
private JEditorPane pane;
public MyTooltip(String htmlCode){
this.pane = new JEditorPane();
this.scroll = new JScrollPane(this.pane);
this.pane.setEditable(false);
this.pane.setContentType("text/html");
this.scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Here the problems begin
this.pane.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.scroll.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.pane.setText(htmlCode);
this.add(scroll);
}
}
the actual code is a bit more complex ofc. but I think this is a good approximation ...
Have you tried JTextPane with HTMLEditorKit (content type text/html)?
I think that's what you need.
Ok, the whole problem just solved itself: One had the idea to let the user write his own texts to be displayed on the tooltips, but that included letting him use multiple "spaces" for indentation when he called for it.
To let HTML render this as intended we replaced every "space" with an & nbsp; so no optimization on gaps between characters would be performed, but this of course has the result that no algorithm for automatic line wrapping would accept any "gap" between words as a suitable place to break the line.
So our implementation actually works as intended, only the Strings we give the tooltip to display are not suitable to be line broken.
Is there a library that would give me 2D graphics with a focus on Text for Swing. I am building a simple form designer and need to position text correctly on a 2D display. It would be nice if there was a library that handled the newlines and possibly editing.
An approach I've used successfully in the past is to draw your form with Graphics2D into a single panel. Graphics2D gives you precise text and line positioning. When you want to edit the text, have the user click on the text and place a JTextArea or similar into the panel where the text is while the user edits.
I like the answer that suggests using a JTextArea when the user is going to edit text, since it is simple. However, if you want true in-place editing, while also rendering exactly your text content, you could investigate creating your own View implementation for a JEditorPane. This gives you a ready-made model and controller for editing, allowing you provide the presentation, so you can render as accurately as necessary. And with the JEditorPane.DefaultEditorKit available as source, you have plenty of guidance about how to implement your own View.
This may be overkill for your needs, or it may give you the fine level of control needed.
I'm currently making a GWT project where I display some HTML in a RichTextArea, and I want the RichTextArea to be selectable/highlight-able by a mouse but NOT be editable/modifiable by the user. In addition to this question, could you also tell me how to retrieve some highlighted text in string from without me having to add a text-background toolbar, which, after highlighting a text from the RichTextArea, you change the color of the text-background, upon which, you add a separate periodically looping thread which checks to see when the text-background changes substantially from white (or a native color of the webpage) and finally extracting the string whose text-background color differs as the selected text.
I really hate to give any pointers without explanation but i think your requirements are bigger ::: so --->
http://examples.roughian.com/index.htm#Widgets~RichTextArea
http://www.java2s.com/Code/Java/GWT/RichTextArea.htm