I have a JLabel, and as the JFrame window expands, the font size automatically changes. The text that is in the JLabel can either be abbreviated or put in full text like so:
Because I am using Eclipse's "AbsoluteLayout" for Swing (frame.getContentPane().setLayout(null)), JLabels won't automatically resize to the size of the texts and are fixed values. Note that I do change those fixed values as the window changes size. I already have an algorithm for this.
Instead of doing a large calculation determining whether the window is large enough to display the un-abreviated text, I would like to know if there is a way to determine whether my text would appear like this:
If I could know that the text would be cut off (due to long text in a small JLabel), I could abbreviate the text, otherwise display it in full.
Is there any way to do what I'm asking while still using the AbsoluteLayout?
Note: It is necessary that I use AbsoluteLayout, so don't bother commenting about it even though it is typically regarded as bad practice.
If I could know that the text would be cut off (due to long text in a small JLabel), I could abbreviate the text, otherwise display it in full.
Compare the width of getPreferredSize() and getSize().
Related
I am using a JTable in a program. The problem is that when I set the size of the whole JTable, I am using this method:
setPreferredScrollableViewportSize(Dimension size)
The question I have is that when using this method, you enter in the width and length in pixels. Will there be issues (formatting-wise) when the program is run on different computers/machines?
Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? - Short answer is yes (you should avoid it)
When setting the preferredSize of the table, you are not actually taking into account the number of rows or columns which the table might have, depending on the layout manager, it may NEVER be larger than the dimensions you provide, regardless of the amount of data
The question I have is that when using this method, you enter in the width and length in pixels. Will there be issues (formatting-wise) when the program is run on different computers/machines?
Generally, yes.
JTable was designed to be shown within a JScrollPane, which allows you some leeway with this, as the JTable can grow and shrink, within the JScrollPane, but the JScrollPane can remain the same.
Having said that, you should NOT be using setPreferredSize and especially not messing with JTables, JTable calculates it's own preferred size based on the needs of the data...and it does a reasonably good job.
If you want to change the size the JScrollPane, then you will need to change the result of getPreferredScrollableViewportSize which lets the JScrollPane know how big a component would like the basic viewable area to be, under optimal conditions
See How to Use Tables and How to Use Scroll Panes
In a Layout Manager when I use setPreferedSize it increased the size of JLabel but show only one word like (JLabel = Name) after using setPreferedSize JLabel becomes like N.... But setFont works correctly..
Can anyone tell the exact difference between the behavior of setFont and setPreferredSize?
But setFont works correctly..
Exactly, and that is the only method you should use. When you use the setFont() method, the component is responsible for determining the preferred size. The component will take into account information like the font, text, border to determine it's preferred size.
When you invoke setPreferredSize(), you are telling the component that you know better, which you don't because you are just making a guess. If your guess is too small you see the "..." because there is not enough space to paint the entire text.
Don't use setPreferredSize()!
I'm creating a chat program, similar to IRC. With my client though, I have the problem that when text is added to a JTextPane (using a GridBagLayout), it resizes instead of wordwraps. Well, it actually will wordwrap eventually, but it shouldn't be resizing. Here is what I mean:
I could set the JTextPane dimension to an exact number, but I want the user to be able to resize the window, and the parts inside as needed. How can I get to put as much text in without it resizing?
I ended up fixing it by adding setting it into a JScrollPand and using .setPreferredSize().
How would one test if a JLabel, with set size, wouldn't be able to display all the text that it set to display?
Make another label that you do not set the size of.
Add the same text to the 2nd label.
Call for the preferred size.
If it is larger than the set size, your text will be truncated.
But the most sensible solution is not to set the sizes of labels in the first place.
See also this example.
I'm writing a custom file selection component. In my UI, first the user clicks a button, which pops a JFileChooser; when it is closed, the absolute path of the selected file is written to a JTextField.
The problem is, absolute paths are usually long, which causes the text field to enlarge, making its container too wide.
I've tried this, but it didn't do anything, the text field is still too wide:
fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));
Currently, when it is empty, it is already 400px long, because of GridBagConstraints attached to it.
I'd like it to be like text fields in HTML pages, which have a fixed size and do not enlarge when the input is too long.
So, how do I set the max size for a JTextField ?
It may depend on the layout manager your text field is in. Some layout managers expand and some do not. Some expand only in some cases, others always.
I'm assuming you're doing
filedNameTextField = new JTextField(80); // 80 == columns
If so, for most reasonable layouts, the field should not change size (at least, it shouldn't grow). Often layout managers behave badly when put into JScrollPanes.
In my experience, trying to control the sizes via setMaximumSize and setPreferredWidth and so on are precarious at best. Swing decided on its own with the layout manager and there's little you can do about it.
All that being said, I have no had the problem you are experiencing, which leads me to believe that some judicious use of a layout manager will solve the problem.
I solved this by setting the maximum width on the container of the text field, using setMaximumSize.
According to davetron's answer, this is a fragile solution, because the layout manager might disregard that property. In my case, the container is the top-most, and in a first test it worked.
Don't set any of the sizes on the text field. Instead set the column size to a non-zero value via setColumns or using the constructor with the column argument.
What is happening is that the preferred size reported by the JTextComponent when columns is zero is the entire amount of space needed to render the text. When columns is set to a non-zero value the preferred size is the needed size to show that many standard column widths. (for a variable pitch font it is usually close to the size of the lower case 'm'). With columns set to zero the text field is requesting as much space as it can get and stretching out the whole container.
Since you already have it in a GridBagLayout with a fill, you could probably just set the columns to 1 and let the fill stretch it out based on the other components, or some other suitably low number.