Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple Java Error

Posted on 2003-12-02
10
Medium Priority
?
406 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
Technology Partners: 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 520 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

604 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