/** @return file chooser in dialog */ public Component getCustomEditor() { final JFileChooser chooser = createFileChooser(); final javax.swing.JDialog dialog = new javax.swing.JDialog( TopManager.getDefault().getWindowManager().getMainWindow(), chooser.getDialogTitle(), true); // attach cancel also to Escape key dialog .getRootPane() .registerKeyboardAction( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dialog.setVisible(false); dialog.dispose(); } }, javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, true), javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW); dialog.addKeyListener( new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE) { dialog.setVisible(false); dialog.dispose(); } } }); dialog.getContentPane().setLayout(new java.awt.BorderLayout()); dialog.getContentPane().add(chooser, java.awt.BorderLayout.CENTER); chooser.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) { File f = chooser.getSelectedFile(); setValue(f); dialog.setVisible(false); dialog.dispose(); } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) { dialog.setVisible(false); dialog.dispose(); } } }); HelpCtx.setHelpIDString(dialog.getRootPane(), getHelpCtx().getHelpID()); return dialog; }
public void btnChoose_actionPerformed() { // java.awt.Rectangle r = dateField.getBounds(); // Point pOnScreen = dateField.getLocationOnScreen(); java.awt.Rectangle r = Jtext.getBounds(); Point pOnScreen = Jtext.getLocationOnScreen(); Point result = new Point(pOnScreen.x, pOnScreen.y + r.height); Point powner = owner.getLocation(); int offsetX = (pOnScreen.x + width) - (powner.x + owner.getWidth()); int offsetY = (pOnScreen.y + r.height + height) - (powner.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= height + r.height; } javax.swing.JDialog dateFrame = new javax.swing.JDialog(); dateFrame.setModal(false); dateFrame.setUndecorated(true); dateFrame.setLocation(result); dateFrame.setSize(width, height); dateFrame.addWindowListener( new WindowAdapter() { // 鍦ㄤ换鎰忕殑闈炴棩鏈熼�夋嫨鍖哄崟鍑伙紝鍒欐棩鏈熼�夋嫨缁勪欢灏嗗彉涓洪潪娲诲姩鐘舵�侊紝鑷姩閲婃斁璧勬簮銆� public void windowDeactivated(WindowEvent e) { javax.swing.JDialog f = (javax.swing.JDialog) e.getSource(); f.dispose(); } }); DatePanel datePanel = new DatePanel(dateFrame, parten); dateFrame.getContentPane().setLayout(new BorderLayout()); dateFrame.getContentPane().add(datePanel); dateFrame.setVisible(true); }