mkaouer / j4me

Automatically exported from code.google.com/p/j4me

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create a button component

GoogleCodeExporter opened this issue · comments

This has been requested by a few people.  The following code was submitted
on the discussion boards:

public class Button extends Label {
       private Runnable _action;  // the action on button press

       public Button(String label) {
               super(label);
       }

        public boolean acceptsInput() {
               return _action != null;
       }



       public void keyPressed(int keyCode) {
               if (keyCode == DeviceScreen.FIRE && _action != null)
                       _action.run();
       }

       public void pointerPressed(int x, int y) {
               if (action != null)
                       _action.run();
       }

       void setAction(Runnable action) {
               _action = action;
       }
       protected void paintComponent ( Graphics g , Theme theme , int
width ,

                                                            int height ,
boolean selected )
       {
               int offset = paintRect( g, theme, getX( ), 0, width, height + 2,
selected );
               offset += 1;

               int left = offset;
               int top = offset;

               height -= offset;
               width -= 2 * offset;

               int primaryColor = Theme.WHITE;
               int secondaryColor = theme.getHighlightColor( );

               // how far down the fill will peak
               //
               double maxSecondary = 1;

               Theme.gradientFill( g, left, top, width, height, true,
primaryColor,
secondaryColor, maxSecondary );
               super.paintComponent( g, theme, width, height, selected );
       }
}

Original issue reported on code.google.com by deanbro...@gmail.com on 29 Feb 2008 at 4:53

The gradientFill doesn't completely fill the rectangle.

Original comment by shi...@gmail.com on 25 Jan 2010 at 3:16

Does anyone tried using this Button method? It seems not to be working...

Original comment by mokonejm@gmail.com on 17 Dec 2010 at 7:38

Try this button class in attachment.

Original comment by java4...@gmail.com on 14 Jan 2011 at 10:45

Attachments:

And the usage is simple - something like on link component.

        Button btn = new Button(L10n.localize("btn","Po\u0161alji Autoru SMS")) {

            public void onClik() {
//                setTitle("Press me");
                SMSThread sms_thread = new SMSThread();
                sms_thread.start();
            }
        };
        btn.setHorizontalAlignment(Graphics.HCENTER);
        append(btn);

Original comment by java4...@gmail.com on 14 Jan 2011 at 10:50