Solved

Simple Java Error

Posted on 2003-12-02
10
393 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 35
Chrome and Firefox Java 5 51
printf performancy 11 71
Setup GlassFish 4 22
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

828 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