0

I'm trying to actively render and found using JButtons was not very good so I set out to make my own class. I don't have much experience with ActionListeners and am trying to use a demo I found online. I'm making my own version of the second and third class examples posted by Ko Wey https://coderanch.com/t/342333/java/Custom-Buttons .

Everything renders fine but nothing happens when clicked

package com.whycom.engine;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JPanel;

public class Button extends JPanel implements MouseListener
{
    private static final long serialVersionUID = 1L;

    private int width;
    private int height;
    private int xPos;
    private int yPos;
    private String text;
    private Color textColor;
    private Color bgColor;
    private Color borderColor;
    private Font font;
    private Color hoverColor;
    private boolean hover;

    private Vector< ActionListener > listeners;


    public Button( int xPos, int yPos, int width, int height, String text,
            Font font, Color textColor, Color bgColor, Color borderColor,
            Color hoverColor )
    {
        super();

        this.width = width;
        this.height = height;
        this.text = text;
        this.textColor = textColor;
        this.bgColor = bgColor;
        this.borderColor = borderColor;
        this.xPos = xPos;
        this.yPos = yPos;
        this.font = font;
        this.hoverColor = hoverColor;

        listeners = new Vector< ActionListener >();
        addMouseListener( this );

    }


    public void render( Graphics g )
    {
       //omitted because it shouldn't be the problem
    }


    @Override
    public void mouseClicked( MouseEvent arg0 )
    {
        fireEvent( new ActionEvent( this, 0, text ) );
    }


    @Override
    public void mouseEntered( MouseEvent arg0 )
    {}


    @Override
    public void mouseExited( MouseEvent arg0 )
    {}


    @Override
    public void mousePressed( MouseEvent arg0 )
    {}


    @Override
    public void mouseReleased( MouseEvent arg0 )
    {}


    private void fireEvent( ActionEvent event )
    {
        for( int i = 0; i < listeners.size(); i++ )
        {
            ActionListener listener = ( ActionListener ) listeners
                    .elementAt( i );
            listener.actionPerformed( event );
        }
    }


    public void addActionListener( ActionListener listener )
    {
        listeners.addElement( listener );
    }


    public void removeActionListener( ActionListener listener )
    {
        listeners.removeElement( listener );
    }
}

When creating my state class I do this

    menu1 = new Button( 0, 0, 400, 100, "Test", buttonFont, Color.white,
            Color.black, Color.white, Color.blue );

    menu1.addActionListener( new ActionListener()
    {
        public void actionPerformed( ActionEvent e )
        {
            System.out.println( "I am clicked!" );
        }
    } );

I've had the getPreferredSize method in Button, as well as putting this.add(menu1) in my state constructor since state extends JPanel. Neither did anything for it.

I've managed to strip my project down to 4 classes that replicate the problem https://github.com/CloudyNinja/StackOverflowButtonQ

Spencer
  • 1
  • 1
  • have you added the menu to the panel it should be placed on ? – Alexander Petrov Sep 14 '18 at 19:48
  • You want to create and post a small running program that demonstrates your attempt. – Hovercraft Full Of Eels Sep 14 '18 at 19:52
  • Your code works for me. – Hovercraft Full Of Eels Sep 14 '18 at 20:00
  • Some problems though. 1) It should be `fireEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text));` since this should be the id for an ActionListener. 2) Your Button class's `public Dimension getPreferredSize()` should be overridden and return the width and height (as a dimension). – Hovercraft Full Of Eels Sep 14 '18 at 20:02
  • It would be better if you extends from JButton instead and you have to implement ActionListener so you can listen for click events, button.addActionListener(new ActionListener() .... etc. – Bashar Abutarieh Sep 14 '18 at 20:19
  • @HovercraftFullOfEels I made a running program, sorry its 4 classes. It also has the two additions you recommended, but it still isn't working for me. – Spencer Sep 14 '18 at 21:30
  • Spence, thanks for the update, but the way this site works, all code must be in your question itself as code-formatted text and not in a link. We don't want the whole project, just a small program that reproduces the problem. Other than the issues that I've noted above (and the problem with a wrong preferred size is a serious one), if these don't fix the problem, then the error lies in code that you're not showing. – Hovercraft Full Of Eels Sep 14 '18 at 21:33
  • I would consider using a `ButtonUI` delegate - something [like this for example](https://stackoverflow.com/questions/47339013/why-paintcomponent-is-defined-on-jcomponent/47341727#47341727) - it means you don't need to re-implement much of the core functionality of the button – MadProgrammer Sep 14 '18 at 21:53
  • *"I'm trying to actively render"* - What does this mean? Does this mean you're using a `BufferStrategy`? – MadProgrammer Sep 14 '18 at 21:57
  • WHOAH! Okay, stop right there, you are using a `BufferStrategy`, this means you can no longer use Swing based components, including `JPanel` - your whole solution needs to be re-thought, this includes mouse and key management as well as rendering – MadProgrammer Sep 14 '18 at 22:00
  • I'd also like to point out that most of the functionality of your linked example is pointless, as the `JButton` actually support roll over and pressed state changes, you just need to configure it – MadProgrammer Sep 14 '18 at 22:03
  • @MadProgrammer Thank you for the input. It seems I need to do some more studying before taking on a personal project like this – Spencer Sep 14 '18 at 22:47

0 Answers0