MouseListener pada array of Button


Begini, di program saya ada array of button. Misalnya ada 10×10.
Nah yg saya bingung adalah gmn caranya untuk memblok sekian button sekaligus.

Makasi sebelumnya.

sumber : http://www.diskusiweb.com/viewthread.php?tid=29193


GAMBAR

Array of Button


/**
* ArrayOfToogleButton.java
*
* echo.khannedy@gmail.com
* https://eecchhoo.wordpress.com/
*
* Powered by Eclipse Europa
*/
package eecchhoo.demo.swing;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* @author echo
*/
@SuppressWarnings(“serial”)
public class ArrayOfToogleButton extends javax.swing.JFrame {
private JPanel jPanel1;
private boolean isDown = false;
/**
* Metode utama class ArrayOfToogleButton
*
* @param args
* Argument parameter comandline atau shell
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ArrayOfToogleButton inst = new ArrayOfToogleButton();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
/**
* Membuat class ArrayOfToogleButton baru
*/
public ArrayOfToogleButton() {
super();
initGUI();
}
/**
* Metode ini berisikan seluruh deklarasi komponen SWING
*/
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle(“Array of Button by USU”);
{
jPanel1 = new JPanel();
GridLayout jPanel1Layout = new GridLayout(5, 5);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
jPanel1Layout.setColumns(5);
jPanel1Layout.setRows(5);
jPanel1.setLayout(jPanel1Layout);
getContentPane().add(jPanel1, BorderLayout.CENTER);
{
for (int i = 1; i <= 25; i++) {
final JToggleButton button = new JToggleButton(Integer.toString(i));
jPanel1.add(button);
button.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
button.setSelected(!button.isSelected());
}
@Override
public void mouseEntered(MouseEvent e) {
if (isDown) {
button.setSelected(!button.isSelected());
}
}
@Override
public void mouseExited(MouseEvent e) {
// Do Nothing
}
@Override
public void mousePressed(MouseEvent e) {
isDown = true;
button.setSelected(!button.isSelected());
}
@Override
public void mouseReleased(MouseEvent e) {
isDown = false;
}
});
}
}
}
pack();
this.setSize(400, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Iklan

2 pemikiran pada “MouseListener pada array of Button

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s