Search code examples
javaswingfontsjtextareajoptionpane

Changing font of JTextArea in JOptionPane


I'm just wondering if you can somehow change the font of the text in showMesaggesDialog by using area.getText() (area is what i call for my JTextArea) so far i have tried this but no luck.

b.addActionListener(new ActionListener() {     
    public void actionPerformed(ActionEvent arg0) {
        String myString=area.getText();
        Font font = new Font("Verdana", Font.BOLD, 12);
        area.setFont(font);
        JOptionPane.showMessageDialog(f.getComponent(0),myString);
    }
});

Solution

  • Rather than calling JOptionPane.showMessageDialog with a String, use a JLabel.

    Try this:

    b.addActionListener(new ActionListener() {     
        public void actionPerformed(ActionEvent arg0) {
            String myString=area.getText();
            JLabel label = new JLabel(myString);
            Font font = new Font("Verdana", Font.BOLD, 12);
            label.setFont(font);
            JOptionPane.showMessageDialog(f.getComponent(0),label);
        }
    });