I am using GXT window to create a popup edit window for editing a form field. As I edit the text field, the form field also changes. But I want to capture when the user has clicked outside of the window. That is, I want to capture blur event on a window, is that possible?
Here is a sample of my code for the window:
final Window window = new Window();
window.setSize(450, 100);
window.setPlain(true);
window.setModal(true);
window.setBlinkModal(true);
window.setHeading("Edit Text");
window.setLayout(new FitLayout());
window.setIconStyle("icon-edit");
window.addWindowListener(new WindowListener() {
public void windowHide(WindowEvent wevent) {
//do something
}
});
Can anyone help? Thanks
This will work for GXT3. GXT2 seems to have the same window.addDomHandler api though so may work in that version as well.
I threw in the MouseOutHandler version too just in case you decide the user shouldn't have to click... Anyway choose your favorite!
window.addDomHandler(new BlurHandler() {
#Override public void onBlur(BlurEvent event) {
// TODO Auto-generated method stub
}
}, BlurEvent.getType());
window.addDomHandler(new MouseOutHandler() {
#Override public void onMouseOut(MouseOutEvent event) {
// TODO Auto-generated method stub
}
}, MouseOutEvent.getType());
By the way, when adding a DomHandler you don't need to sink the event.
Related
I've set a JTextField visibility to false, and when I click a button it is supposed to immediately show but it doesn't until I resize the window.
I'm building a login/'sign up' app using Swing containing tree text fields (Username, password and confirm password (which visibility is set to false until I press the signup button). But the text field does not appear until I resize the window and I can't seem to figure out what the problem is. I used the same 'code structure' as I did in another project, and didn't have any problem at all.
You can see the code below:
setTxtusername(new JTextField("Entrez username",20));
getTxtusername().setVisible(true);
setTxtpassword(new JTextField("Entrez Password",20));
getTxtpassword().setVisible(true);
setTxtconfirmer(new JTextField("Confirmer Password",20));
getTxtconfirmer().setVisible(false);
setBtnlogin(new JButton("Login"));
getBtnlogin().setVisible(true);
getBtnlogin().addActionListener(l);
getBtnlogin().setActionCommand("login");
setBtnsignup(new JButton("Sign up"));
getBtnsignup().setVisible(true);
getBtnsignup().addActionListener(l);
getBtnsignup().setActionCommand("Signup");
//Hb is an horizontal box and Vb is a vertical one
getHb2().add(btnlogin);
getHb2().add(btnsignup);
getHb2().add(warninglabel);
getHb2().setVisible(true);
getVb().add(txtusername);
getVb().add(txtpassword);
getVb().add(txtconfirmer);
getVb().add(hb2);
getVb().setVisible(true);
pnl.add(vb);
pnl.setVisible(true);
Here is the ActionListener
public class Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String action = e.getActionCommand().toString();
switch(action){
case "login":
break;
case "Signup":
ConfirmerVisible(true, txtconfirmer);
conteneur.revalidate();
creationCompte();
break;
}
}
public void creationCompte() {
if(getTxtconfirmer().getText().length() >=6 && getTxtpassword().getText() == getTxtconfirmer().getText()) {
if(admin.isSelected()) {
Utilisateur u = new Utilisateur(txtusername.getText(), getTxtpassword().getText(), 1);
//Connexionuser.create(u);
//Connexionuser.login(u)
}
}
}
}
private void ConfirmerVisible(boolean b, Component c) {
c.setVisible(b);
}
}
To refresh GUI Use:
revalidate();
repaint();
I guess once you doing "resize" it's triggered automatically by JFrame and recursively triggering refresh on child components also.
I need to be able to process a click on the tabs in a JTabbedPane. I'm not using this to change tabs, and this isn't going to trigger on tab change. What I'm attempting to do is close the tab when it is right clicked. However, I'm not sure how I can access the tab to add a click event on it. Most of the questions related to clicking on JTabbedPanes suggest using a ChangeListener, but that won't work, since the tabs aren't going to be changed on right click.
Is there any way for me to add a click event to a JTabbedPane's tab?
Is there any way for me to add a click event to a JTabbedPane's tab?
Read the section from the Swing tutorial on How to Use TabbedPanes for a working example on how to close a tab with a mouse click.
Keep a link to the tutorial handy for Swing basics.
Sorry for late answer, but I found this very usefull for me and for avoid extra clicks detected by stateChanged (with this you can detect all you want in "click tab"):
myJTabbedPane.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Panel 1 click");
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Finally, if you want to detect right click on tab you can see next tutorial (search getModifiers() in next page):
https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
I developed a Java program that open specific files when the user clicks on a Jlabel. The click event worked very well. I added a keypress event so that a user can press a key on the keyboard to open the file but it's not working.
Can anyone show me how to use a key event listener to open a file or file path in Java. Am new to Java an still learning. I need your help. Please
In order to make that to work you need to understand how is the KeyListener working.. the widget needs to have the focus gained in order to catch that event and IMHO set the focus on a JLabel makes no much sense... hence in this case you can just work with a keyListener assigned to the JFrame
so my suggestion would be to work on a keylistener on the JFrame and define the keys you need for the open the file there..
Example:
public class Test extends JFrame implements KeyListener{
.....
.....
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
I added an AWTEventListener to process grab event. So this listener just use sun.awt.SunToolkit.GRAB_EVENT_MASK
mark.
But This listener can not capture UngrabEvent. The tricky thing is, when a JComboBox popuped its menulist, it can capture this event.
I use the following code for testing.
Start the program, click on the empty area of the frame, click on the frame title. Then there should be an UngrabEvent. But the listener does not capture it.
Start the program, click on the combobox and make its menulist popuped. click on the
frame title. Then there should be an UngrabEvent. And the listener captures it.
It is very strange...Is there any relationship between UngrabEvent and JComboBox?
public class ComboboxLearn {
public static void main(String[] args) {
// TODO Auto-generated method stub
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
System.out.println(event);
}
}, sun.awt.SunToolkit.GRAB_EVENT_MASK);
JComboBox box = new JComboBox(new Object[] { "AAA", "BBB", "CCC" });
box.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuCanceled(PopupMenuEvent e) {
System.out.println(e);
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
System.out.println(e);// Set a breakpoint here
}
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
System.out.println(e);
}
});
JFrame f = new JFrame();
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(box);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(new Dimension(100, 100));
f.setVisible(true);
}
}
I sense you're experimenting; but generally, you shouldn't rely on Sun/Oracle's undocumented APIs.
I want a popup that will hide when the
mouse is pressed outside the popup but
not hide when the mouse is pressed on the popup.
Why not bring up a JDialog when you see isPopupTrigger() and hide it when you see it deactivating, as another window activates? The notion is discussed here.
Although trashgod's reasoning is understandable, it doesn't answer the question: What you are actually trying to do won't work because for the grab even to fire, you need to have a window grabbed: ((SunToolkit)Toolkit.getDefaultToolkit()).grab(someWindow);.
You could change your code as follows
class ComboboxLearn {
public static void main(String[] args) {
// TODO Auto-generated method stub
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
System.out.println(event);
}
}, sun.awt.SunToolkit.GRAB_EVENT_MASK);
JComboBox box = new JComboBox(new Object[] { "AAA", "BBB", "CCC" });
JFrame f = new JFrame();
box.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuCanceled(PopupMenuEvent e) {
System.out.println(e);
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
System.out.println(e);// Set a breakpoint here
//UNGRABBING WINDOW
((SunToolkit) toolkit).ungrab(f);
}
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
System.out.println(e);
//GRABBING WINDOW
((SunToolkit) toolkit).grab(f);
}
});
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(box);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(new Dimension(100, 100));
f.setVisible(true);
}
}
Then you will get your grab events but at the level you are working on you don't need them.
Then when would I need them?
Assuming you run into this nasty bug which although closed, I could still reproduce it, you need to implement your own popup mechanism. You did everything fine you attached your AWT listeners to close the popup whenever there is a click outside it but something is missing. You click everywhere and the popup disappears except on the window captions and outside your application! Shoot you think! How does JPopupmenu do it. And after you spend some time reading jdk code and trying various stuff, you realize that it's this undocumented event that does the trick.
I don't know the internals of this grab() method and I don't have time to investigate so there might be side effects not very obvious. Call it on your own risk.
Using standalone SWT Scrollbars is something of a hack (using this workaround), but it can be done. Here's a snippet:
ScrolledComposite scrolledComposite = new ScrolledComposite(
parent, SWT.V_SCROLL);
ScrollBar scrollbar = scrolledComposite.getVerticalBar();
Shell tip = new Shell(UserInterface.getShell(), SWT.ON_TOP
| SWT.NO_FOCUS | SWT.TOOL);
// ..stylize and fill the tooltip..
Now what I'm trying to do is monitor when the user is interacting with the scrollbar. In particular, I want to know when the user is dragging the scrollbar—and when it has been released—in order to display an Office 2007-style tooltip revealing which page the position of the scrollbar corresponds with.
Presently, I have the following code which displays the tooltip:
scrollbar.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent event) {}
public void widgetSelected(SelectionEvent event) {
tip.setVisible(true);
}
}
It would seem logical then to have the tooltip disappear when the mouse button is released:
scrollbar.addListener(SWT.MouseUp, new Listener() {
public void handleEvent(Event event) {
tip.setVisible(false);
}
});
However, neither scrollbar nor scrolledComposite seem to respond to the SWT.MouseUp event when the user interacts with the scrollbar.
I presently have a workaround that hides the tip after a timeout, but I'm not satisfied with this. Any insights would be most appreciated!
Scrollbar's javadoc said this:
When widgetSelected is called, the
event object detail field contains one
of the following values: SWT.NONE -
for the end of a drag. SWT.DRAG.
SWT.HOME. SWT.END. SWT.ARROW_DOWN.
SWT.ARROW_UP. SWT.PAGE_DOWN.
SWT.PAGE_UP. widgetDefaultSelected is
not called.
So my suggestion is get your tooltip to appear and disappear is to check for the event.detail type.
public void widgetSelected(SelectionEvent event) {
tip.setVisible(event.detail != SWT.NONE);
}
scrollBar.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
if (e.detail == SWT.NONE) {
// end of drag
System.out.println("Drag end");
}
else if (e.detail == SWT.DRAG) {
// drag
System.out.println("Currently dragging");
}
}
});
Hope this will help you... But I can see a problem with mousewheel use that throws multiple drag end events...
Paul,
try using addMouseEvent method from a Scrollable object. For example:
Scrollable scrollable = scrollbar.getParent();
scrollable.addMouseListener(new MouseListener () {
void mouseDoubleClick(MouseEvent e) { ... }
void mouseDown(MouseEvent e) { ... }
void mouseUp(MouseEvent e) { ... }
});
Actually, I don't know if this approach will work. But, it's an attempt.
Good luck!