Restrict number of lines in html JLabel - java

I have a JLabel that needs to display some html-formatted text. However, I want to restrict this to being 4 lines long (and if so, provide a button to see everything).
So far, I've tried setting the maximum size manually or via a layout manager. However, both of these solutions can cause part of a line to be displayed.
edit: To add a little more details, I need to force 4 lines even when respecting line wrapping correctly, resizing components, and changing font sizes. I've considered handling resize/fontsize changes by replacing the label with a new one that fits correctly.
JLabel seems to handle incomplete tags well, so I could probably do something like a binary search on the input string finding which character would cause it to go over the 4 line limit (using FontMetric to determine how many pixels 4 lines would be), and then replacing an existing label with the new one. The big downside to this approach is that I need to run the computation every time the user resizes the panel or changes fonts (and it feels like a dirty dirty hack).

Add the JLabel to a JScrollPane as set the scrollpane with a reasonable preferred size. Scrollbars will appear a necessary.
I don't know of any absolute solution to the questions since I doubt you can define what a "line" is. One line of text may be font 12 and another 24. I don't know of any way to calculate the height of each given line.
Even if you did use a ComponentListener to handle the componentResized() event I'm not sure you can come up with a reasonable algorithm to to calculate the exact width/height of of a 4 line display.

I would try running through the String of text and removing all text after the third "\n"
String shortenText(String oldtext){
String newText = "";
for(int i=0;i<3;i++){
newText += oldtext.substring(0,oldtext.indexOf("\n"));//adds one line to String
oldtext = oldtext.substring(indexOf("\n")+1);//shorten old string to prepare for next iteration
}
return newText;
}
You may also want to try the same algorithm, except strip of <p> and <br> tags as well...
If you know the values of the possible tags just switch the text from "\n" to "<br>" or any tag you need

Hey, I found a way that works. The framework I'm working with allows me to create a listener for font size changes. In this listener, I determine what the new max size of the label is (getFontMetrics(font).getHeight() * 4) and then re-set the maximum height on the label to this and then relayout everything. This even handles the word wrap case well. I'm guessing that someone could do nasty things with silly HTML input, but this covers the 99% case pretty well.

Related

How can I make it so a JLabel only displays the first line of text?

Take a look at this picture:
This is a JLabel with a height that is not allowed to vary. However when there is too much text to fit in its given size it attempts to display both lines. I do not want this, how do I make it so that only the first line up to the "in new" is displayed?
Why not make it scrollable? This way you can maintain the preferred size, and at the same time display the info when it's needed. You just have to put the JLabel inside a JScrollPane:
JScrollPane scroller = new JScrollPane(yourJLabel,
JScrollPane.VERTICAL_SCROLLABAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLABAR_AS_NEEDED);
The solution was to not use html (ie, "<html>" inside of the string) for it was what was causing the lines to warp when too long. All bolding and monospacing had to be done without using html.

Changing the amount you "tab" java?

As above. I have a modal JOptionPane that uses a text area to display a slightly larger than normal message. The Pane works fine and is currently roughly 300px sq. The problem is I am trying to output something similar to the following:
Amt (TAB HERE) x (TAB HERE) Type (TAB HERE) Price (TAB HERE)
Again I have no problem with the actual Panel at all it's displaying at the size I want but for some reason it "tabs" really far. Like 1/3 of the JOptionPane such that it cuts off after Type and I lose half my text. Is there any way I can specify the size of the tab I want? I've tried aligning manually using spaces but as you can imagine not all letters are the same width and it's just too damn hard to get it to line up properly so I NEED to tab. I don't want to mess around with new layout managers either. I am simply concatonating a string in a method returning it to a JTextArea and then putting that inside my JOptionPane so I'd like a solution that works doing this. If I output to the command line the tab looks normal, like maybe 5 odd characters but doing that in this manner it's more like 2 words long....
Worse case scenario I make a bigger JOptionPane but I would prefer not to for aesthetics.
You can put an HTML table in your JTextArea like they show here. Either a JTextArea or a JTable can go in a JOptionPane.

Display a row of Strings on a JComponent so that the single Strings are selectable/their location getLocation()-able?

In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.

Text is not fully shown in JTextField

They say a single image is worth 1000 words:
I'll just note that the size is set to default. (build in NetBeans)
any idea how do I fix this?
Adam.
Without you showing code, I'd say that your JTextField width is not set to be wide enough. You can resize it to be large enough for the number of characters you anticipate.
However, this does not guarantee that the user will not type in more characters, which would show the text cutoff as well.
You can extend the Document that JTextField uses to add the maximum character restriction, as shown at
http://www.rgagnon.com/javadetails/java-0198.html
what are the lengths of your data,it seems you changed the layout and that's causing that problem as the border seems also occupying half of the character.
They say a single image is worth 1000
words:
Actually its not. When posting a question a SSCCE is worth 1000 words.
Stuff like that usually happens when you don't use a layout manager. Assuming (which is all we can do since you didn't post any code) that you are using a proper layout manager then your basic code for creating a text field to display 3 characters is:
JTextField width = new JTextField(3);
The reason is the LNF that was assigned to the frame, once I've changed that, it all works fine.

Reading from Java JTextArea

Okay, thus may seen kind of odd, but I wanted to get some suggestions from everyone here. I am a beginning Java developer (after 2 years of ASP.NET web development) and I have recently began working on my first Java project - a calculator. I realize that their are tons of calculators out there, but I thought it would be a good beginner project.
Anyway, here is what I need help with. Currently, I am using a Scrolling JTextArea for display (instead of a simple JTextField) that is approximately 5 rows tall. I want the user to be able to scroll through the list to see previous entries and such. The format of the box will be equation on one line and the program will generate the answer on the next and so on.
My real question is, how is the best way to implement this? My fist idea was to read through the JTextArea when equals is pressed, down to the last line and try to search that line for the operator (+, -, etc.) and the operands. Is this the best way to go about this? Although, this would work would work, I think it could get cumbersome and sounds very inefficient. I am open to any suggestions, even possibly replacing the JTextArea is some other component would work better.
Thanks!
There's no need to read through the JTextArea contents - use JTextArea.append() to add to the end. Here are some examples of JTextArea content manipulation:
JTextArea ta = new JTextArea("Initial Text");
// Insert some text at the beginning
int pos = 0;
ta.insert("some text", pos);
// Insert some text after the 5th character
pos = 5;
ta.insert("some text", pos);
// Append some text
ta.append("some text");
// Replace the first 3 characters with some text
int start = 0;
int end = 3;
ta.replaceRange("new text", start, end);
// Delete the first 5 characters
start = 0;
end = 5;
ta.replaceRange(null, start, end);
If you are open to different interfaces, you might want to try something like a JTextField at the top of your view, from which you can receive as input your 'new' inputted equation, and then below it with the same width a JList that would scroll to have all of the previous equations and their results. That would make parsing of the current formula much easier, and you would also have an easy time of keeping your previous formula and their results in a scrollable list, with the easy option of keeping the most recent on top.
your idea is interesting. so you would have a line such as.
2+2
then when pressing calculate would add the line
4
and so on then you could type in another equation.
it could work but as you said it wouldn't be the most efficient implementation... but that's just a tradeoff of getting the desired functionality.
If i were going to implement it the way you discribed (with a JTextArea) I'd use scanner, and scan the value string a line at a time.
if the line has +/- in it then do the calculation and add both the original line and the answer to a string.
the new string is the new value of the text field.
this method would get pretty cumbersom as you would be continually recalculating the users old entries more were added.
I guess if you continually stored the last line of the document, when you run out of lines, calculate the last stored and append the answer, then it wouldn't be so bad.
Here's what I would do:
use a JTextField to enter in the calculations, and a JList to display the old ones and their answers.
You could treat each line as a single operation. That way you could use the String array returned directly by:
String [] operations = textArea.getText().split("\n");
And then you'll know that exactly each one of them as a complete operation ( may be invalid, but that' another story )
Is this what you asked or do I totally misread you?
I think a simpler solution would actually use two components. A TextArea to hold the "history" of what's happened so far, and a textfield where the user inputs new entries.
Thanks to everyone who replied. You all gave me some ideas to think about. I think right now, I am going to go with my original idea of using a single JTextArea and try to find ways to optimize the process. If that gets too difficult (which is very possible), I will follow the majority's advice and use two separate fields. Thanks for replying everyone!

Categories

Resources