public class Text extends JPanel {
private String text;
public Text()
{
this.setPreferredSize(new Dimension(20,20));
setFont (new Font(text, Font.PLAIN, 24));
text = "";
}
public void showUnderline()
{
Hashtable<TextAttribute, Object> map = new Hashtable
<TextAttribute, Object>();
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
}
the text object will be created within another class. in that class I will need to underline it with the showUnderline method. The method seems incomplete.I'm shooting for the java exclusive approach, meaning no HTML.
How do I link the text to the showUnderline method?
What do you mean 'java exclusive approach, meaning no HTML' ? You probably are looking for a JLabel, and you can put very simple html in it. Here's the first result on google:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html
It has an example of making text different colors, fonts, and bold or italicized. You could probably just do something like:
JLabel label = new JLabel("<u>MY TEXT</u>",JLabel.CENTER);
From there, you can place it like you would place any other JComponent.
If you really don't want HTML, you could use a JTextPane. Here's an example:
http://www.exampledepot.com/egs/javax.swing.text/style_hilitewords2.html
Related
I'm basically just trying to get text from a text area and then display it on a Label in Bold format. Any suggestions? This is what the code looks like, but obviously it's not correct.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sInput = TF_INPUT.getText();
TA_OUTPUT.setText(Font.Bold,sInput);
}
Don't guess at what methods to call or how and what parameters to pass in -- that's what the Java API is for -- to tell exactly what's available. If you did this and looked up JLabel, you'll see that it has a setFont(...) method that it gains from its JComponent parent and which you can and should use to set the font. Then look up Font to see what constructors are available (I often use the String, int, int constructor). So it could be something like:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sInput = tfInput.getText();
taOutput.setText(sInput);
taOutput.setFont(new Font(Font.DIALOG, Font.BOLD, 24));
}
Also you can re-use a component's font by calling getFont() on it and then deriveFont(...) on the Font to make it bold or change its size.
Actually, JLabel supports HTML. So everything you need to do is wrap your text in <b> tags.
I am new to java swing and I am creatin a Jlabel as follows :
JLabel Lport = new JLabel ("Port: ");
final JTextField Tport = new JTextField ("1883", 10);
what i want to do is to get the name of the label as a string because i want to use it in a switch-case, so i need to get the label name or a unique identifier of that label, some thing like an ID as it exists in Android, i tried the method ",getAction.toString", ".getName" but none of them displayed the name of the labe, which is according to the code posted is "Port: ". please see my attempts below:
if ( (isIPReady(Tip)) && (isPortReady(Tport)) ) {
Thread mqttThread = new Thread(MQTTRunnable, MQTT_THREAD);
mqttThread.start();
System.out.println("Action: " + Tport.get); //here i do not know which method to use
setViewEnableState(Bconnect, true);
}
The short answer is to use JLabel#getText which will return the text which is displayed by the JLabel.
An alternative could be to store your own key-value pair into the different JComponent instances. Each JComponent allows to put and retrieve client properties. A copy-paste from the class javadoc:
Support for component-specific properties. With the
putClientProperty(java.lang.Object, java.lang.Object) and
getClientProperty(java.lang.Object) methods, you can associate
name-object pairs with any object that descends from JComponent.
This would allow you to write:
private static final String ID_KEY = "MyUniqueIDKey";
JLabel label = new JLabel( "Whatever" );
label.putClientProperty( ID_KEY, "labelName" );
and then later on
String labelName = (String) label.getClientProperty( ID_KEY );
Note that this works with any JComponent, including JLabel and JTextField instances like the ones you are using in your code.
JLabel's name is different than the text it displays. To get the text from a JLabel, use getText().
You mention you want the name of the label but in your example you're calling a get on your textfield.
This applies to both a textfield and a label anyway.
That constructor sets the initial text that will display in the text field (or label).
If you want to set a name, you must first set it using setName() then use getName().
I have a JTextPane and I have some text within that JTextPane. However, because I have been using HTML within the Pane, the text seems to have been automatically changed to Times New Roman.
I'm trying to set the font type within the JTextPane to the default font of the GUI (the font of the JTextPane when it's not HTML). However I can't just set the font to one font because it differs from operating system, therefore I want to find a way to get the default font and then change the text I have to the default font.
To demonstrate how the text is swapped to Times New Roman when converted, the following code is the format I have used. How could I change it to achieve my goal?
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class GUIExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My App");
frame.setSize(300,300);
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><b>This is some text!</b></html>");
frame.add(pane);
frame.setVisible(true);
}
}
Thanks!
The following will do the trick:
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
(Note that JTextPane extends JEditorPane.)
Update (Aug 2016):
For the setting to survive Look & Feel and system changes (e.g. Fonts changed in the Windows Control Panel) the line can be placed here:
#Override
public void updateUI() {
super.updateUI();
putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
}
(This is also called during construction.)
Simplest way is probably something like this:
string fontfamily = pane.getFont().getFamily();
That will give you the default font. Then just apply it using CSS:
pane.setText("<html><body style=\"font-family: " + fontfamily + "\"<b>This is some text!</b></html>");
The JComponent html renderer uses its own font, and not that of the JComponent. In order to get the component to render the same, you need to set the font attributes in the html string. Among other things, you will need to set the font family, size, bold/italics, etc.
As an example, you could do the following:
JTextPane pane = new JTextPane();
Font font = pane.getFont();
pane.setContentType("text/html");
pane.setText("<html><font face=\"" + font.getFamily() + "\" size=\"" + font.getSize() + "\"></font>This is some text!</html>");
It would be fairly trivial to create a function that does this for you. Pass it a JComponent and a string, and it would create the html text for you, including all the font tags.
How do I get a JLabel displaying a HTML string to appear greyed out (which is the behaviour of JLabels that don't display HTML text)? Is there another way than actually changing the colour myself by modifying the foreground property?
JLabel label1 = new JLabel("Normal text");
JLabel label2 = new JLabel("<html>HTML <b>text</b>");
// Both labels are now black in colour
label1.setEnabled(false);
label2.setEnabled(false);
// label1 is greyed out, label2 is still black in colour
Thank you very much for all of your responses. From what I gather, it seems that Java doesn't support automatic greying out of JLabels when they use HTML text. Suraj's solution has come closest to the fix considering the limitations.
I have however, tried a different out-of-the box approach, where I have put the HTML text JLabels inside of an inner JPanel and did this:
mInnerPanel.setEnabled(shouldShow); //shouldShow is a boolean value
Which hasn't worked. Any suggestions for this way?
EDIT: Added implemented solution.
If text is HTML, the text wont be grayed out because of the following code in BasicLabelUI#paint()
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
}
As you can see if the text is html, then the View is used to paint and it is not checked wheter the label is enabled or not.
Hence we need to do it explictly as shown below:
label2.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (!evt.getPropertyName().equals("enabled"))
return;
if (evt.getNewValue().equals(Boolean.FALSE))
label2.setText("<html><font color=gray>HTML <b>text</b></html>");
else
label2.setText("<html><font color=black>HTML <b>text</b></html>");
}
});
Implemented solution:
Color foreground = (shouldShow) ? SystemColor.textText : SystemColor.textInactiveText;
for (Component comp : mInnerPanel.getComponents())
{
comp.setForeground(foreground);
}
Caved in and used setForeground in the end, as it appears that Java seems to explicitly ignore the enabled property when painting JLabels so long as it contains HTML text. See also #Suraj's answer, for "pure" solution.
I would suggest the following, which is combination of two solutions provided here:
public class HtmlLabel extends JLabel{
public void setEnabled(boolean enabled){
if(getClientProperty(BasicHTML.propertyKey) != null ){
Color foreground = (enabled) ? SystemColor.textText : SystemColor.textInactiveText;
setForeground(foreground);
}
super.setEnabled(enabled);
}
}
You can specify the font color in the HTML.
Override the paint method in the UI, set the client property BasicHTML.propertyKey to null if it is disabled and call super...
Is it possible to enable the selection of text from a JLabel? If not, what's the best alternative control to use, and how can it be configured to appear like a JLabel?
A JTextField doesn't allow html-formatted text like a JLabel. If you want selectable html text you could alternatively try a JTextPane set to html formatting:
JTextPane f = new JTextPane();
f.setContentType("text/html"); // let the text pane know this is what you want
f.setText("<html>Hello World</html>"); // showing off
f.setEditable(false); // as before
f.setBackground(null); // this is the same as a JLabel
f.setBorder(null); // remove the border
You can use a JTextField without enabling the editing
JTextField f=new JTextField("Hello World");
f.setEditable(false);
content.add(f);
Pierre
Building on the answers:
You can use a JTextField without enabling the editing
JTextField f=new JTextField("Hello World");
f.setEditable(false);
f.setBackground(null); //this is the same as a JLabel
f.setBorder(null); //remove the border
I don't know how to stop the text from "Jumping" when you select it, or replace the text (programmatically). Maybe it is just my computer...
When using JTextField, you will also want to remove the border:
f.setBorder(null);
and set the disabled text color: f.setDisabledTextColor(Color.black);
As variant below CopyableLabel supports html tags and Fonts as JLabel.
public class CopyableLabel extends JTextPane {
private static final long serialVersionUID = -1;
private static final Font DEFAULT_FONT;
static {
Font font = UIManager.getFont("Label.font");
DEFAULT_FONT = (font != null) ? font: new Font("Tahoma", Font.PLAIN, 11);
}
public CopyableLabel() {
construct();
}
private void construct() {
setContentType("text/html");
setEditable(false);
setBackground(null);
setBorder(null);
putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
setFont(DEFAULT_FONT);
}
}
JLabels cannot be editable.
However, you could use a JTextField and just change the foreground / background colors to make it appear as a JLabel. If you wanted to be really fancy you could add code to change the colors when it's selected to indicate that it's editable.
Besides the changes suggested in other responses (setEditable, setContentType, setOpaque or setBackground, maybe setEnabled + setDisabledTextColor(Color.black), maybe setBorder(null) and/or setMargin(new Insets(0,0,0,0))
To get the font of a JTextPane to look like a JLabel, see the suggestion from this blog post:
"Unfortunately, simply calling set font on JEditorPane will have no effect, as the default font is pulled from a style sheet rather than the JComponent. There is, however, a clever way around the errant font default. The best way to change the default font in an HTML rendering JEditorPane, is to alter the style sheet like this:"
// create a JEditorPane that renders HTML and defaults to the system font.
JEditorPane editorPane =
new JEditorPane(new HTMLEditorKit().getContentType(),text);
// set the text of the JEditorPane to the given text.
editorPane.setText(text);
// add a CSS rule to force body tags to use the default label font
// instead of the value in javax.swing.text.html.default.csss
Font font = UIManager.getFont("Label.font");
String bodyRule = "body { font-family: " + font.getFamily() + "; " +
"font-size: " + font.getSize() + "pt; }";
((HTMLDocument)editorPane.getDocument()).getStyleSheet().addRule(bodyRule);