Solved

Simple Java Error

Posted on 2003-12-02
10
396 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:mb007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 

Author Comment

by:mb007
ID: 9864845
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
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9864859
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.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9864871
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())));
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mb007
ID: 9864882
Thenx alot that worked !!!!!

youngsing

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

0
 
LVL 9

Expert Comment

by:yongsing
ID: 9864885
>> 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));
0
 

Author Comment

by:mb007
ID: 9864897
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 ???
0
 
LVL 9

Accepted Solution

by:
yongsing earned 130 total points
ID: 9864900
You can also set the initial value when you contruct your JSpinner. The following example uses the SpinnerNumberModel. The spinner will have a minimum value of 0 and a maximum value of 100. The initial value is set to 10, and the step size is 1.

jSpinner1 = new JSpinner(new SpinnerNumberModel(10, 0, 100, 1));
0
 

Author Comment

by:mb007
ID: 9864906
Great It works ... Thanx Alot for you help

0
 
LVL 37

Expert Comment

by:zzynx
ID: 9864923
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 9864929
Sorry, too late.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question