Bishal Gautam Bishal Gautam - 9 months ago 28
Java Question

setting the font name, style and size for the jtextarea

I have a pop up to select font for the user . I have listed them in 3 JList. From the 3 list they will select the "Font Family", "Font Size" and "Font Style".
I have this code:

Fonts f= new Fonts();
int result=JOptionPane.showConfirmDialog(null,f,"Fonts",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
String fontium=f.getFonts();
int size=Integer.parseInt(f.getSizes());
String style=f.getStyle();
System.out.println("you selected \n Font:" + fontium + "\n size:" + size + "\n style:" + style);
Font font=new Font(fontium, style, size);


The problem i'm having is style comes in string but style accepts int. How can i solve this.


I really don't know what Fonts class does and what the style value returns.

There are 2 possible results of these.

  • If style value returns "0"or "1" or "2", then you can convert the value to String just like this.

    String style=f.getStyle(); int fontStyle = Integer.parseInt(style);

  • If style value returns styleName such as "bolditalic" or "italic" or "bold" or "plain", then you need to control the value like this

int fontStyle = 0; if (style.equals("bolditalic")) { fontStyle = Font.BOLD | Font.ITALIC; } else if (style.equals("italic")) { fontStyle = Font.ITALIC; } else if (style.equals("bold")) { fontStyle = Font.BOLD; } else if (style.equals("plain")) { fontStyle = Font.PLAIN; }