Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting font in message dialog box 1

Status
Not open for further replies.

tbuch

Programmer
Oct 17, 2000
80
US
Could someone please tell me how to set the font in a message dialog box? Just on a hunch, this is what I tried, but to no avail.

Code:
JOptionPane jopt = new JOptionPane();
String result;
result = "Print this string.";
jopt.setFont(new Font("Monospaced", Font.BOLD, 12));
jopt.showMessageDialog( null, result, "Results", JOptionPane.PLAIN_MESSAGE );

Any help would be appreciated.
tbuch
 
You can use a JLabel instead of a String to pass as the "message" to your JOptionPane. And set the Font on the JLabel. I do not know whether the Font set by the setFont method in JOptionPane is used (the method is inherited from Component). You can have many components on a JOptionPane, so being able to set the Font on the individual components gives you more flexibility.
Code:
    JOptionPane jopt = new JOptionPane();
    String result;
    result = "Print this string.";
    JLabel resLabel = new JLabel(result);
    resLabel.setFont(new Font("Monospaced", Font.BOLD, 50));
    //jopt.setFont(new Font("Monospaced", Font.BOLD, 50));
    jopt.showMessageDialog( null, resLabel, "Results", JOptionPane.PLAIN_MESSAGE );
 
Thanx, Hologram...That fixed me right up. The only other problem was that I needed multiple lines. I achieved this using HTML tags in my String.

tbuch
 
The "message" you pass to JOptionPane can also be an array. For example an array of JLabels :
Code:
  public void doIt() {
    JOptionPane jopt = new JOptionPane();
    JLabel[] labels = new JLabel[4];
    labels[0] = new JLabel("First line BOLD 50");
    labels[0].setFont(new Font("Monospaced", Font.BOLD, 50));
    labels[1] = new JLabel("Second line ITALIC 30");
    labels[1].setFont(new Font("Monospaced", Font.ITALIC, 30));
    labels[2] = new JLabel("Third line PLAIN 30");
    labels[2].setFont(new Font("Monospaced", Font.PLAIN, 30));
    labels[3] = new JLabel("Third line BOLD ITALIC 50");
    labels[3].setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 30));
    jopt.showMessageDialog( null, labels, "Results", JOptionPane.PLAIN_MESSAGE );
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top