Link to home
Start Free TrialLog in
Avatar of mb007
mb007

asked on

Simple Java Error

What is wrong with the following Line ??? I know its something simple.... Can anyone help me out. Thanx guys

void jButton1_actionPerformed(ActionEvent e) {
     pageTA.setFont(new java.awt.Font(jComboBox1.getSelectedItem().toString(),jComboBox2.getSelectedItem().toString(),Integer.parseInt(jSpinner1.getValue().toString())));
 }

I get the following error

"project_3.java": cannot resolve symbol: constructor Font (java.lang.String,java.lang.String,int)in class java.awt.Font at line 347, column 24

My first combo box has the fonts name,  2nd combo box has the style (BOLD,PLAIN) ect, and the spinner acts as the size of the fonts.

When I run this everything works fine except the STYLE and I want to include that in the project

void jButton1_actionPerformed(ActionEvent e) {
          pageTA.setFont(new java.awt.Font(jComboBox1.getSelectedItem().toString(),java.awt.Font.PLAIN,Integer.parseInt(jSpinner1.getValue().toString())));

  }


Thank You Marcin
Avatar of mb007
mb007

ASKER

I aslo have question about JSpinner .... How do you set the value for it so it dosen't start at 0 ... It will start at 10.

Thanx
The second parameter of the Font contructor is an integer primitive, but you pass in a String:

jComboBox2.getSelectedItem().toString()

You will have to convert your String to an integer first.
You can do it this way:

void jButton1_actionPerformed(ActionEvent e) {
  int style = Font.PLAIN;
  String selectedStyle = jComboBox2.getSelectedItem().toString();
  if (selectedStyle.equalsIgnoreCase("BOLD")) {
    style = Font.BOLD;
  } else if (selectedStyle.equalsIgnoreCase("ITALIC")) {
    style = Font.ITALIC;
  }
   pageTA.setFont(new java.awt.Font(jComboBox1.getSelectedItem().toString(), style,Integer.parseInt(jSpinner1.getValue().toString())));
}
Avatar of mb007

ASKER

Thenx alot that worked !!!!!

youngsing

Do you know how to change the value on the jSpinner so i dosn't start at 0.

>> I aslo have question about JSpinner .... How do you set the value for it so it dosen't start at 0 ... It will start at 10.

jSpinner1.getModel().setValue(new Integer(10));
Avatar of mb007

ASKER

I'm missing an  identifier

jSpinner1.getModel().setValue(new Integer(10));


"project_3.java": <identifier> expected at line 46, column 21


Were the setValue is ???
ASKER CERTIFIED SOLUTION
Avatar of yongsing
yongsing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mb007

ASKER

Great It works ... Thanx Alot for you help

Avatar of zzynx
You could store the following in the combobox:
new Integer(Font.BOLD),
new Integer(Font.PLAIN),
...
in that combo box instead of integers.

Then you could pass
                                ((Integer)jComboBox2.getSelectedItem()).intValue()
as second parameter.

Of course then you need to define a simple ListCellRenderer for your combo box to display the right strings for the style.
Something like

public class StyleListCellRenderer extends JLabel implements ListCellRenderer {
   
    public StyleStateListCellRenderer() {
        setOpaque(true);
    }
   
    public java.awt.Component getListCellRendererComponent(javax.swing.JList list,
                                                                                        Object value,
                                      int index,
                                                                                        boolean isSelected,
                                                                                        boolean cellHasFocus) {
        if  (value == null)
             setText("?");
        else {
                   switch ( ((Integer)value).intValue() ) {
              case Font.BOLD :
                            setText("Bold");
                  break;
              case Font.PLAIN :
                            setText("Plain");
                            break;
               
                        case default:
                            setText("?");
                            break;
                   }
        }
        setBackground(isSelected ?
                      list.getSelectionBackground() : list.getBackground());
        setForeground(isSelected ?
                      list.getSelectionForeground() : list.getForeground());
        return this;        
    }
}

activate it by
              ComboBox2.setRenderer(new StyleListCellRenderer());

For your spinner to start at 10:
                            mySpinner.setValue(new Integer(10));

Success
Sorry, too late.