views:

39

answers:

1

I am trying to Create a Simple Notepad in Java, i would post the full code but i didnt think it would be necessary since the problem is here(i think). Please Help.

    if(cb.getSelectedItem().equals("Plain")){
  MainText.setFont(new Font(getFontName(MainText),Font.PLAIN,getFontSize(MainText)));}

here are the above used methods

public int getFontSize(TextArea t){
    return t.getFont().getSize();
}
public String getFontName(TextArea t){
    return t.getFont().getFontName();
}
public int getFontStyle(TextArea t){
    return t.getFont().getStyle();
}
A: 

Setting the Font the way you have it is perfectly fine. You can also do it like this:

 MainText.setFont(MainText.getFont().deriveFont(Font.PLAIN));

As the Font code is fine, you should ensure that your cb.getSelecedItem test is working as expected. Perhaps stepping through or including some debug statements would be a good next step.

Also, note that in Java it is convention to begin variable names with lower case letters. Upper is used to start class names.

akf
Thank you, you guys always help me so much when i'm stuck, i really appreciate your help, i'm going to try this right now. Yeah i know what the convention is for naming variables , but sometimes when i'm in a rush i don't quite follow it, when the program is finished i'll be doing some code cleaning though.Thank you.
JIM
also i think you meant MainText.getFont().deriveFont(Font.PLAIN), thank you.
JIM
that is right, i did mean to use `getFont()` in the middle there. updated.
akf
How does being in a rush prevent you from using "mainText" instead of "MainText". In fact its easier to type "m" instead of "M" because you don't need to hold down the Caps key at the same time. Sloppy coding results is silly problems. Do it right the first time especially if you expect us to help you in the future by reading your code.
camickr