0

In this instance die1 is an Integer that is derived from a calculation elsewhere. I want the value of die1 to be visually differentiated with a border and/or larger text/different color. Is there a way to do this that does not involve having 2 separate JLabels? Thank you.

firstJLabel.setText("Die 1: " + die1);

1 Answer 1

2

Is there a way to do this that does not involve having 2 separate JLabels?

You can use HTML in a label:

firstJLabel.setText("<html><font color=\"red\">Die 1: </font>" + die1 + "</html>");

Or you could use a JTextPane and make it look like a label. It supports attributes:

JTextPane textPane = new JTextPane();
textPane.setBorder( null );
textPane.setOpaque( false );

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);

//  Add some text

try
{
    StyledDocument doc = textPane.getStyledDocument();
    doc.insertString(0, die1, null);
    doc.insertString(0, "Die 1: ", green);
}
catch(Exception) {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.