Results 1 to 6 of 6
  1. #1
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default 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.

    Java 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.


    Java 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 11:32 AM.

  2. #2
    sletlyenlaree is offline Member
    Join Date
    Mar 2009
    Location
    Portugal
    Posts
    1
    Rep Power
    0

    Default Hello From Portugal

    Hello, I'm new here :D
    Thanks for all poster who have posted useful information here

    sletlyenlaree from Elvas, Portugal

  3. #3
    Join Date
    Apr 2009
    Location
    Portugal
    Posts
    2
    Rep Power
    0

    Default I'm New Here

    Hello,
    I love this place and willing to learn a lot from the members here.

  4. #4
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default First time, great to be here!

    Hope everyone is doing excellent today

    Excited to be here!

  5. #5
    Join Date
    Apr 2009
    Location
    Portugal
    Posts
    2
    Rep Power
    0

    Default I'm New Here

    Hello,
    What a forum, this forum surely will get many quality members for not too long :p

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. how to set font size for a xml value
    By bala_kj in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2008, 11:00 AM
  2. Swing - Changing component default font
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 10:52 PM
  3. JTable with Font
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 1
    Last Post: 12-12-2007, 05:22 PM
  4. Help with save font information in jtextarea
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:22 PM
  5. Font question
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •