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.

Recommended Answers

All 5 Replies

Read the API doc for the String class. Does it have a setFont method()?

To find which classes have the setFont() method go to the API doc page. At the top of the page is a link labeled: INDEX

Click that and then click on the letter S.
Do a find for setFont to find the classes using that method.

Also
If you take the time to read the API doc for JList you will find an explanation with examples of how to use a ListCellRenderer to format JList entries any way you want.

as JamesCherrill wrote, that possible

but this idea isn't easy job, beacuse wrong implemntations can freeze whole JList contents for a few moments

good luck

Member Avatar for hfx642

Does a number have a colour? No???
A String doesn't have a font either.
They are (both) just value holders.
You can set the font (and colour) of a JLabel though.
Only components have properties, not variables.

Thanks guys!

- WolfShield

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.