jlabel setLineWrap - outgoing text - java

How to setLineWrap, I'm according to https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#setLineWrap%28boolean%29
but how I can setLineWrap to jlabel, I have something like this:
String a = "text (...)";
JLabel label = new JLabel(a);
but my text is leaving
I mean:
JLabel:
aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx where a is text and x is text that disappeared
JTextArea:
aaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa

There is no setLineWrap method in JLabel. But if you set HTML to the JLabel you can overcome this.
JLabel l = new JLabel("<html><p>line 1</p><p>line 2</p></html>");

You can actually use a JTextField and make it readonly to look like a Label. When you make the text field readonly, the long text can be scrollable using keyboard.
JTextField txtLabel = new JTextField();
txtLabel.setEditable(false)
txtLabel.setText("aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx");
If you want the text to wrap you may want to use the JTextArea by making it readonly with a label look and feel.

Related

Java : JLabel with text and icon

I would like to ask if it is possible in a single JLabel to have text icon text, what i mean is the icon to be in the center of my String text.
I managed to move text left or right of the icon but i cant figure out how to put the icon in the middle.
icon to be in the center of my String text
A JLabel has text and icon - you can have the label on top of the text, but not two text's and one icon. You can achieve the same look with 3 JLabel's together in the proper Layout. For example, using a BoxLayout:
Box box = Box.createHorizontalBox();
box.add(new JLabel("Text 1"));
JLabel image = new JLabel();
image.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
box.add(image);
box.add(new JLabel("Text 2"));
Alternatively, if you wish the text to be on top of the image you can do so by setting the appropriate alignments:
JLabel label = new JLabel();
label.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
label.setText("My Text");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
I would like to ask if it is possible in a single JLabel to have text icon text
A JLabel can display simple HTML:
String html = "<html>before <img src=\"file:someFile.jpg\">after</html>";
JLabel label = new JLabel( html );
but HTML takes longer to render so you may want to consider the BoxLayout approach.

Set the font of texts retrieved from JTextField

I have the user enter some words through a JTextField. I want to set the font of the string. Here is what I have so far.
Font f;
f = new Font(input.getText(), Font.ITALIC, 32);
word = new JLabel(f, SwingConstants.CENTER);
Unfortunately, Java is throwing me a compiler error because JLabel doesn't accept Font as a parameter. Is it possible to set the font of a string retrieved from a text field and have it displayed on a JFrame?
See JComponent.setFont(Font).
Sets the font for this component.
But better than a text field to set a font name, see this answer for a (styled) combo or this answer that uses a list:

How to set String's font size, style in Java using the Font class?

Suppose I have a String, "Hello World". I want to change the style of this string to a BOLD font, and set the size of all the characters from 12 to 18 [pt]. After that I want to use this string in a JLabel and JButton. How can I do that?
Font myFont = new Font("Serif", Font.BOLD, 12);, then use a setFont method on your components like
JButton b = new JButton("Hello World");
b.setFont(myFont);
Look here http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont%28float%29
JComponent has a setFont() method. You will control the font there, not on the String.
Such as
JButton b = new JButton();
b.setFont(b.getFont().deriveFont(18.0f));

How do I put html in a JLabel in java?

How do I use html tags in a JLabel in java?
To put html in a JLabel, you would make it look something like this
JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");
This will do the trick:
String labelText ="<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>";
JLabel coloredLabel =new JLabel(labelText);
There are following ways
Using SetText method of JLabel Object
JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");
Passing String to JLable class Constructor.
JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");
Using String and passing it to JLabel class Constructor similar to above example but using String.
String HTMLlabelStr = "<html><tag>blah blah</tag></html>";
JLabel HTMLlabel = new JLabel(HTMLlabelStr);
This should do the trick:
JLabel whatever =
new JLabel("<html><something>Put Stuff Here</something></html>");
Also you can use this with all Swing buttons, menu items, labels, text panes, editor panes, tool tips, tabbed panes etc...
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");
JLabel myHTMLLabel =new JLabel("<html>");
myHTMLLabel.setText("<html><font color='green'>Hello World</font>");

Dynamic text box in Java

how do I make a text box which the user can insert text into, then that text can be saved to some variable?
JTextField is probably the class you are looking for.
JTextField textField = new JTextField();
yourPanel.add(textField);
This will add the textField into your JPanel. Then at any point in your code where you have a handle to your textField, call getText(); of your JTextField.
String s = textField.getText();
See this tutorial for a better reference:
http://download.oracle.com/javase/tutorial/uiswing/index.html
A JTextField or JTextArea will do what you are asking for, but you'll need either a button or a listener to actually know when to save this to a String.
javax.swing is Event based, which means that you cannot extract the text like this:
JTextField myField = new JTextField();
//wait for user input
String s = myField.getText(); //not guaranteed to work!
Instead, you may want to make a "Submit" button that will send the text to your program when it is clicked:
http://download.oracle.com/javase/tutorial/uiswing/components/button.html

Categories