HI Button problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: HI Button problem

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    HI Button problem

    Hi all,

    Basically my problem is that I've been able to create the buttons, but am unable to get any of the text to show up on the buttons, nor for the event listener to work. Any help would be greatly appreciated.

    Thanks

    Nate

    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class Test1
    {
    private static final int FRAME_WIDTH = 400 ;
    private static final int FRAME_HEIGHT = 400 ;

    public static void main(String[] args)
    {
    JFrame frame = new JFrame() ;
    LifeButton[][] myButton = new LifeButton[10][10] ;

    JPanel panel = new JPanel() ;
    panel.setLayout( new GridLayout( 10, 10 ) ) ;

    for( int i = 0 ; i < myButton.length ; i++ )
    {
    for( int k = 0 ; k < myButton.length ; k++ )
    {
    myButton[i][k] = new LifeButton( "O", false, i, k ) ;
    panel.add( myButton[i][k] ) ;
    }
    }


    frame.add( panel ) ;

    frame.setSize( FRAME_WIDTH, FRAME_HEIGHT ) ;
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
    frame.setVisible( true ) ;
    }
    }


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;


    public class LifeButton extends JButton
    {
    private String text ;
    private boolean value ;
    private int x ;
    private int y ;

    public LifeButton( String text, boolean value, int x, int y )
    {
    System.out.println( "Initializing button " + x + " " + y ) ;
    this.text = text ;
    this.value = value ;
    this.x = x ;
    this.y = y ;
    button() ;
    }

    public void button()
    {
    System.out.println( "in button " + x + " " + y ) ;

    final JButton myButton = new JButton( text ) ;

    class ClickListener implements ActionListener
    {
    public void actionPerformed( ActionEvent event )
    {
    System.out.println( "in button action " + x + " " + y ) ;

    if( value )
    {
    value = false ;
    text = "0" ;
    }
    else
    {
    value = true ;
    text = "X" ;
    }

    myButton.setText( text ) ;
    }
    }

    ActionListener listen = new ClickListener() ;
    myButton.addActionListener( listen ) ;
    }
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    4

    Re: HI Button problem

    I would try changing

    ActionListener listen = new ClickListener (); to

    ClickListener listen = new ClickListener ();

    OR

    If you are interested in mouse clicks you can play around with the mouse classes


    mybutton.addMouseListener(new MouseListener() {
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
    if ( value ) {
    value = false; // etc
    }

    }
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    });

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: HI Button problem

    Quote Originally Posted by maureencioe View Post
    I would try changing

    ActionListener listen = new ClickListener (); to

    ClickListener listen = new ClickListener ();
    Interesting - would you care to explain why?

    Indirection is the right direction...
    A. Glew
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: HI Button problem

    The problem with the button text is that you're not displaying the buttons that have the text. You have defined a LifeButton subclass of JButton, and you display these on a panel, but for some reason, in the button() method (not a very descriptive name), you create a new JButton and put the text and action listener into that. At the end of the button() method, this new button goes out of scope and takes the text and the action listener with it to oblivion.

    I suggest you simply remove all references to 'myButton' in the button() method.

    At the source of every error which is blamed on the computer, you will find at least two human errors, one of which is the error of blaming it on the computer...
    T. Gilb
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center