Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to change font/ font color etc in a graphic object using JCombobox?

843806Feb 7 2008 — edited Feb 8 2008
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.
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.

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!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 7 2008
Added on Feb 7 2008
6 comments
355 views