How to addMouseListener for FilePane in JFileChooser

addMouseListener for FilePane in JFileChooser

Page 1 of 1

2 Replies - 9775 Views - Last Post: 21 October 2008 - 09:12 PM Rate Topic: -----

#1 mypoortom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-October 08

How to addMouseListener for FilePane in JFileChooser

Post icon  Posted 21 October 2008 - 07:05 PM

I have searched through some posts in forum, but I couldn't find one about mouse handling for JFileChooser or FilePane :(
adding a MouseListener to FilePane of JFileChooser:
My end goal is: show my own PopupMenu in FilePane when I right Clik on a File Item in List (placed in FilePane), but I can't handle the MouseEvent, FilePane always show its default PopupMenu, bypass my mouse handler.
I'm writing an FTP Client, like CuteFTP, and use JFileChooser to browser Local File.
I really need someone's help, cause I haved tried on this for 1week, but... :((
I can show a simple example code here:
/*Class fileChooser_Frame, include main() method*/
import java.awt.BorderLayout;
import javax.swing.JFrame;
import sun.swing.FilePane;
import javax.swing.JFileChooser;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JList;
import java.awt.Point;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;

public class fileChooser_Frame extends JFrame {
	BorderLayout borderLayout1 = new BorderLayout();
	public fileChooser_Frame() {
		try {
			jbInit();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	private void jbInit() throws Exception {
		getContentPane().setLayout(borderLayout1);
		JFileChooser jF = new JFileChooser();
		FilePane filePane = (FilePane) jF.getComponent(2);
		final JPopupMenu jPop = new JPopupMenu("Testing");
		JMenuItem jMenuItem = new JMenuItem("Item 1");
		jPop.add(jMenuItem);

		MouseListener mouseListener  = new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				System.out.println("Mouse Clicked");
				checkPopup(e);
			}
			public void mousePressed(MouseEvent e) {
				System.out.println("Mouse Pressed");
				checkPopup(e);
			}
			public void mouseReleased(MouseEvent e) {
				System.out.println("Mouse Released");
				checkPopup(e);
			}
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}
			private void checkPopup(MouseEvent e){
				 if(e.isPopupTrigger())
					 jPop.show(e.getComponent(),e.getX(),e.getY());
			}
		};
		filePane.addMouseListener(mouseListener);
		getContentPane().add(filePane, BorderLayout.CENTER);
/*The result here is: FilePane show only its default PopupMenu, not my jPop :((, I need help here, pls*/
	}

	public static void main(String[] args) {
		fileChooser_Frame filechooser_frame = new fileChooser_Frame();																				 filechooser_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		filechooser_frame.pack();
		filechooser_frame.setVisible(true);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to addMouseListener for FilePane in JFileChooser

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to addMouseListener for FilePane in JFileChooser

Posted 21 October 2008 - 08:49 PM

The code as you post it does not even compile.....
once a few correction were done it works perfectly for me....
OK your variables name convention are kind of obscure but once everything is renamed to be unique and not confusing your application works like a charm and the right click prompts for the appropriate menu that displays the correct info
Was This Post Helpful? 0
  • +
  • -

#3 mypoortom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-October 08

Re: How to addMouseListener for FilePane in JFileChooser

Posted 21 October 2008 - 09:12 PM

View Postpbl, on 21 Oct, 2008 - 08:49 PM, said:

The code as you post it does not even compile..... once a few correction were done it works perfectly for me....

Oops, i don't know why, but I did compile successfully, then I copy and paste it here. So sorry that I have wasted some of your time!
I want to say: thank you very very much for your help! :-)

Quote

OK your variables name convention are kind of obscure but once everything is renamed to be unique and not confusing your application works like a charm and the right click prompts for the appropriate menu that displays the correct info

Uhm, I know that i'm just a beginner, so my style is not really nice, hihi!
But, I don't understand: "the right click prompts for the appropriate menu that displays the correct info"??? does it mean: you got no problem? Did it show my jPop, instead of the "View | Refresh | New Folder" PopupMenu?

Quote

private void checkPopup(MouseEvent e){
if(e.isPopupTrigger())
jPop.show(e.getComponent(),e.getX(),e.getY());
}

Looking forward to hear from you!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1