d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Question
12Next
Add Reply New Topic New Poll
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 04:40pm
I need to change fonts using Choice Boxes:

Is this how I would declare the fonts i need to use?

Code

Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
Font Courier = new Font ("Courier", typeStyle, typeSize);
Font Dialog = new Font ("Dialog", typeStyle, typeSize);


Also how would I add these to a choice box.

I can post whole code if needed. ( here it is : http://pastebin.com/y4xeB0Cw )

Any help is appreciated.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2013 05:10pm
why are you using AWT? last i heard it was replaced by swing in java 1.5

but anyway, have you looked at examples of how to use Choice?

http://java.comsci.us/examples/awt/Choice.html

font declaration looks fine to me off hand, but i havent memorized the constructor. you can look it up if you need it. http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 05:38pm
Quote (carteblanche @ Sep 24 2013 07:10pm)
why are you using AWT? last i heard it was replaced by swing in java 1.5

but anyway, have you looked at examples of how to use Choice?

http://java.comsci.us/examples/awt/Choice.html

font declaration looks fine to me off hand, but i havent memorized the constructor. you can look it up if you need it. http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html


I am just using the code outline given by the professor, our homework is to make changes to it.

I will look at the examples and see if it helps me out.

Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 05:58pm
I understand kind of what i need to do just the syntax isn't clear to me even after looking at the links.

Code

Label label1 = new Label("Select a Font style");
panel.add(label1);
faceFont = new Choice();
setFont (TimesRoman);
setFont (Courier);
setFont (Dialog);
panel.add(faceFont);
faceFont.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
}
});


above is where I want to add the color choices, just not sure how to put it in the proper syntax (color choices was much easier)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2013 06:33pm
if you're following that model, put your fonts in an array.

Font[] fonts = new Font[3];
fonts[0] = times;
....

public void itemStateChanged(ItemEvent event) {
// selected font is in fonts[mychoice.getSelectedIndex()] so do whatever you want with it

}
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 07:27pm
attempted making the array and it isn't working.

Code
Panel panel1 = new Panel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
controlPanel.add(panel1);
Label label1 = new Label("Select a typeface");
panel1.add(label1);
faceFont = new Choice();
faceFont.add(FONT_NAMES[0]);
faceFont.add(FONT_NAMES[1]);
faceFont.add(FONT_NAMES[2]);
panel1.add(faceFont);
faceFont.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
}
});


also the last line i try and text.setFont etc and it doesn't work.













i think the only thing that is broken now is
"The method setFont(Font) in the type Component is not applicable for the arguments (String)"
error with
Code

text.setFont(FONTS[faceFont.getSelectedIndex()]);


This post was edited by SoftSpeaker on Sep 24 2013 07:42pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Sep 24 2013 07:59pm
Your FONTS array contains Strings and not java.awt.Font objects.
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 08:03pm
Quote (rockonkenshin @ Sep 24 2013 09:59pm)
Your FONTS array contains Strings and not java.awt.Font objects.


Code

static final String FONT_NAMES[] = {"TimesRoman", "Courier", "Dialog"};
static final String FONTS[] = {TimesRoman, Courier, Dialog};


String typeFace = INITIAL_FACE; // current typeface
int typeStyle = INITIAL_STYLE; // current type style
int typeSize = INITIAL_SIZE; // current type size

Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
Font Courier = new Font ("Courier", typeStyle, typeSize);
Font Dialog = new Font ("Dialog", typeStyle, typeSize);


So the declarations at the bottom don't help the ones in the array?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2013 08:22pm
Quote (SoftSpeaker @ Sep 24 2013 10:03pm)
Code
static final String FONT_NAMES[] = {"TimesRoman", "Courier", "Dialog"};
static final String FONTS[] = {TimesRoman, Courier, Dialog};


String typeFace = INITIAL_FACE;    // current typeface
int typeStyle = INITIAL_STYLE;      // current type style
int typeSize = INITIAL_SIZE;        // current type size

Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
Font Courier = new Font ("Courier", typeStyle, typeSize);
Font Dialog = new Font ("Dialog", typeStyle, typeSize);


So the declarations at the bottom don't help the ones in the array?


declarations mean nothing if you're not using them.
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 08:36pm
I'm lost once again.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll