I need to be able to select the text in a JLabel. Ive read some guides on the net that talk about using a JTextfield to simulate a JLabel, however this is no use to me as I my JLabel will span multiple lines. So any ideas on how to do this or if it's even possible?
You can use a non-editable JTextArea which allows for multiple lines and selectable text, and visually looks the same as a JLabel.
I do not think it is possible with a JLabel (at least I do not know how)
Related
It's been awhile since I asked a question!
I'm developing an application in Java where JLabels are used. Anyway, as you may or may not be able to see from the picture below, the text that says Category Test gets cut off and ends up saying Categor... instead. Is there any way that I can "break" the text once it fills up the width of the label? Here is the image:
What I did
I used a JTextPane like so:
JTextPane text = new JTextPane();
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
pane.add(text, c);
Then I added my text, but thanks anyway!
JLabels can't do that by default. But JLabels have some support for html, so a JLabel with the text <html>First Line<br />Second Line</html> would show up on two lines.
If you want a component that can split the lines by itself, take a look at JTextArea.
As I recall, you need to use a JTextArea if you want textwrap. JLabel doesn't do it.
You can at StyledLabel component from JIDE Common Layer open source project at http://java.net/projects/jide-oss/.
The problem with html JLabel approach is it doesn't auto-wrap and about 20 to 40 times slower than a plain JLabel.
The problem with JTextArea or JTextPane approach is it has a weird size issue and is also 20 times slower.
StyledLabel extends JLabel. Automatically line wrapping is just one of the many features it adds. And the performance is as fast as a plain JLabel.
Hope it will help.
Forgive me if this question is trivial. I am not a GUI expert. However, i need to create a Swing control in which the text will only be inserted programmatically based on some calculation (user input never allowed). Are there any preferences here to use JLabel vs. noneditable JTextField? I understand that both will work, but just curious if it is better to use one or another in such circumstances.
Unlike a JLabel, a non-editable JTextField can still be scrolled horizontally to see text that would be replaced by ellipses in a JLabel. More on the latter can be seen here.
The difference is how they look and behave. Also you can cut(&paste) from JTextField.
i don't know how to write a jtextfield on a vertical alignment, i can't find in the alignment option. Thanks
JTextField is when you only need one line. User name and password fields for example.
JTextArea is when you need multiple rows.
See here and here
There is no way to make JTextField vertical see the documentation
So, I am suggesting you to use JTextArea
I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue
My original plan today was to learn how to use the JProgressBar, but now I am stuck with a new problem which I was not expecting. So here is how my JFrame looks like right now.
The problem is that the JTextField right under the JTextArea is not wrapping around. It is going beyond the size of the JFrame. I want it to resize relative to the JFrame. How can i do that? I have tried the JLabel but it does the same thing, only it adds ellipses in the end where the text overflows.
Try using a JTextArea if you intend for the field to expand vertically to accommodate the text.
JTextFields are intended for a single line of text.