This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Rows in JTextArea
In JTextArea, operations such as select, highlight and so on seem all to depend on offset from beginning of text. In an app that displays line-oriented text, I need to select, highlight (based on info from elsewhere, not caret) based on row and column.
Is there some functionality built-in, or in some helper class, to get offset from row,col? I realize I could maintain separate data on line-start offsets etc and calculate row,col-->offset, but surely JTextArea (or its model) already knows this in order to display the text, so I'm persuaded there must already be a way to do this.
I did see examples that use something like this one, using textarea.viewToModel(new Point(x,y));, where x and y were purportedly row,col, but so far as I can see, x, and y are pixel coordinates, not row,col... so not sure what to make of that.
Clues? Thanks!
Edited: So the question has been closed under the impression of five commenters that this is a duplicate of other questions, which it is not. I did not ask about how to convert offset to row, col, nor about how to convert screen pixel coordinates to offset, which are the subjects covered in the other articles.
In case someone else stumbles in here looking for the answer to what I actually did ask, I've now discovered that it's as follows.
JTextArea has the functionality I expected to find, but evidently overlooked on earlier browsing: getLineStartOffset(int line) which will give the offset-from-start-of-text for a particular line (row) of text. To this one can easily add the char-posn-in-line, and thus arrive at the offset of a particular character.
A textArea contains a stream of text. There's no magic method that will locate a row/column in your text model, since that can be ambiguous if your text contains variable-width characters or different font sizes. You must maintain the data necessary to map from your idea of what a particular (row,column) means with regard to your data.
That's simple example of rectangular fragment selection http://java-sl.com/tip_vertical_selection.html
You can use javax.swing.text.Utilities.getRowStart()/getRowEnd() methods. First find start row offset for the row number. Then just add col number to get the offset.
Related
I am using Apache PdfBox 2.0 in order to parse a pdf file. Having some fixed strings, I was able to create a system based on:
A fixed text, as a starting point
The next cell/text position, or null
The bottom area, to determine the height of the rectangle.
Using the starting point, I am computing the x and y (see below pic for pdf structure in PDF Box:
Using the "next" text block (which is another fixed value, for example a field or a table header), I am determining the width of the desired region, using formula:
width = second.x - first.x
or something similar. So, in a table, for example, knowing in advance the header names, it's easy to detect the columns. What I am trying to do (and so far fail to do so in an accurate way) is to determine the lines in a pdf table. This table sometimes contains missing values in some columns and also multiple lines values for some rows/columns. I have extended my "system" (first, next, bottom) to work dinamycally with table rows, and this works great when I have "normalized" tables (e.g. no whitespaces and/or at least, no multiple line values). But it's not working with real world data, because so far I could not find a way of determining the location (x, y, width, height) of a multi-line value. Is this even possible with PDF Box? Some people suggested to convert the pdf to html first and then to parse the html instead. Is this a viable option? Has anyone worked with this library? I will try to use this next.
Like I said in my previous comments, I have found a partial solution for my issue. This is based on two things:
First, I assume that one column for each table contains only distinct values which never occupy more than 1 row.
Next, since I also have some fixed texts in the document, I have determined these texts coordinates and use them as a delimiter of the area which contains the text I want to extract. For example, the "current, next, bottom" system (as I call it) can contain for example: "Column name A", "Column name B", "Fixed text C" (or second row from the same table - determined based on the unique single-row values).
It is not perfect, and problems may occur if the fixed texts may occur more than once in the document. Of course, improvements can be made by filtering the correct occurrence using the vertical coordinates and so on, but for the moment, I will close this question, as it seems that this problem has no standard answer and currently there is no open source library able to extract tabular data from pdfs.
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.
I am designing an on-screen form to be filled in, and I think it makes sense to stick it together as a collection of text areas. I note in the documentation of JTextArea that a text area can be subdivided into rows and columns, but I can't find any methods that appear to deal with placing text directly in any specific row/column cell in a text area.
Are there such methods, or is there an alternative text component that would work better for this purpose?
Thanks in advance for any insights.
John Doner
If you want a table, there is JTable but it is a bit more complicated. (Here is a tutorial) Alternatively you could put your JTextAreas into a layout such as GridLayout
A bit hackish, but you could insert (row-1) newlines and (column-1) space characters before your actual content. Of course that would only work on a previously empty text area.
However, you can extend that approach so that you only insert characters if needed, and otherwise just count already existing characters. That is, to go to a row, you skip the first row-1 newlines. Then in that line, you skip the first column-1 characters.
If there aren't enough newlines or characters already in the text area, you add more at the end of the text.
However, it gets trickier if your text content contains newlines itself.
It's doable, but it's gonna be ugly.
The row and columns values are just used to give the text area a preferred size.
I don't see the point of trying to set text at a specific row when creating a form. Generally forms would be designed with label/text field pairs, so the user know where there are entering the text.
If you text area is for output then you just add new lines when you want to display text on a different line.
Forcing a text area to be able to insert text at a given row/column is definitely not the way it was intended to be used.
I don't understand the real requirement so I can't make any other suggestions.
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
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!