1

I can't figure this out for the life of me. What I want to happen is when my mouse hovers over the "Start" JButton in the menu to enlarge it by like 50-100px. What is a way to do that, that would work with my code here? thanks in advance! "I'm using java eclipse btw" don't know if that helps or not

//START_Button
    JButton button = new JButton("Start");
    frame.pack();
    JTextPane TEXT = new JTextPane();
    BufferedImage buttonIcon;
    try {
        buttonIcon = ImageIO.read(new File("C:\\Users\\GOULDEN\\Desktop\\MENU_START.png"));
        button = new JButton(new ImageIcon(buttonIcon));
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setContentAreaFilled(false);
        Container contentPane = frame.getContentPane();
        contentPane.add(button);
        button.setBounds(500,250,300,75);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
BScody
  • 11
  • 2

1 Answers1

0

Implement MouseListener and use the mouseEntered() and mouseExited() to make your button bigger.

Declare the variables that will be accessed in other methods as instance variables to be able to access them.

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Demo extends JFrame implements MouseListener {

    private static final long serialVersionUID = 1L;    
    private JButton startButton;


    public Demo() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 500, 500);
        this.setLayout(null);        

        startButton = new JButton("Start");                                   
        startButton= new JButton(new ImageIcon("path/to/image.jpg"));
        startButton.setBorder(BorderFactory.createEmptyBorder());
        startButton.setContentAreaFilled(false);
        startButton.setBounds(1, 2, 100, 25);
        startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        startButton.addMouseListener(this);

        this.add(startButton);
        this.setVisible(true);
    }

    public static void main(String[] args) {
         new Demo();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub


    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        startButton.setSize(400, 125);
        this.repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        startButton.setSize(100, 25);
        this.repaint();
    }              
    }

btw, there is really no need to use the bufferedImage if you aren't going to use your image properties, so just insert the image location directly into ImageIcon so you can remove the try-catch block

new JButton(new ImageIcon("path/to/image.jpg"));

Check out these links to read more about the topics used in your example.

  1. Variable Scopes
  2. MouseListener implementation
  3. Working With Images
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • I added the MouseEntered and put the button.setSize(400,125); but it says button cannot be resolved, which I think is because it can't access the JButton "button" but I don't know how to let the Public void mouseEntered have access to it. sorry if it's a simple fix I'm pretty new to code – BScody Jul 09 '15 at 18:25