Solved

Simple Java Error

Posted on 2003-12-02
10
391 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:mb007
Comment Utility
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
Comment Utility
>> 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mb007
Comment Utility
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
Comment Utility
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
Comment Utility
Great It works ... Thanx Alot for you help

0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
Sorry, too late.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
strDist challenge 35 83
DO we need Java installed on a Windows PC and WHY ? 13 76
eclipse formatting 6 36
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now