Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I size my custom component appropriately?

Tags:

java

swing

So I have a custom Button, that runs good, no errors.

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class LukeButton extends JComponent implements MouseListener{
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setTitle("Luke");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        LukeButton lb = new LukeButton();
        lb.addActionListener(e->{
            System.out.println("Success");
        });
        frame.add(lb, BorderLayout.CENTER);

        frame.setVisible(true);
    }
//ArrayList of listeners
private final ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();

public LukeButton(){
    this.addMouseListener(this);
}
//Adds a listeners to the list
public void addActionListener(ActionListener e){
    listeners.add(e);
}
//Called when button is provoked
public void fireActionListeners(){
    if(!listeners.isEmpty()){
        ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "LukeButton");
        for(ActionListener l: listeners){
            l.actionPerformed(evt);
        }
    }
}
//Listens for click on my component
public void mousePressed(MouseEvent e){
    fireActionListeners();
}
public Dimension getPreferredSize(){
    return new Dimension(100, 100);
}
//Draws my button
public void paintComponent(Graphics g){

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D)g;
    Shape rec = new Rectangle2D.Float(10, 10, 60, 80);

    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(5));
    g2.draw(rec);
    g2.setColor(Color.BLUE);
    g2.fill(rec);
}
//Methods that must be over written.
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}

Here is my main problem- when ever I click ANYWHERE on the JFrame, it says the button was clicked, but the only part I want to have the action listener on is the blue rectangle I have on the JFrame. (You'll under stand my issue if you run my program) Does anyone know how to fix this? Thanks for taking your time to read :)

like image 892
Wyatt Lowery Avatar asked Aug 14 '15 23:08

Wyatt Lowery


Video Answer


1 Answers

Change your mousePressed method so that it first checks to see if the Point of clicking is enclosed by the blue rectangle. A simple if block is all that is needed. Note: do not create your Rectangle inside of paintComponent. If it is a constant, declare and create it in the class so that it can be used in the mousePressed method. Also note that Rectangle2D has a contains(...) method that will help you very much. Also note that MouseEvent has a getPoint() method that will also help you very much.

Also, to size your button, override its getPreferredSize() method as was recommended by me in your previous recent question. Also, avoid calling setSize() on your JFrame or on any component. Also, pack() your JFrame before showing it.

like image 166
Hovercraft Full Of Eels Avatar answered Oct 06 '22 00:10

Hovercraft Full Of Eels