I am a huge newbie and I have a program that normally prints items to the Java console window. I would like this program to become a window in which the user can interact with. The reason why I have not resorted to dialog boxes and panels is because this program require multiple prints to the console window. A traditional dialog box does not continuously update or compound on data that has already been printed on the box. I realize that there is another way of doing this by creating a program that mimics the Java console window. Because I am a noob, all of the java console redirecting questions and answers on this site have blown over my head. Can anyone please help me?
See maybe How to Use Editor Panes and Text Panes will be helpful and give you some ideas.
The short answer is, every time you want to update the contents of a text box, call the setText function again. There's no "append" function on the contents: you have to give the entire contents each time. If you want something that mimics a console window, where messages continue to scroll, the simplest thing to do is to keep the entire contents in a StringBuilder. Each time you get new text append to the StringBuilder, then setText(myStringBuilder.toString).
You could, I supppose, write mybox.setText(mybox.getText()+"new contents"). That would be a little inefficient but probably not a big deal.
I don't know exactly what you're up to, but trying to redirect console output to a text box sounds like more nuisance that it's worth. Just put your data in the text box: don't write it somewhere else, then try to get it back and put it where you want it. I suppose if you have thousands of lines of code writing to the console and now you want it to go a text box, there might be value in not having to change all that code. But the structure of a console app is so different from the structure of a GUI app that changing the output statements would probably be the least of the things you'd have to rework.
Related
I'm building a grammatical text interpreter with a JavaFX GUI where you can just paste your text into a TextArea. This creates something of a problem though, since line breaks from MS Word are somehow formatted in a special way (both paragraphs (ENTER) and line breaks (SHIFT+ENTER).
If I copy the text to a plain .txt file and open it in a browser (tried Chrome, Safari and Firefox), I can copy it again from there without any problem. They somehow fix the issue.
Pasting the text into a new e-mail in Apple's mail and then copying it from there, fixes it, even pasting it into a JEditorPane (java swing), fixes it.
Notepad, TextEdit, Notes and Pages does NOT solve it.
But I need the ability to copy the text directly from a variety of sources and asking people to copy it to somewhere else is just not an option.
I searched for a solution before this post, and the general solution I could find was to replace "\r" with "\r\n", but this doesn't seem to help, as the line break is already lost, as soon as it has been pasted into the TextArea.
A different solution would of course be to simply use a JEditorPane instead of the TextArea, but when I create an AnchorPane and put a JEditorPane into it as a swingComponent, I get an area the size of the initial text, and editing that text on runtime doesn't change the size of that area (which is NOT the area of the contentPane).
Now, I know that I present different solution ideas here, and that I probably should just ask for how to get one specific thing to work, but I'm really getting a headache over this, so weather it's a way to get the contentPane to work properly with the layout, finding a way to make the TextArea keep these linebreaks or even something different is fine. I've just spent too much time trying to solve it now :(
I'm working on one of my first java applets and I want to start of fairly simple (though I have a good understanding of how code works I dont know much in terms of what methods I all have at my disposal when using java)
I have created a Jframe window that has a JTextarea in it. I would like to execute certain lines of code when certain things are typed into this box. In essence, its a simple text input system. How would I go about doing this or is there a better component to use for this?
in addition to getText(), for JTextField some prefer the getDocument() method. In Java, Listeners are used to capture events, such as "what was typed to the text area". This tutorial will get you started, if you have trouble implementing you can come back with a more specfic question and some code :)
We have a requirement where we already have pre printed stationery and want user to put data in a HTML form and be able to print data on that form. Alignment/text size etc are very important since the pre-printed stationery already has boxes for each character. What could be a good way to achieve this in java? I have thinking of using jasper reports. Any other options? May be overlay image with text or something?
Also we might need to capability to print on plain paper in which case the boxes needs to be printed by our application and the form should match after the printed with the already printed blank stationery containing data.
Do we have some open source framework to do such stuff?
Jaspersoft reports -- http://sourceforge.net/projects/jasperreports/
You will then create XML templates, then you will be able to produce a report in PDF, HTML, CSV, XLS, TXT, RTF, and more. It has all the necessary options to customize the report. Used it before and recommend it.
You will create the templates with iReport then write the code for the engine to pass the data in different possible ways.
check http://www.jaspersoft.com/jasperreports
Edit:
You can have background images and overlay the boxes over it and set a limit on the max character size ... and many more
It is very powerful and gives you plenty of options
Here is one of iReport's tutorial for a background image http://ireport-tutorial.blogspot.com/2008/12/background-image-in-ireport.html
The big problem when printing form content that has been filled in electronically, is aligning it correctly on the pre-printed form. You may get content to align for one printer, but when you use another it is completely misaligned.
Fly Software have a form design product called InForm Designer that gets around the problem nicely by allowing users to specify and save vertical and horizontal offsets for printers. This ensures filled in form content is always aligned. I've tried it and it works perfectly. Take a look for yourself here...
http://www.flysoftware.com/products/inform_designer/overview.asp
It might be worth implementing a printer offset similar to InForm's in your own application (if possible).
Some things to think about.
First in terms of the web page, do you want use the stationery as the form layout?
Does it have to be exact?
Combed boxes (one for each character)
Do you want to show it like that on the web page, or deal with the combing later.
How are you going to deal with say a combed 6 digit number. Is this right aligned. What if they enter 7 digits. Same for text. what if it won't fit.
Font choices, we had a lot of fun with W...
How aligned do you want the character within the box, what font limitations does that imply, some of the auto magic software we looked at did crap like change the size of each character.
Combed editing is a nightmare, we display combed, but raise an edit surface the size of the full box on selection.
Another thing that might drive you barking mad, you find find small differences in the size and layout of the boxes, so they look okay from a distance but a column of boxes sort of shifts about by a pixel. Some of testing guys had to lend us their electron microscopes, so we could see how many ink molecules we were out by. :(
Expect to spend a lot of time in the UI side of things, and remember printed stationery changes, so giving yourself some sort of meta description of the form to start with will save you loads of trouble later on.
I'm looking for a highly efficient Swing Java component which I can just plug into my application UI. I already tried using the classes within Swing such as JTextArea with no avail; they simply aren't high-performance enough and have any crippling drawbacks. Additionally, it'd be nice if it had standard console features such as scroll-lock, clear console, colours, and so on.
Edit: Forgot to say, this console will have a lot of debug information streaming into it, and it needs to be fully scrollable.
Cheers,
Chris
I fail to see what is wrong with using a JTextPane. It supports attributes which you can specify as each piece of text is added to the console. Clearing it is also obviously a no brainer. When added to a scroll pane it also supports scrolling.
You can add scroll locking by using Smart Scrolling.
Plus, it removes text too early and
No idea what that means as text is never removed unless you specifically remove it from the document.
doesn't allow the user to scroll while
input is being entered (afaik). The
effect is that you just see text
flashing while the number of rows
remains the same.
By default the text scrolls automatically as text is append to the document assuming the code is executed on the EDT. This scrolling can be controlled the the example provided in the link above.
Edit:
but I'd still like a library solution
I don't know of any
auto-colourise text coming from
different streams
The Message Console might give you some ideas.
(i.e., detect [error] prefix on a
line) and colourise lines based on
this)
This is easily done by adding a DocumentFilter to the Document of the text pane. You can add attributes as text is inserted into the Document.
Be sure that you read about the Event Dispatching Thread (EDT) in swing!
BTW: a simple search 'java swing console' will give you a lots of hints OR you could use/adapt the beanshell textfield which is a jtextfield too ...
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());