JList and Font compile errors
i am trying to make a program to convert the text in the JTextArea into a differend font.
but i get 2 compile errors, one for teh event listener of the JList, one for the font object.
here is my code, can someone look at it and tell me what is wrong with it?
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
publicclass Toverveer{
String[] fonts ={"avalon","bardic","Dragons"};// i asume you dont have this fonts, but that is not important
JFrame frame;
JTextArea textarea;
JList list;
Font f;
publicstaticvoid main (String[] args){
Toverveer gui =new Toverveer();
gui.go();
}
publicvoid go(){
frame =new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textarea =new JTextArea(20, 20);
list =new JList(fonts);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
JPanel panel =new JPanel();
panel.setBackground(Color.darkGray);
frame.getContentPane().add(BorderLayout.CENTER, panel);
panel.add(textarea);
panel.add(list);
frame.setSize(300,300);
frame.setVisible(true);
}
publicvoid valueChanged(ListSelectionEvent lse){
if( !lse.getValueIsAdjusting() ){
String selection = (String) list.getSelectedValue();
System.out.println(selection);
f =new Font(selection,"PLAIN", 12);
textarea.setFont(f);
}
}
}
[2765 byte] By [
senseoa] at [2007-11-27 9:13:17]

the compiler tells you what is wrong:
28: addListSelectionListener(javax.swing.event.ListSelectionListener) in
javax.swing.JList cannot be applied to (Toverveer)
so, does the class implement the ListSelectionListener Interface?
47: cannot find symbol
symbol : constructor Font(java.lang.String,java.lang.String,int)
location: class java.awt.Font
f = new Font(selection, "PLAIN", 12);
so, go to the api docs and make sure there is a constructor that satisfies
what you are supplying as arguments. If there isn't one, check what the
constructors are and modify your code accordingly (hint: in the api docs also check the fields)
about the font part:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html
the constructor looks like this:
Constructor Summary
Font(String name, int style, int size)
Creates a new Font from the specified name, style and point size.
so i create a object of font, pull the font name out of the list, set the size, only thing i think is wrong is that i said "PLAIN" instead of PLAIN.
but that gave a compile error because variable plain did not exist
i dont understand it..... tell me, what did i do wrong?
> about the font part:
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.
> html
> the constructor looks like this:
>
> Constructor Summary
> Font(String name, int style, int size)
> Creates a new Font from the specified name, style and
> point size.
>
> so i create a object of font, pull the font name out
> of the list, set the size, only thing i think is
> wrong is that i said "PLAIN" instead of PLAIN.
> but that gave a compile error because variable plain
> did not exist
>
> i dont understand it..... tell me, what did i do
> wrong?
Font.PLAIN, not just PLAIN.
PLAIN is a static constant within the Font class, it's not just a free-standing global constant.
what do you mean by that?
the program is working now, i removed the JPanel, because it was kind of useless, and added some fonts.
if i have more questions about this program, should i start a new topic for them, or should i post them here(because i don't think they'll have to do with JList or font)