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;
publicclass FrameDemo {
publicstaticvoid main(String[] args) {
JFrame frame = new JFrame("This is a Frame.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MouseAdapter() {
publicvoid 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.
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:
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.
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.
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.
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.
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
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
, 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!