9 Replies Latest reply on Feb 16, 2009 6:54 AM by 843851

    HGraphicButton

    843851
      Hi,

      is it possible to display text on a HGraphicButton ?

      I tried with setTextContent("string",HState.NORMAL_STATE) but that doesn't seem to work.

      greetz
        • 1. Re: HGraphicButton
          843851
          The HGraphicButton is a user interface component representing a push-button with static read-only graphical content. By default it uses the HGraphicLook class to render itself.
          So the only way to achieve the functionality you need is to create your own look that extends HGraphicLook and overwrite showLook method.
          • 2. Re: HGraphicButton
            843851
            Have you tried this one:
            setTextContent("string",HState.ALL_STATES)

            Streamer
            • 3. Re: HGraphicButton
              843851
              Yes I did,

              apparently the setTextContent method has no effect at all.
              • 4. Re: HGraphicButton
                843851
                use HTextButton that is all.
                • 5. Re: HGraphicButton
                  843851
                  But HTextButton doesn't display a background image either, and that is what I want to do.
                  • 6. Re: HGraphicButton
                    843851
                    there is no component that has both text and image.sorry. But you can make one of your own easily:
                    public class MyButtOn extends HGraphicButton {
                        private String text;
                        public MyButtOn(String text){
                           this.text=text;
                        }
                    
                        public void paint(Graphics g){
                            //displays the image...
                            super.paint(g);
                            // display the String 
                            g.drawString(text,hereX,hereY);
                        }
                    
                    
                    }
                    depending on what you want to achieve it can be slightly more complex, but simple is always the best.
                    • 7. Re: HGraphicButton
                      843851
                      This works, thanks a lot !
                      • 8. Re: HGraphicButton
                        843851
                        Hi, i tryed the code but dosen't work for me:

                        I implemented the class:

                        public class MyGraphicHaviButton extends HGraphicButton
                        {
                        private String text;
                        private int xTextPosition;
                        private int yTextPosition;

                        public MyGraphicHaviButton (String text,int xTextPosition,int yTextPosition,
                                  Image image,int xPos,int yPos,int xDim,int yDim)
                        {                  
                             super(image,xPos,yPos,xDim,yDim);
                             this.text=text;
                             this.xTextPosition=xTextPosition;
                             this.yTextPosition=yTextPosition;
                        }

                             public void paint(Graphics g){
                        //displays the image...
                        super.paint(g);
                        // display the String
                        g.drawString(text,xTextPosition,yTextPosition);
                        }
                        }

                        and used in the HContainer thet must display it:

                        Image sfondoNotSelected = Tools.loadImage("resource/notselected.png");

                        myButton=new MyGraphicHaviButton ("PROGRAMMI TV",430,274,sfondoNotSelected,410,254,210,60);
                        myButton.setFont(new Font("Tiresias",1,20));
                        myButton.setVisible(true);     
                        myButton.repaint();

                        I can see the component with the right Image in the background, but the text is not showed.
                        Why?
                        Where is the problem?

                        Thx

                        Claudio

                        Edited by: Kolok on Feb 6, 2009 2:24 AM
                        • 9. Re: HGraphicButton
                          843851
                          Earlier I have also faced this problem. I have solved this problem by extending the HGraphicLook class and overriding the showLook, widgetChanged method. Just write the following code in showLook method:

                          super.showLook(g, visible, state);+
                          g.setColor(visible.getForeground());+
                          String string = visible.getTextContent(state);+
                          *if(string != null) {*+
                          visible.getTextLayoutManager().render(string, g, visible, super.getInsets(visible));+

                          and wirte following code in widgetChanged method

                          super.widgetChanged(visible, changes);+
                          visible.repaint();+

                          now set the look of your HGraphicButton by creating the object of this class. Now you will be able to set text content also in the HGraphicButton.
                          Cheer!