I have a main window:
public class MainPanel extends JFrame implements MouseListener {
public MainPanel() {
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
ChildPanel child = new ChildPanel();
add(child);
JPanel spacer = new JPanel();
spacer.setPreferredSize(new Dimension(50, 50));
add(spacer);
pack();
setLocationRelativeTo(null);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click event on MainPanel");
}
}
And a child JPanel:
public class ChildPanel extends JPanel implements MouseListener {
public ChildPanel() {
setBackground(Color.RED);
setPreferredSize(new Dimension(200, 200));
//addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click event on ChildPanel");
}
}
With the call to addMouseListener commented out in the child panel, the parent receives click events when I click anywhere in the window, including on the child. If I uncomment that call and click on the child panel, only the child receives the click event and it doesn't propagate to the parent.
How do I stop the event from being consumed by the child?
In Swing, you generally want the clicked component to respond; but you can forward the mouse event to the parent, as shown below. Here's a related example.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** #see https://stackoverflow.com/questions/3605086 */
public class ParentPanel extends JPanel {
public ParentPanel() {
this.setPreferredSize(new Dimension(640, 480));
this.setBackground(Color.cyan);
this.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked in parent panel.");
}
});
JPanel child = new JPanel();
child.setPreferredSize(new Dimension(320, 240));
child.setBackground(Color.blue);
child.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked in child panel.");
ParentPanel.this.processMouseEvent(e);
}
});
this.add(child);
}
private void display() {
JFrame f = new JFrame("MouseEventTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ParentPanel().display();
}
});
}
}
I don't think you can. I believe it's a Swing design principle that only one component receives an event.
You can get the behavior you want, however, but pass the JFrame to the ChildPanel and calling its mouseClicked(MouseEvent) or whatever method you want. Or just get the parent component.
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click event on ChildPanel");
this.frame.mouseClicked(e);
getParent().mouseClicked(e);
}
Related
I have a JSplitPane with continuous layout turned on. How do I prevent other components from receiving mouse events while the divider is being dragged?
public class Test {
public static void main(String[] args) throws Exception {
JButton top = new JButton("top");
top.setRolloverEnabled(true);
top.setMinimumSize(new Dimension(100, 100));
top.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
top.setBackground(Color.green);
}
#Override
public void mouseExited(MouseEvent e) {
top.setBackground(null);
}
});
JButton bottom = new JButton("bottom");
bottom.setRolloverEnabled(true);
bottom.setMinimumSize(new Dimension(100, 100));
bottom.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
bottom.setBackground(Color.green);
}
#Override
public void mouseExited(MouseEvent e) {
bottom.setBackground(null);
}
});
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setResizeWeight(0.5);
split.setContinuousLayout(true);
split.setTopComponent(top);
split.setBottomComponent(bottom);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(split, BorderLayout.CENTER);
f.setVisible(true);
}
}
How do I prevent other components from receiving mouse events while the divider is being dragged?
Don't know of a way to turn off all events.
But I'm guessing your real concern is that you don't want the background to change.
If so, then you can add exception logic to the MouseListener to ignore the mouseEntered event when the mouse button is pressed:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
JButton top = new JButton("top");
top.setRolloverEnabled(true);
top.setMinimumSize(new Dimension(100, 100));
top.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) return;
top.setBackground(Color.green);
}
#Override
public void mouseExited(MouseEvent e) {
top.setBackground(null);
}
});
JButton bottom = new JButton("bottom");
bottom.setRolloverEnabled(true);
bottom.setMinimumSize(new Dimension(100, 100));
bottom.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) return;
bottom.setBackground(Color.green);
}
#Override
public void mouseExited(MouseEvent e) {
bottom.setBackground(null);
}
});
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setResizeWeight(0.5);
split.setContinuousLayout(true);
split.setTopComponent(top);
split.setBottomComponent(bottom);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(split, BorderLayout.CENTER);
f.setVisible(true);
}
}
Edit:
Maybe you can use your own EventQueue. The EventQueue is responsible for dispatching events to the components. So maybe you can:
Add a MouseListener to the divider
on mousePressed you replace the default EventQueue with your custom EventQueue that will ignore MouseEvents for all components other than the divider.
on mouseRelease you restore the original EventQueue.
Check out Global Event Dispatching for a basic example to get you started.
Mouse event appears not to work, and i can't find out, why.
I added a debug output at imgEdit.drawDot and there's no output at the console. I'm a newbie in java, so my code may seem to be very bad, as well as my english
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* Created by doctor on 12/29/15.
*/
public class MainUI {
Window mainWindow;
MainUI() {
mainWindow = new Window();
}
}
class Window extends JFrame {
Window() {
setBounds(0, 0, 600, 400);
setTitle("RebBrush");
Panel mainPanel = new Panel();
Container mainCont = getContentPane();
mainCont.setLayout(null);
mainCont.add(mainPanel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class Panel extends JPanel {
private ImageEdit imgEdit;
private JLabel imgLabel;
Panel() {
setLayout(null);
imgEdit = new ImageEdit(600, 400);
imgLabel = new JLabel(new ImageIcon(imgEdit.getImage()));
imgLabel.setBounds(0, 0, 600, 400);
add(imgLabel);
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
imgEdit.drawDot(e.getX(), e.getY());
}
#Override
public void mouseMoved(MouseEvent e) {
}
});
}
}
Simply getting rid of the null layouts did the trick for me. I'm not sure what ImageEdit is (some other class you've defined?), but by running the following I see "Mouse Dragged" show up in the console, so the mouseDragged method is definitely being called. Just uncomment the imageEdit stuff to put it back in.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* Created by doctor on 12/29/15.
*/
public class MainUI {
Window mainWindow;
MainUI() {
mainWindow = new Window();
}
public static void main(String[] args) {
new MainUI();
}
}
class Window extends JFrame {
Window() {
setBounds(0, 0, 600, 400);
setTitle("RebBrush");
Panel mainPanel = new Panel();
Container mainCont = getContentPane();
mainCont.add(mainPanel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class Panel extends JPanel {
//private ImageEdit imgEdit;
private JLabel imgLabel;
Panel() {
//imgEdit = new ImageEdit(600, 400);
//imgLabel = new JLabel(new ImageIcon(imgEdit.getImage()));
//imgLabel.setBounds(0, 0, 600, 400);
//add(imgLabel);
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged");
//imgEdit.drawDot(e.getX(), e.getY());
}
#Override
public void mouseMoved(MouseEvent e) {
}
});
}
}
I have a JPanel that is using a KeyListener as the content pane as the window; however, there are buttons and text fields in a grid layout on top of the JPanel.
How do I prioritize the focus of the JPanel that it retains focus after editing text or clicking the buttons so I can read the key input?
You just need to add a FocusListener on the focusLost event and then request focus back again. Something like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JPanelFocus {
public static void main(String... argv) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("FocusTest");
JButton b = new JButton("Button");
final JPanel p = new JPanel();
p.add(b);
// Here is the KeyListener installed on our JPanel
p.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ev) {
System.out.println(ev.getKeyChar());
}
});
// This is the bit that does the magic - make sure
// that our JPanel is always focussed
p.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent ev) {
p.requestFocus();
}
});
f.getContentPane().add(p);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
// Make sure JPanel starts with the focus
p.requestFocus();
}
});
}
}
This won't work if you have fields that need to keep the focus though (you mentioned an editable text field). When should key events go to the text field and when should they go to the JPanel?
As an alternative, you could also just make the child components non-focusable.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyPanel extends JPanel implements KeyListener {
public MyPanel() {
this.setFocusable(true);
this.addKeyListener(this);
// for each component
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Item1");
this.add(comboBox);
// this is what keeps each child from intercepting KeyEvents
comboBox.setFocusable(false);
}
public void keyPressed(KeyEvent e) { ... }
public void keyTyped(KeyEvent e) { ... }
public void keyReleased(KeyEvent e) { ... }
public static void main(String[] args) {
// create a frame
JFrame frame = new JFrame();
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add MyPanel to frame
MyPanel panel = new MyPanel();
frame.add(panel);
frame.setVisible(true);
}
}
I want to lose the focus on a JTextField when the user is clicking on the main panel.
setFocusable(true) doens't work for me.
If I am clicking on the main panel, the JTextField has still the focus and you can enter stuff.
Note: My JFrame is set to focusable (true).
I think since you want to get the focus on clicking the main panel, you should implement a simple MouseListener to do the job for you. Again since the panel (main panel) is added to the JFrame or rather it's set as the content pane, that is the place where setFocusable(true); should be called. The code below should sort out the problem :
mainPanel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
super.mouseReleased(e);
Focus.this.grabFocus();
}
});
Please note that Focus is the name of my class and am extending the JPanel before adding it to the JFrame.
Note: My JFrame is set to focusable (true).
for example
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JPanelAndFocus {
private JFrame frm = new JFrame("JPanel_And_Focus");
private JPanel panel = new JPanel();
private JTextField one = new JTextField(10);
private JTextField two = new JTextField(10);
private JTextField three = new JTextField(10);
public JPanelAndFocus() {
//panel.setLayout(new FlowLayout());
panel.add(one);
panel.add(two);
panel.add(three);
panel.setFocusable(true);
panel.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
//delayed should be important for events from DocumentListener / InputMask
#Override
public void run() {
//panel.requestFocus();
panel.requestFocusInWindow();
}
});
}
});
panel.setPreferredSize(new Dimension(400, 100));
frm.add(panel);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLocation(400, 300);
frm.pack();
frm.setVisible(true);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//one.requestFocus();
one.requestFocusInWindow();
}
});
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanelAndFocus jpaf = new JPanelAndFocus();
}
});
}
}
Whereas the different methods cited above didn't work for me, meaning that the focus remained on the JButton, I find e.getSource() works to set the component focus right
YourContainerPanel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
JComponent clicked = (JComponent)e.getSource();
clicked.requestFocusInWindow();
}
});
Try this
this.getParent().requestFocus()
How to resize popup component after invoke show() method?
This example is not work:
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MyFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MyFrame().setVisible(true);
}
});
}
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(400, 300);
setLocation(400, 300);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
showPopup();
}
});
}
void showPopup() {
JComponent popup = new JPanel();
popup.setPreferredSize(new Dimension(100, 40));
popup.setBorder(BorderFactory.createLineBorder(Color.GREEN));
Popup p = PopupFactory.getSharedInstance().getPopup(this, popup, this.getLocationOnScreen().x, this.getLocationOnScreen().y);
p.show();
popup.setPreferredSize(new Dimension(200, 80));
popup.setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
Size of the component popup does not change.
I found the solution, you must explicitly change size of the window of popup component.
SwingUtilities.getWindowAncestor(popup).setSize(200, 80);
But this solution works only in case if popup component located in another window. And this is not what I need.
Don't provide the preferred size. It will re size automatically.
popup.setPreferredSize(new Dimension(200, 80));