• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing - detect mouse click outside modal JDialog boundary

 
james johnson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JFrame with objects located on it at specific positions.

I'd like to pop up a modal JDialog on top of that JFrame and let the user click on the object pixels on the underlying JFrame and detect those mouse clicks from within the JDialog so that I can show some meta information from within the JDialog.

I know that the user cannot click on the actual JFrame objects once the JDialog is showing, but if they move the JDialog to the side, they can physically click the mouse on the pixel locations of the JFrame objects, and it's those clicks I'd like to detect from within the JDialog.

When I look at the standard MouseEvent's they all seem to be constrained to the JDialog boundaries (which is fair enough for normal use), and I know that what I'd like to do here is non-standard, just wondering if anyone has any ideas on how I might approach it.

Cheers
James
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

As a user I would not expect that clicking on the main window brings up help about the dialog in front of it. Why not make the help functionality obvious by adding a small icon or label labelled "Help" to the dialog?
 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess i know what you want to do.
But the dialog is modal so you wont be able to capture the clicks.
I was wondering if you can do it by making another frame instead of the dialog
& turning it always-on-top(only in Tiger).

A very raw example :

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


class H
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JFrame iframe = new JFrame();
final JLabel lbl = new JLabel("Hello");
iframe.getContentPane().add(lbl);
String[] petStrings = { "Bird", "Cat", "Dog", "Rab@bit", "Pig" };
try{
iframe.setAlwaysOnTop(true); // only in 1.5
}catch(Exception e){e.printStackTrace();}

//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(new DefaultComboBoxModel(petStrings));
petList.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
lbl.setText(e.paramString());
}
});
petList.setSelectedIndex(3);
frame.getContentPane().add(petList);
frame.pack();
frame.setVisible(true);
iframe.pack();
iframe.setVisible(true);
}
}
[ January 08, 2007: Message edited by: Vishal Mungi ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!