Reputation: 157
I'm making a wordpad program and have tried to create a JComboBox to change font size. It runs/compiles and shows my options, but when I click on an index the font size won't change. This is the only method that modifies font size, any suggestions to make it work?
fontsize.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(fontsize.getSelectedIndex()==0)
{
txt.setFont(txt.getFont().deriveFont(10));
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==1){
Font fontss = txt.getFont();
Font biggerFont = fontss.deriveFont(10);
txt.setFont(biggerFont);
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==2){
Font fontss = txt.getFont();
Font biggerFont = fontss.deriveFont(10);
txt.setFont(biggerFont);
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==3){
Font fontss = txt.getFont();
Font biggerFont = fontss.deriveFont(10);
txt.setFont(biggerFont);
txt.setLineWrap(true);
}
else{}
}
}
);
Upvotes: 1
Views: 776
Reputation: 11
Use the deriveFont method that takes a float as argument, this one will create a new Font with the same parameters but a new size.
Font biggerFont = fontss.deriveFont((float)10);
Upvotes: 1
Reputation: 91
This is how I would solve this problem using a HashMap.
fontsize.addActionListener(
new ActionListener(){
HashMap<Integer, Integer> fontSizeMap = new HashMap<Integer, Integer>();
int max = 4;
int minSize = 10;
{
for(int i = 0; i< max; i++) {
int fontSize = newSize + (2 * i);
fontSizeMap.put(i, fontSize);
}
}
public void actionPerformed (ActionEvent e) {
Font currFont = txt.getFont();
int index = fontsize.getSelectedIndex();
if(fontSizeMap.get(index) != null)
{
int newSize = fontSizeMap.get(index);
Font myIdea = new Font(currFont.getFontName(),currFont.getStyle(),newSize);
txt.setFont(myIdea);
txt.setLineWrap(true);
} else {
// do nothing or handle this case differently.
}
}
});
Upvotes: 0
Reputation: 17474
Why won't this font change size?
Because you didn't change the size at all.
You may want to have a look at the Java API for Font and see what deriveFont(int)
actually means.
What you are supposed to place in the parameter list for deriveFont(int style)
is the font style, not the font size. That is why are not able to change your font size.
public Font deriveFont(int style)
Creates a new Font object by replicating the current Font object and applying a new style to it.
Parameters: style - the style for the new Font
If you want to change the font size from your current font, you can do this:
Font currFont = txt.getFont();
int newSize = 20;
txt.setFont(new Font(currFont.getName(), currFont.getStyle(), newSize));
Upvotes: 1
Reputation: 157
Here is a working version of the code above. JTextArea.setFont can only take one parameter which is a font, you have to create a new font with the JTextArea's current font and then set sai font as the parameter for
JTextArea.setFont(myFont);
fontsize.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{Font currFont = txt.getFont();
if(fontsize.getSelectedIndex()==0)
{
int newSize = 10;
Font myIdea = new Font(currFont.getFontName(),currFont.getStyle(),newSize);
txt.setFont(myIdea);
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==1){
int newSize = 12;
Font myIdea = new Font(currFont.getFontName(),currFont.getStyle(),newSize);
txt.setFont(myIdea);
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==2){
int newSize = 14;
Font myIdea = new Font(currFont.getFontName(),currFont.getStyle(),newSize);
txt.setFont(myIdea);
txt.setLineWrap(true);
}
else if(fontsize.getSelectedIndex()==3){
int newSize = 16;
Font myIdea = new Font(currFont.getFontName(),currFont.getStyle(),newSize);
txt.setFont(myIdea);
txt.setLineWrap(true);
}
else{}
}
}
);
Upvotes: 0
Reputation: 91
The argument to fontss.deriveFont(10);
is always 10. Instead of hardcoding a number as is done in this case, probably a copy-and-paste error, it might be beneficial to use a map to associate index with font size and use the index (as key) to access the font or font size.
Upvotes: 0