Hello all,
I have a window with a JList inside it. I would like each line in the JList to show up
in a certain font. But when I try the 'setFont(font)' method where 'font' has already been
defined it say that it cannot find the symbol 'setFont(java.awt.Font)'. Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
public class chooseFont {
public static void createAndShowGUI() {
Font font = new Font("TimesRoman",Font.PLAIN,10);
// Create Window
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
String fontString[] = {"Helvetica".setFont(font), "Frutiger", "Myriad Pro", "Avenir Std",
"Trajan", "Optima Std", "ITC Franklin Gothic Std", "Futura",
"Bickham Script", "Univers", "Eurostile", "Interstate", "Trade Gothic",
"Gill Sans", "Warnock", "Kepler", "Bodoni", "Bembo", "Rockwell",
"Meta", "Gotham"};
JList fontList = new JList(fontString);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.setLayoutOrientation(JList.VERTICAL);
JScrollPane fontSP = new JScrollPane(fontList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
fontSP.setPreferredSize(new Dimension(150, 90));
c.gridy=0;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 15, 25);
content.add(fontSP, c);
JButton changeFont = new JButton("Change Font");
c.gridy=0;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(changeFont, c);
JLabel exampleText = new JLabel("This is the example text");
c.gridy=1;
c.gridx=0;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 15, 0);
content.add(exampleText, c);
JButton okB = new JButton("Okay");
c.gridy=2;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(okB, c);
JButton cancelB = new JButton("Cancel");
c.gridy=2;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(cancelB, c);
JFrame fontWin = new JFrame("JPad v0.1: Change Text Font");
fontWin.setSize(300, 200); // 300, 200
fontWin.setLocation(100, 100);
fontWin.setContentPane(content);
fontWin.setResizable(false);
fontWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fontWin.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
So, what am I doing wrong? I thought it might be that I was calling the method on a string
so I changed it to an Object array, but that still gave the save error.
- WolfShield
P.S. Yes, I know I'm calling 'TimesRoman' on 'Helvetica'. I'm just trying to get the setFont()
method to work, then change all of the fonts to what I need.