import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author NyxCharon
*/
public class CloseButtonTest extends JFrame
{
public CloseButtonTest()
{
JPanel pan=new JPanel();
JButton exit=new JButton("Click to close");
exit.addActionListener(new ExitListener());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Close Test");
setSize(60, 80);
pan.add(exit);
add(pan);
setVisible(true);
}
//Action Listener, Fires when the button it is attached to is pressed.
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
CloseButtonTest run=new CloseButtonTest();
}
}
Mouse listeners are for tracking mouse actions such as clicking mouse buttons and wheel scrolling. However, clicking a button on you application is an "action" and you're supposed to use those listeners instead - that way using keyboard (using tab to select different buttons, space/enter to click) and different handedness is handled transparently without you doing anything about it. You could in theory use mouse listeners (can't tell why it didn't work in your particular case), but it's a lot harder and usually the result is glitchy on some systems. You generally use a mouse listener only if you want to register actions that the component doesn't support (such as clicking a panel or other non-button object) or you want to find out things like which exact button was pressed.that's awesome man! thank you very much! one question tho, why can't you use the mouse listener? isn't it meant to do that?
that's awesome man! thank you very much! one question tho, why can't you use the mouse listener? isn't it meant to do that?
As a matter of fact I did not. I waited for five minutes and I got a reply from another source, as soon as I posted this. Got the book since I wanted cover some ground this night. It is exactly what I wanted. I also got some ebooks, for more examples. It is exactly what I wanted. It covers licensing and everything. Pretty good.Right now I am downloading Eclipse and Android SDK.
Cannot refer to a non-final variable editor_scrPane inside an inner class defined in a different method
JScrollPane editor_scrPane = new JScrollPane();
editor_scrPane.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
@Override
public void ancestorResized(HierarchyEvent e) {
editor_scrPane.setSize(new Dimension());
}
});
Try using 'this' instead of editor_scrPane.