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

It is the completely correct code
I hope that's what you need
Just put DrawingBoard into JFrame and run
Good luck!
public class DrawingBoard extends JPanel implements ActionListener{
     private String message = "message";
     private Font font = new Font("Serif", Font.PLAIN, 10);
     private Color color = Color.RED;
     private Color bg = Color.WHITE;
     private int size = 10;
     public DrawingBoard(){
          JComboBox cbFont = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
          cbFont.setActionCommand("font");
          JComboBox cbSize = new JComboBox(new Integer[]{new Integer(14), new Integer(13)});
          cbSize.setActionCommand("size");
          JComboBox cbColor = new JComboBox(new Color[]{Color.BLUE, Color.RED, Color.GREEN});
          cbColor.setActionCommand("color");
          JComboBox cbBG = new JComboBox(new Color[]{Color.BLUE, Color.RED, Color.GREEN});
          cbBG.setActionCommand("bg");
          add(cbFont);
          cbFont.addActionListener(this);
          add(cbSize);
          cbSize.addActionListener(this);
          add(cbColor);
          cbColor.addActionListener(this);
          add(cbBG);
          cbBG.addActionListener(this);
     public void setMessage(String m){
          message = m;
          repaint();
     protected void paintComponent(Graphics g){
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          g2.setColor(bg);//set background color
          g2.fillRect(0,0, getWidth(), getHeight());          
          g2.setColor(color);//set text color
          FontRenderContext frc = g2.getFontRenderContext();
          TextLayout tl = new TextLayout(message,font,frc);//set font and message
          AffineTransform at = new AffineTransform();
          at.setToTranslation(getWidth()/2-tl.getBounds().getWidth()/2,
                    getWidth()/2 + tl.getBounds().getHeight()/2);//set text at center of panel
          g2.fill(tl.getOutline(at));
     public void actionPerformed(ActionEvent e){
          JComboBox cb = (JComboBox)e.getSource();
          if (e.getActionCommand().equals("font")){
               font = new Font(cb.getSelectedItem().toString(), Font.PLAIN, size);
          }else if (e.getActionCommand().equals("size")){
               size = ((Integer)cb.getSelectedItem()).intValue();
          }else if (e.getActionCommand().equals("color")){
               color = (Color)cb.getSelectedItem();
          }else if (e.getActionCommand().equals("bg")){
               bg = (Color)cb.getSelectedItem();
          repaint();
}

Similar Messages

  • How to change the background color of a cell in datagrid using flex3

    i want to change the background color of a cell.....how can i achieve this.....and also i want to know how a spacing cane be done between cells in a datagrid...plzzz help me???

    The only way I can see to do this is to use an item renderer for your cells.  This is really scruffy and would need tyding up, and maybe with a little more time could do better or someone else may have an idea but none the less this works.
    Define a custom component as below;
    This has logic to see what the value of the data is proveided by the dataprovider for the row, and if it matches the conditions in this case is equal to 5 sets the background color.
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="88" height="26" dataChange="doColor()" borderColor="#000000" borderStyle="solid"
        backgroundAlpha="1">
        <mx:Script>
            <![CDATA[
                private function doColor():void {
                    if (data.value == 5) {
                        setStyle('backgroundColor', 0xcccccc);
                    } else {
                        setStyle('backgroundColor', 0xffffff);
            ]]>
        </mx:Script>
    </mx:Canvas>
    Now just apply the item renderer in the datagrid and that will do it.
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  xmlns:ns1="*">
        <mx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                [Bindable]
                private var ac:ArrayCollection = new ArrayCollection([
                    {value : 1},
                    {value : 2},
                    {value : 3},
                    {value : 4},
                    {value : 5},
                    {value : 6},
                    {value : 7},
                    {value : 8},
                    {value : 9},
                    {value : 10}
          ]]>
        </mx:Script>
        <mx:DataGrid x="40" y="36" width="408" height="193" dataProvider="{ac}">
            <mx:columns>
                <mx:DataGridColumn headerText="Column 1" dataField="value" itemRenderer="MyComp"/>
                <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
                <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
            </mx:columns>
        </mx:DataGrid>
    </mx:Application>
    I hope this helps
    Andrew

  • How do change the text color in the variable screen ?

    Hi Experts ,
    I would like to know about , How do change the text color in the variable screen ?
    Using web templates (Analytical) can get the output. It has the variable screen contains 6 fields (Company code, Country , Region , COB, Plant and Purchasing Group). I want to make RED color text on Plant. Please help me .
    Thank you ,
    Prasad.

    Hi,
    I am looking for nearly the same. What I have found is that it seems to manipulate the SAP theme that is used in standard when template is executed in the portal. Just display the source code of the HTML and there you will see the included SAP theme (normally SAP_TRADESHOW). Then you have to go to the SAP portal and change this stuff. But for that you have to know where to find it and what impact this change has.
    I am not pretty sure if this is the right way. But as I want to change the standard layout of a whole template to a customer specific layout I think there is no other way in BI7.0.
    Regards,
    Peter

  • I just want to know that how to change the font color, font size, and forgr

    i just want to know that how to change the font color, font size, and forground color in JTextPane for the Selected text. i try diffrerent menthod like setFont and setColor, these methods change the fonts of the whole textpane. so please reply me as soon as possible, and thanks in advance.
    Moazzam

    Sell the Old one... Buy a New one...

  • How to change the font, the font size, and color text

    How to change the font, the font size, and the color for a selected text in C++.
    David

    Look at the documentation in the SDK about how to build a plugin and then the PDEdit APIs available to them.

  • Win10 blurry fonts/programs | How to change the accent color?

    Hi,
    Recently I installed Win10 Build 9926 on a virtual machine for testing and feedback and I am havin' some problems...
    First, I changed the Display settings to 125% and now the third-party Win32 classic apps are looking blurry... [Sorry, i can't provide a image link]
    Second: The Personalization "pane" (Sorry, long time mac user) only has options for the lock screen and now I dont know how to change the accent color...
    And this, I can keep with it, but I made a registry tweak to enable the start screen but now when I use it the taskbar is still there!!! ANNOYING!
    Any help will be appreciated...
    Thx,
    Tiago

    Hi,
    I made a test in our testing enviroment, found that when this problem occures,
    Sign Out current user and login again would be helpful to resolve this problem. Please take this method as a workaround temporarilly.
    Second question, to modify backgound color, you can access system personalize to modify the color:
    Roger Lu
    TechNet Community Support

  • How to change the font size of text element in smartform

    Hi
    i want to know how to change the font size of text element in smartform
    regards
    Gincy

    Hi
    You can create Smartstyles, in which you can define paragraph formats for texts and character formats for character level changes.
    Paragraph format ---> alignment, font size etc
    character format for ---> superscript, subscript.
    After creating, in your smartform, there will be a option in the text element to enter a style.
    You give the created style and you can use the paragraph and character formats i that style.
    Hope this is clear..
    Regards
    Sekar

  • I like the mail feature with one exception; I cannot figure out how to change the font size of incoming mail permanently.  And, when answering an email I have to highlight the first few words and zoom it so I can see what I am writing.  what am I missing?

    I like the mail feature with one exception; I cannot figure out how to change the font size of incoming mail permanently.  And, when answering an email I have to highlight the first few words and zoom it so I can see what I am writing.  what am I missing?

    You can type the email using what you set in preferences and then highlight the text and use command - minus sign (or command - + for larger) to reduce the size of the text.
    You can also type command - T and a window will appear allowing you to select fonts/sizes/color/ background highlight.
    The above works in Notes also. I haven't tried to do this in any other Apple application.
    For incoming emails, you can use the above to reduce font size, but I don't know of a way to permanently set the incoming font size to a default.

  • How to change default fonts in Keynote?

    How to change default fonts in Keynote?

    You can't set a default font.
    Instead use a custom slide master with the font you want to use.

  • How to change the font size and style on run time

    dear all
    i try to change the font style and font size on runtime. I did the following:
    1- i created an item(:font_size) in which i will write the size of the font for the the other item ('customer_name')
    2 on the post_change trigger for 'font_size' i write this code
    SET_ITEM_PROPERTY('customer_name',FONT_size,(:font_size);
    i write 12 then then font size changed , then i write 18 , the size does not change. and when i write any value , no change happens. I do not know why
    the second problem is how to change the font style
    i made three checkbooks (bold,italic,underline)
    on the trugger when_checkbox_checked i write
         IF :BOLD = 'B' THEN
         SET_ITEM_PROPERTY('N_SAMPLE',FONT_STYLE,'BOLD');
         ELSE
    SET_ITEM_PROPERTY('N_SAMPLE',FONT_STYLE,'REGULAR');
         END IF;     
    no change happend at all.
    please help

    Hi friend,
    it's a really really strange tip... May be it's a Forms bug? I've tried with set_item_property..and.. you're right, it doesn't work..
    So.. you can try making this:
    - create a visual attribute with an specific font size....
    - use the
    SET_ITEM_INSTANCE_PROPERTY('block.item',CURRENT_RECORD,VISUAL_ATTRIBUTE,'you_visual_attribute');
    and call it from psot-change....
    It works
    Hope it helps,
    Jose.

  • How to change the font dynamically in SAP script

    Hello,
    There are three fields for a line item to be printed in SAP form (Through Script). The font of one field has to be bigger than rest of the two.
    Can anybody tell me how to change the font of a specific field in a line of an SAP-script?
    Regards
    Kiran

    Hi,
    Its like any other character format. you can have any field with a character format.
    --Ragu

  • How to change the font of the text in Control Editor??

    How to change the font of the text in Control Editor??

    Hi,
            you have to create a smartstyle and acces that in So10 transaction.in change mode,menu->FORMAT->CHANGE STYLE.
    here you will get all styles available.
    so first you have to create a style in SE72 and activate it. and then
    goto SO10-> change mode
    menu->FORMAT->CHANGE STYLE.
    now you will see the style you have created. select that.
    now you can see the format.
    <b>Reward points</b>
    Regards

  • How to change the font size of text and case in smartforms

    hi,
       could any one explain how to change the font size of text in smartforms. ex. previously i had taken P4 left aligned and C6 but now i want to decrease the font without making it bold. pl......... guide me
       secondly the value which are coming from tables are upper case but i need lower case. pl. guide
    thanking u

    check out the smart styles...create a font of ur size n use.
    Define the paragraphs & character formats using SMARTSTYLES & use them in your smartform.
    You have to give the smartstyle name in the Form attributes-->Output options for the formats to be used in your smartform.
    You can define font size in Smartform Style.
    Smartform Styles > Charcterformat> Font --> size.
    to load the font to SAP server chk this
    Re: Adding a new font for SAPscript/SMARTFORM output
    Regards
    Vasu

  • How to change the font used to fill in a text field.

    Can you please tell me how to change the font that comes up when a text field is filled in?
    Can I change this setting in InDesign or does it have to be done in Acrobat?

    It must be done in Acrobat. InDesign only allows Times Roman for some reason.
    To change it in Acrobat 11, open the PDF and select: Tools > Forms > Edit
    and select the field(s) you want to change, bring up the field properties dialog, and look on the Appearance tab for the font dropdown.

  • How to change the font size of the text that is revolving ?

    Hi there,
    I am using the Revolution theme.
    How to change the font size of the text that is revolving (on the first menu page) ?
    Thanks

    You can't.  That's hard coded into the theme.
    OT

Maybe you are looking for

  • Popup instead of list when using WD4A ALV Grid Drop Down By Key

    I am develop a WD4A application and I am using an ALV grid.  I have set the cell editor for one column wiht the following code: CREATE OBJECT lr_ddbk     EXPORTING       SELECTED_KEY_FIELDNAME = 'ZCURRENCY'.   Data: lv_key_visable TYPE abap_bool,    

  • My iPod wont charge when i play on it, or even show that it is charging, plus it wont charge over 50%.

    My iPod has recently begun to have issues. When I plug it in to a charging cord (wall adapter or computer) it won't show that it is connected. And, I can't charge it and play at the same time. Finally, it charges really slow. As in, takes at least 5

  • Hello friends while doing file to jdbc senario I am getting error

    Hello All, while doing file to jdbc senario in sap xi getting error in the receiver communication channel..error is 2007-07-17 04:11:00 Error SAPEngine_Application_Thread[impl:3]_35 1184625660906 Attempt to establish database connection failed with S

  • What is the best way to do this artwork?

    Hi, I am fairly new to Illustrator. This design is going to be used for a repeat textile/fabric design.I am trying to trace JUST the inside of the middle square, since it is all I need at this point.  BUT, since everything extends outside of the box,

  • Trying to Export my 1-1/2 hour project to iDVD

    Lost the first attempt to create this one, forty hours..files accidentally lost, now all rebuilt and don't want to mess it up this time. Will appreciate your help very much. I tried to export..chose Quicktime Movie...and having a few chapter markers,