participate


Swing - JFrame - MouseEvent handling
<<   Back to Forum  |   Give us Feedback
This topic has 7 replies on 1 page.
neblaz
Posts:10
Registered: 10/4/09
JFrame - MouseEvent handling   
Oct 29, 2009 10:49 AM

 
Hi,

the following code shows a simple frame, a MouseListener reacts on a mouseclick:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.JFrame;
 
public class FrameDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("This is a Frame.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	System.out.println("MouseClicked.");
            }
        });
 
        int width = 300;
        int height = 300;
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}


My questions:
1. Nothing happens when I click on the title of the frame. How to achieve this? The MouseListener seems only to work within the borders.
2. How to achieve, that if clicked outside the frame, a message like "MouseClicked outside the frame." appears?
3. How to achieve, that if clicked on the support button for closing the frame a message like "MouseClicked, frame closed." appears? By means something shall be done before frame closes.

Thx!
 
BoBear2681
Posts:975
Registered: 1/17/08
Re: JFrame - MouseEvent handling   
Oct 29, 2009 11:17 AM (reply 1 of 7)  (In reply to original post )

 
neblaz wrote:
1. Nothing happens when I click on the title of the frame. How to achieve this? The MouseListener seems only to work within the borders.

The mouse events are going to the frame's "content pane," that is, the panel inside the frame that holds any child components. Add your mouse listener to it instead:

frame.getContentPane().addMouseListener(myListener);


2. How to achieve, that if clicked outside the frame, a message like "MouseClicked outside the frame." appears?

This isn't possible in Java. You can only detect mouse events that occur inside your own application.

3. How to achieve, that if clicked on the support button for closing the frame a message like "MouseClicked, frame closed." appears? By means something shall be done before frame closes.

Add a WindowListener and listen for the windowClosingEvent.

frame.addWindowListener(new MyWindowListener());
 
camickr
Posts:32,733
Registered: 2/27/98
Re: JFrame - MouseEvent handling   
Oct 29, 2009 11:42 AM (reply 2 of 7)  (In reply to original post )

 
1. Nothing happens when I click on the title of the frame. How to achieve this? The MouseListener seems only to work within the borders.

Correct, the frame is actually an OS component and as such Jave does not have access to the title bar and borders. The functionality is handled by the OS.
 
neblaz
Posts:10
Registered: 10/4/09
Re: JFrame - MouseEvent handling   
Oct 29, 2009 11:43 AM (reply 3 of 7)  (In reply to #1 )

 
For No 1 I changed the code to:

        frame.getContentPane().addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent evt) {
        		System.out.println("MouseClicked.");
        	}
        });


But no different behaviour.

Thx!
 
neblaz
Posts:10
Registered: 10/4/09
Re: JFrame - MouseEvent handling   
Oct 30, 2009 9:34 AM (reply 4 of 7)  (In reply to #3 )

 
I'll make a new topic with a more detailed code, so perhaps then some of those points can be achieved.

Thx!
 
neblaz
Posts:10
Registered: 10/4/09
Re: JFrame - MouseEvent handling   
Nov 4, 2009 12:31 AM (reply 5 of 7)  (In reply to #1 )

 
@BoBear2681:

frame.getContentPane().addMouseListener(myListener);


If the JFrame has different components than JLabels, for example some JButtons, JTextFields, etc. this code doesn't help much. If I click outside the JTabel on a component like a JButton, the MousListener above is not reached...

So my problem is: I have a JFrame "full" of components (JLabels, JButtons, JTextFields, etc.) and have there also a JTable. When I edit a cell and don't hit enter or click on another cell, but rather somewhere outside the JTable (might be click on the JLabel, or a JButton, etc.), I would like the current value to be stored.

I can't modify all those existing components inside the JFrame, and add a MouseListener to them.

I added a FocusListener to the JTabel:
		table.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				System.out.println("JTable, focusGained.");
			}
 
			public void focusLost(FocusEvent e) {
				System.out.println("JTable, focusLost.");
			}
		});


But that works not as expected. Did I use it correctly? The FocusListener is somehow in the docs associated with keyboard, so maybe it doesn't work for mouse clicks as expected?

The interesting thing is: When the JFrame is opened, and I click on the JTable, the FocusGained event occures. But when I then doubleclick onto a cell, the FocusLost event occures, although I'm inside that cell and able to edit it.

So my main question is: Can I solve that problem? It looks simple...I have a JFrame with lots of components, one of it is a JTable. When editing a cell and not saving the currently changed cell value by hitting enter or clicking on another cell, but rather clicking on any other component outside the JTable but inside the JFrame, the currently changed cell value should be saved.

Thx!
 
kopik
Posts:110
Registered: 11/15/08
Re: JFrame - MouseEvent handling   
Nov 4, 2009 1:18 AM (reply 6 of 7)  (In reply to #5 )

 
hii,

1st. way is (jtextfield or any textInputsObject, inc. for selectedItems from Combo) that you create focus listener and add this methods links to real needed components

this example selected all valid value in components

//
    FocusListener fcsListener = new FocusListener() {
 
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }
 
        public void focusLost(FocusEvent e) {
            //dumpInfo(e);
        }
 
        private void dumpInfo(FocusEvent e) {
            //System.out.println("Source  : " + name(e.getComponent()));
            //System.out.println("Opposite : " + name(e.getOppositeComponent()));
            //System.out.println("Temporary: " + e.isTemporary());
            Component c = e.getComponent();
            if (c instanceof JFormattedTextField) {
                ((JFormattedTextField) c).selectAll();
            } else if (c instanceof JTextField) {
                ((JTextField) c).selectAll();
            }
        }
 
        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };
 
//link:
 
someYourTextField.addFocusListener(fcsListener);
someYourCombo.getEditor().getEditorComponent().addFocusListener(fcsListener);
 


or:
2nd. way is:

         someYourTextField.addFocusListener(new FocusListener() {
 
            public void focusGained(FocusEvent e) {
                someYourTextField.requestFocus();
                someYourTextField.setText(someYourTextField.getText());
                someYourTextField.selectAll();
            }
 
            public void focusLost(FocusEvent e) {
            }
        });


1/ for Combo you can add .addItemListener(someListener);, too
2/ please if you don't know what's happened, then never mixing FocusListener and ItemListener fo same components (combo)
3/ for jtable you can use as follows

//global def.
private JTable resultCityTable = new JTable(); 
private ListSelectionModel lsCity;
private String miroCity = "";
private int rowCity = 0;
 
//add def to jtable
lsCity = resultCityTable.getSelectionModel();
lsCity.addListSelectionListener(new TablesListener());
 
//create this class
 
class TablesListener implements ListSelectionListener {
 
        public void valueChanged(ListSelectionEvent e) {
            miroCity = "";
            rowCity = 0;
            if (e.getValueIsAdjusting() == false) {
                rowCity = resultCityTable.getSelectedRow();
                if (rowCity == -1) {
                    miroCity = "";
                } else {
                    if ((resultCityTable.getValueAt(rowCity, 0)) == null) {
                        miroCity = "";
                    } else {
                        miroCity = resultCityTable.getValueAt(rowCity, 0).toString();
                        // .... 
                        if ((resultCityTable.getValueAt(rowCity, 3)).equals(1)) {
                           // ...
                        } else {
                            //...
                        }
                    }
                    if (!(miroCity.matches(AmendedCities))) {
                        // ....
                    }
                }
            } else {
                //ReSetDefaultData();
            }
        }
    }
} 


Note: code runs on each click to the object (listeners)

... kopik
 
camickr
Posts:32,733
Registered: 2/27/98
Re: JFrame - MouseEvent handling   
Nov 4, 2009 8:07 AM (reply 7 of 7)  (In reply to #5 )

 
, but rather clicking on any other component outside the JTable but inside the JFrame, the currently changed cell value should be saved

Then why didn't you ask that as your question? We are not mind readers, we don't know what the real requirement is unless you tell us. You would have had your answer a week ago!

Table Stop Editing
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums