So in JTextArea there is a getLineCount() is there something similar for JTextPane, because I could find anything. Perhaps there is a different way of obtaining that? I want to get the number of currently existing lines.
There is (as you noted) no built-in way to find the number of lines in a JTextPane- this may be (although I see no official reference to support this) because of JTextPane's ability to hold many different text styles in a single area, which would lead to some complications when finding the total number of lines.
You may be able to calculate the number of lines on your own, by keeping track of the text (and its style) that is presented inside of your JTextPane- you can then use other methods (perhaps LineBreakMeasurer (documentation here)) to calculate the number of lines for each piece of text, and then sum up the total.
Related
So I wish to output the console to a JTextArea which I can do, however I wish for the text area to be treated like the Eclipse console, and remove old lines once the line count has reached a certain threshold. Such that, once the line count (or character count) has reached a threshold of say, 300, every new line added will delete the first line added meaning that the number of lines (or characters) in the JTextArea's document will never exceed that threshold. How might I do this? Cheers.
and remove old lines once the line count has reached a certain threshold.
You would use a DocumentListener for something like this. You will receive an event every time text is added or removed from the Document. You can then implement your logic to control the lines (or total characters).
Read the section from the Swing tutorial on How to Write a DocumentListner for more information and basic examples.
You can also check out Limit Lines in Document for an example that shows how you can limit then number of lines using this approach.
I've been working on a GUI to handle DNA sequences. Most of the molecules will be plasmids, which are circular forms of DNA. I can get a sequence as a string and display it in a JTextPane, but I'm not sure how to handle cases where the user might want to select a section of the sequence that crosses from the end to the beginning of the sequence. One thing I've considered is displaying the sequence twice, so you can select the last part of the first section and the first part of the last section, then overwriting some function so that copying the text will put the correct sequence on the clipboard instead of the string that was actually selected. (I'll have to do that anyway, I'm displaying the forward and reverse strands of the DNA, then a blank line, so that 1 "line" of actual sequence becomes 3 lines of text.
Is there some trick to circular strings that I just don't know about?
Do you know how other programs handle the selection of pieces of circular DNA? You might be able to get some inspiration from other software, like alignment viewers (supporting circular DNA) in this list on Wikipedia: en.wikipedia.org/wiki/List_of_alignment_visualization_software.
I think it would be wise to decide on how you want the GUI to work first, and then start working on your code. Otherwise you risk wasting a lot of time on implementing ideas that you end up not using.
You could even consider offering both a circular and a linear view on the same sequencing data, as this screenshot from the CLC Sequence Viewer shows:
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.
None of the answers to two previous questions (here and here) resolve my problem.
I have a multi-column jtable for which I want to display string-content of some columns over more than one line within the cell based on newline char's ("\n") within the string. The number of newlines per string is random, known only at run-time. Only the affected row must be adjusted across all columns to the new height. There may be a different number of lines per affected column, and the row-height needs to be adjusted to the maximum height of these, across the columns.
How do I do this?If possible some sample code would be very much appreciated.TIA
If I got you right, I think you need a MultilineCellRenderer . There are already plenty of examples around. Normally they are based on a JTextArea to get the line wrap functionality.
I haven't used it myself yet, but here is an example, which looks kinda good at first view:
MultilineCellRenderer
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.