convert a method to work with gui - java

I've got a huge method that prints multiple lines, numbers, characters and uses system.out and multiple data types, it works. But I'd like to use it in a jframe. I tried converting every system.out statement to a jtextArea.setText(), and did casting for non string types but nothing comes out when I run it.
Is it possible? what is the right way of doing that.
jtextarea right for my method.

If you wish to append text to a JTextArea, use the append method. Right now your code is using setText, which does just that: sets the text of the JTextArea, removing all previous text in the process (and in this way your code seems to almost guarantee that the JTextArea either contain no text, or contain a single new line character).

try jTextField if its only gonna output.
Try jTextField.append if the user will write stuff on the text.

Related

How to clear JTextArea?

I'm trying to clear the JTextArea.
Currently, I'm using
jtextarea.setText(null);
What is the difference if I use
jtextarea.setText("");
There is no difference. They both have the effect of deleting the old text. From the java TextComponent page:
setText
public void setText(String t)
Sets the text of this TextComponent to the specified text. If the text is null
or empty, has the effect of simply deleting the old text. When text has been
inserted, the resulting caret location is determined by the implementation of
the caret class.
Note that text is not a bound property, so no PropertyChangeEvent is fired when
it changes. To listen for changes to the text, use DocumentListener.
Parameters:
t - the new text to be set
See Also:
getText(int, int), DefaultCaret
What the author was trying to was clear the JTextArea, not add a null character to it!
JTextArea0.selectAll();
JTextArea0.replaceSelection("");
This selects the entire textArea and then replaces it will a null string, effectively clearing the JTextArea.
Not sure what the misunderstanding was here, but I had the same question and this answer solved it for me.
JTextArea0.selectAll();
JTextArea0.replaceSelection("");
Actually There is the difference , i think so.
If you set it to null, The actual value written in text area will be nothing. But if you set it to "" it wil be an empty character. The same like you can set it to "z" and there will be written z, but null means unknow. You will not feal the difference until you gonna need to use the text written in textArea.

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.

Java JTextArea Question

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.

Input-validation based on entire content of JTextField, not just the last typed character

Is there a way to validate text in a JTextField while you type, based on what you already typed in that field? Should I create a keyEventListener of some sort, or is there a way to override the insertString method to let it do that. I prefer the latter, but it only gives you control over the last character that was typed, not the entire string of text already present in the text field. I want it to beep as soon as one tries to enter more than one decimal point, and not adding that second decimal point to the text field.
Thanks for your help,
Erik
What's the problem with adding a listener? That's what they are there for.
You might also want to have a look at JFormattedTextField.

How to redirect all console output to a GUI textbox?

I currently have a program that prints lines of text to the screen in various manners such as 'System.out.println()' statements and for loops the print all elements in an array to screen.
I am now adding a GUI to this program. My problem is that I want to print everything that prints to eclipse's console to a textbox in my GUI instead. Is this possible and if so how would I go about doing this.
Thanks in Advance.
Check out this blog article, entitled Redirecting System.out and System.err to JTextPane or JTextArea. It describes almost everything you need.
The basic idea is that you create your own specialized output stream. In your implementation of the write() methods, you call some code to append the new data to your text box. Then, you set this new output stream as your System.out by calling System.setOut() or System.setErr().
NOTE: that article is missing one thing. You need to start your program in a separate thread.
An idea:
Create your own PrintStream that outputs everything to this textbox.
Then set this new PrintStream to be the standard output stream like that:
System.setOut(myPrintStream());

Categories

Resources