Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2008, 12:28 PM
Member
 
Join Date: Feb 2008
Posts: 4
JavaInLove is on a distinguished road
How to change font/ font color etc in a graphic object using JCombobox?
Hello

My program im writing recently is a small tiny application which can change fonts, font sizes, font colors and background color of the graphics object containing some strings. Im planning to use Jcomboboxes for all those 4 ideas in implementing those functions. Somehow it doesnt work! Any help would be grateful.

So currently what ive done so far is that: Im using two classes to implement the whole program. One class is the main class which contains the GUI with its components (Jcomboboxes etc..) and the other class is a class extending JPanel which does all the drawing. Therefore it contains a graphics object in that class which draws the string. However what i want it to do is using jcombobox which should contain alit of all fonts available/ font sizes/ colors etc. When i scroll through the lists and click the one item i want - the graphics object properties (font sizes/ color etc) should change as a result.

What ive gt so far is implemented the jcomboboxes in place. Problem is i cant get the font to change once selecting an item form it.
Another problem is that to set the color of font - i need to use it with a graphics object in the paintcomponent method. In this case i dnt want to create several diff paint.. method with different property settings (font/ size/ color)

Below is my code; perhaps you'll understand more looking at code.

Code:
public class main... Color[] Colors = {Color.BLUE, Color.RED, Color.GREEN}; ColorList = new JComboBox(Colors); ColorList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { JComboBox cb = (JComboBox)ev.getSource(); Color colorType = (Color)cb.getSelectedItem(); drawingBoard.setBackground(colorType); } });;
1) providing the GUI is correctly implemented with components
2) Combobox stores the colors in an array
3) ActionListener should do following job: (but cant get it right - that is where my problem is)

- once selected the item (color/ font size etc... as i would have similar methods for each) i want, it should pass the item into the drawingboard class (JPanel) and then this class should do the job.


Code:
public class DrawingBoard extends JPanel { private String message; public DrawingBoard() { setBackground(Color.white); Font font = new Font("Serif", Font.PLAIN, fontSize); setFont(font); message = ""; } public void setMessage(String m) { message = m; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); //setBackground(Color.RED); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint g2.drawString(message, 50, 50); } public void settingFont(String font) { //not sure how to implement this? //Jcombobox should pass an item to this //it should match against all known fonts in system then set that font to the graphics } private void settingFontSize(Graphics g, int f) { //same probelm with above.. } public void setBackgroundColor(Color c) { setBackground(c); repaint(); // still not sure if this done corretly. } public void setFontColor(Color c) { //not sure how to do this part aswell. //i know a method " g.setColor(c)" exist but i need to use a graphics object - and to do that i need to pass it in (then it will cause some confusion in the main class (previous code) }
My problems have been highlighted in the comments of code above.
Any help will be much appreciated thanks!!!

Last edited by JavaInLove : 02-07-2008 at 12:32 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set font size for a xml value bala_kj Advanced Java 3 04-01-2008 12:00 PM
Swing - Changing component default font Java Tip Java Tips 0 03-11-2008 11:52 PM
JTable with Font Rama Koti Reddy AWT / Swing 1 12-12-2007 06:22 PM
Help with save font information in jtextarea valery New To Java 1 08-06-2007 09:22 PM
Font question orchid Java 2D 1 08-01-2007 03:03 AM


All times are GMT +3. The time now is 12:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org