Java Thread

mihir

VIP Member
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.
 

neilofbodom

Member
Hi

I'm writing a program for a school project using JAVA on JCreator. it is a music trivia quiz and i'm currently working on the main menu. I imported these packages:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

The problem i encountered was when using the mouseClicked() method from the class MouseListener. I want the program to close when the user clicks on exit but it is not doing it. This is my code:

public void mouseClicked(MouseEvent e){
if (e.equals(exit)){
MainMenuWindow m = new MainMenuWindow();
m.setVisible(false);
}
}
Is there something wrong with the code or do i have to use some other commands?
Thanks

-neilofbodom
 

Troncoso

VIP Member
ahhh, okay. Well if that's the case, shouldn't you just do

Code:
if (e.equals(exit)){
     System.exit(0);
}
 

NyxCharon

Active Member
What is the exit? Are you referring to the top right corner exit button, or did you make your own?

If it's the former, then you simply add
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
to the class in which the JFrame is created.
 

neilofbodom

Member
the exit button is a button i created myself. the program closes when the user clicks on the close button at the top right corner of the window. but i want the program to close when the user clicks on the exit button i created.
 

NyxCharon

Active Member
Don't know what I was thinking earlier. You don't use a mouse listener man, you use a action listener. I wrote a quick app to show you how it works. Should compile fine for you.

Code:
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(); 
    }
}
 

neilofbodom

Member
:eek: 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?
 

TrainTrackHack

VIP Member
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?
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.
 

NyxCharon

Active Member
:eek: 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?

Hackapelite summed it up pretty well, but to expand, you could in theory still use one. However, The only way i could think that would work, is if on a mouse pressed/clicked event, you grabbed the coordinates, and then compared that to the area the button is in. Simply put, making a action listener simplifies all of this, so stick with that. :p
 

zombine210

New Member
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.

how's it going with this?
i just finished hello android using eclipse & AVD!
it's pretty cool. i don't know much java, and don't have any ideas for programs, but i'm in here; feels like i'm swimming with teh sharks.
 

Troncoso

VIP Member
So, for the first time I'm playing with GUI's in Java. I went the easy route and used WindowBuilder Pro in eclipse, rather than hard coding the entire interface. This is what I have:

bk.png


Now, I have the JTextPane inside a JScrollingPane (on the right). What I would like is for the Scrolling Pane to auto-resize when the JFrame is re-sized. I've tried adding it to the panel's BorderLayout.Center, but that doesn't do anything. I try doing an event handler that checks that the Frame has been re-sized, but I run into an this error:

Code:
Cannot refer to a non-final variable editor_scrPane inside an inner class defined in a different method

Here is the code I try:

Code:
JScrollPane editor_scrPane = new JScrollPane();
		editor_scrPane.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
			@Override
			public void ancestorResized(HierarchyEvent e) {
				editor_scrPane.setSize(new Dimension());
			}
		});

I understand that code won't re-size properly, but the main focus is the fact that I can't reference the Scrolling Pane at all inside the event handler.
 
Top