-
Set Fonts for items in JLIST
HELP! Trying to set the fonts for the items in a JList. The list is of available fonts for the user to choose, but want the fonts to be shown so the user does not have to guess what each one looks like.
jessiep
-
Application logic
Did you face any technical difficulties?
It seems like a pure design / login issue. What you can do is add a sample string next to each font you display. MS work like... Or set the font of the font name to the font type
Sharbov.
-
clearer explanation
well what I have tried so far was to create an array of strings that are placed into a JList, the JList is then placed into a JScrollPane. That works fine, when I try to set the strings by using setFont method I revieve this error:
EngravingApp.java [96:1] cannot resolve symbol
symbol : method setFont (java.lang.String,int,int)
location: class java.lang.String
fontNames[0].setFont("Century", Font.PLAIN, 14);
fontNames is the name of the array containing the names the user will see in the list.
I guess it would seem that since I am referencing a string in an array, I would just use the code as I have
fontNames[0].setFont("Century", Font.PLAIN, 14);
and I should be calling setFont on the String located at fontNames. array number.
I saw an example that uses Class StringData, but haven't quit figured out what is happening there either
Thanks,
Jessie
jessiep
-
The error message explains it all....
You're trying to call the method .setFont(...) on a String object! If you check the Java API (http://java.sun.com/j2se/1.3/docs/ap...ng/String.html), you will see that such a method does not exist for String.
ArchAngel.
O:-)
-
reply, any more?
ArchAngel,
your absolutly right!, I wasn't thinking that what I was doing was setting the font for the text box, not on the string it contained. So now the Q of how to use the setFont method on the LJist, which does have it, but maybe not on the individual list items, any ideas? possibly using the paint method to redraw each one with a unigue font?
thanks on the last one though
Jessie
jessiep
-
I think it probably could be done, but I think that'd take a lot more research. My suggestion is to follow sharbov's suggestion:
Render the font names in the JList in a consistent font and then have a JLabel next to the list with "Here is some sample text". When the user selects a JList entry, update the JLabel so that the text is drawn using the specified font.
I think this is a much simpler solution.
ArchAngel.
O:-)
-
yes, I have built a JLabel that holds the text that user enters into the text box, with the chosen font, so that is all good, as far as saying "it works", just was thinking it'd be better to have the fonts showing
Anyway If I do get it, I'll post the solution
Jessie
jessiep
-
Sure. It'd be great if you do.
ArchAngel.
O:-)
-
I think you can use this method:
http://java.sun.com/j2se/1.4.2/docs/...etCellRenderer(javax.swing.ListCellRenderer)
Example:
http://java.sun.com/j2se/1.4.2/docs/...nderer_example
(If you haven't been able to sort it by tomorrow, I'll have a go at coding it myself in the morning)
ArchAngel.
O:-)
-
I lied....I just wrote it:
Code:
import javax.swing.*;
import java.awt.*;
public class MyCellRenderer extends JLabel implements ListCellRenderer {
public static void main(String[] args) {
JFrame frame = new JFrame("Font JList");
// Create some fonts
fonts = new Font[] {new Font("Dialog", Font.PLAIN, 12), new Font("Dialog", Font.BOLD, 15)};
// Populate the JList
JList dataList = new JList(fonts);
// Specify the renderer
dataList.setCellRenderer(new MyCellRenderer());
// Add the JList
frame.getContentPane().add(new JScrollPane(dataList));
// Finish setting up window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// Get text to display.
String s = value.toString();
// Set the text.
setText(s);
// Get the font.
Font f = (Font)value;
// Set the font.
setFont(f);
return this;
}
}
ArchAngel.
O:-)
-
...and here's your complete component:
Code:
import javax.swing.*;
import java.awt.*;
public class FontListCellRenderer extends JLabel implements ListCellRenderer {
public static void main(String[] args) {
// Create a window
JFrame frame = new JFrame("Font JList");
// Get some fonts
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
// Populate the JList
JList dataList = new JList(fonts);
// Specify the renderer
dataList.setCellRenderer(new FontListCellRenderer());
// Add the JList to the window (inside a scroller)
frame.getContentPane().add(new JScrollPane(dataList));
// Finish setting up window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// Get text to display.
String s = value.toString();
// Set the text.
setText(s);
// Get the font.
Font f = (Font)value;
// Set the font.
setFont(f.deriveFont(15.0f));
return this;
}
}
;-)
ArchAngel.
O:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks