Normally a left-orientated JLabel shortens text at the right by adding periods, e.g. Hello wo.... Anyone knows an JComponent (TableCellRenderer is not sufficient) which can shorten text in the middle (Hel...rld), e.g. useful for displaying file names?
The LeftDotRenderer displays the dots on the left. I know its not what you asked for, but you might be able to use the concepts in the code to create your own.
Related
I want to make a Swing program that writes updates on what you have done. It writes all the information on an uneditable JTextField.
For example:
You have changed the text to BLUE.
You have changed the text to RED.
The problem is that I can't make the two lines separate.
What I get is this:
You have changed the text to BLUE. You have changed the text to RED.
What I've tried is this: (This does not work)
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to BLUE.");
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to RED.");
you can't have multiple lines with JTextField , you can use JTextArea for that purpose , and the code wouldn't be much different too , you can still use the same code or you can just
TF_BetInfo.append("\nyou have ...... ");
where TF_Betinfo is a JTextArea rather than a JTextField .
You can't use JTextField for this. JTextField is sinle-line edit control. That is why you got all your output in one line.
If you want several lines to be printed in edit use JTextArea. In your case you can use jTextArea.append(string) (note: jTextArea is an object of class JTextArea, and string is an object of class String).
You can't actually use several lines in a JTextField, but what you can do, is using a JTextArea of the wanted size, and then use the following:
yourJTextArea.setLineWrap(true);
This will automatically detect when the JTextArea needs to use another line and add it, pretty cool, useful and you only need one code line to do it.
JTextArea is more flexible but if you really want flexibility read about JTextPane or JEditorPane, you can show URLS, internet pages and everything that comes to your mind.
So I've been developing web applications for 10 years, but have fond memories of working on small swing applications that related to an introductory programming curriculum I was paid to help with before that. I generally like building swing/java UI especially with a decent UI builder. However, in the intervening time I've become very accustomed to providing clear, red error messages of variable length and number when the data on a form is invalid. Sometimes with icons, sometimes with markers on the field etc. Very common stuff that's easy to do in the web world, and is really helpful and improves usability.
Today I was trying to find a way to display a nice list of error messages at the top or bottom of my swing form, icons and field markers can come later, but for now I just want a nice variable sized list of things that need to be corrected, in a red color.
At first blush this seems like it would be easy, but..
JLabel doesn't wrap, so long text is hidden, or you have to make the form ultra wide
JText Area has the same issue unless you define a number of columns, and I want to fill the space available. A set number of columns won't resize.
JTextPane wraps but as far as I can tell it won't turn red unless you get into the whole way over complicated StyledDocument bit. I don't want to do anything more complicated than change the text color so this seems like overkill.
There's also the minor issue of pushing outside the window space, and one can call pack(), but Id like to only increase the height of the form and wrap the text not change the dialog to the width of the message (creating lots of strained layout and dead space if a single long message doubles the width)
I also don't really want to have scroll bars either for the message area or the form as a whole.
This surely is a wheel that's already been invented, so what's the right way to show a list of red (but otherwise unformatted) error messages in a swing form?
Label doesn't wrap, so long text is hidden, or you have to make the
form ultra wide
use HTML
JText Area has the same issue unless you define a number of columns,
and I want to fill the space available. A set number of columns won't
resize.
use word and line wrap
A set number of columns won't resize. and together with "I also don't really want to have scroll bars"
put JTextArea to JScrollPane, to disable JScrollBars, set null for Borders from JScrollPane
JTextPane wraps but as far as I can tell it won't turn red unless you
get into the whole way over complicated StyledDocument bit. I don't
want to do anything more complicated than change the text color so
this seems like overkill.
use HighLighter, but nothing clear from posted description about your issue with StyledDocument
I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I write superscript word for checkbox text in java?
So I have this very simple program that basically consists of a GUI with a few text fields and a button.
The idea is that the user enters numbers into three fields and presses the button. Then what happens is a few mathematical procedures are carried out in the background, and the resuling answer is presented in a fourth text field.
Now, this does the job, but the formatting looks awful. At the very least, I would like to have part of the output superscripted. I have next to no experience with these things, but thought I would be able to get the hang of this on my own, but I'm stuck. I think I need to use AttributedString and possibly Font, but I can't get anything to work. And I've found no tutorials.
Does anyone have any quick pointers? That'd be most helpful!
Do you need the output field to be editable? If not, try using a JLabel with HTML code. Something like:
jLabel4.setText("<html>ax<sup>2</sup>+bx+c</html>");
You can add a border to that JLabel to make it look like a text field.
Why not use other swing components that support HTML to display the answer like JLabel. If you use a JLabel for instance you can use inline html to format your answer
The reasons you are having difficulty has to do with the lack of a default typsetting system. Font selection and such typically provides a very limited means to do proper math typesetting (which is a specialized subset of general typesetting).
I don't know of any math specific typesetting already built-in to the Java libraries, that said, perhaps you can integrate the ExTex project into your Java components or roll your own solution using the 2d API (look at baseline offsetting).
Other alternatives are to generate a graphic, and display it's rendered image. If the display is static, this might be a much easier choice.
I have questions according to my final project in my IT Faculty.. I have to make A simple text editor (like notepad) without using JTextArea (GUI Java), I have to make my own JTextArea..
Idea/Topic= Own Text Editor
Algorithms= doesnt have any idea yet
Data Structure= Rope(Member of Binary Trees)
Requirement= Typing without JTextArea...
Is that possible to make that? Can a panel (or something else like that) has a listener or make a listener to do that?
simply thinking, First step I just want to try for typing on a panel, absolutely algorithms needed but i dont get right algorithms so far.. Is that possible?
My thinking is, for text that im typing is stored to a tree.. Is that Rope(Member of Binary Trees) for my data Structure?
Thanks for helping...
It's possible.
Use as you mentioned JPanel attaching KeyListener and MouseListener.
You need a model (Document). For simplest case it could be e.g StringBuilder where you can add content and show the StringBuilder content.
You need caret position (int field) to indicate where the edit should happen.
You need Font field to keep all the font info to be used in the editor.
Override paintComponent() and use FontMetrics to measure the text and calculate necessary width/height. For the start I would create a text area without line wrap.
Implement viewToModel/modelToView methods. They should calculate position of caret for given x,y and x,y for given caret position.