JDialog and JOptionPane
Hi, I got a trouble... I am using a JDialog inside a JOptionPane, with JTextField, the JDialog can be visible or invisible many times, the problem is that I cannot set the focus, despite of I used "requestFocus()" over the textfield, when I did this, it works only once, when I set invisible the JDialog, and then I set visible, the requestFocus(), dosn't work, can any body help me please...
Wilfredo
# 1
> I am using a JDialog inside a JOptionPane
Your question makes no sense since you can't add a JDialog to a JOptionPane.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
# 2
Maybe I didn't explain myself as I wanted or maybe yo don't undertand. This is my code:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class JOptionPaneTest extends JOptionPane {
private JDialog dialog;
private Toolkit toolkit;
private Dimension dimension;
private int width;
private int height;
private JFrame parent;
private JTextField text;
private JButton ok;
public JOptionPaneTest(){
init();
}
public JOptionPaneTest(JFrame parent) {
this.parent = parent;
init();
}
public void init(){
createComponents();
setProperties();
addComponents();
}
public void createComponents(){
dialog = new JDialog( parent,true);
text = new JTextField("Wilfredo");
ok = new JButton("ok");
toolkit = Toolkit.getDefaultToolkit();
dimension = toolkit.getScreenSize();
width = dimension.width;
height = dimension.height;
}
public void setProperties(){
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(1);
dialog.setBounds(50, 50, 400, 300);
dialog.setLayout(null);
text.setBounds(20,50,100,20);
ok.setBounds(20, 80, 80, 20);
ok.addMouseListener( new MouseAdapter(){
public void mouseClicked( MouseEvent e ){
close();
}
});
}
private void addComponents() {
dialog.add(text);
dialog.add(ok);
}
public void setVisibleDialog( boolean b ){
dialog.setVisible(b);
text.requestFocus();
}
public void close(){
setVisibleDialog(false);
}
public static void main(String[] args) {
JFrame window = new JFrame();
window.setBounds(100,200,400,300);
window.setTitle("TEST");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("show");
window.add( button );
final JOptionPaneTest test = new JOptionPaneTest(window);
button.addMouseListener(new MouseAdapter(){
public void mouseClicked( MouseEvent e){
test.setVisibleDialog(true);
}
});
window.setVisible(true);
}
}
please anybody help me please...
# 3
try use grabFocus() instead of requestFocus().Message was edited by: Nep23
# 4
First of all you should not be extending JOptionPane. If you want to create a custom dialog then extend JDialog. Then get rid of the dialog variable and simply invoke the dialog functions directly.
Secondly a modal dialog means that code placed after the setVisible(true) method does not execute until the dialog is closed. The easiest way to place focus on a component is to order it like this:
pack();
someComponent.requestFocusInWindow();
setVisible( true );
Of course if you use the pack() method you should also be using LayoutManagers so the pack() method can determine the appropriate size of the dialog. You should only use a null layout if you panel is used to drag and drop components on the panel.
# 5
Ok, the reason I am using dialog here with a JoptionPane is that I want benefits of using joptionpane and jdialog, I mean, I do not want my program excecuting before I close the dialog, and I don't want to have another window on the toolbar, this happens when I use a JDialog, and using JOptionPane, this is as I want. I tried your suggestions, but those ones doesn't work as I want... please another suggestions ...
# 6
I'm sorry, but your description of the problem doesn't make any sense.
You are not using a JOptionPane. You are using a JDialog and the dialog is modal which is why your requestFocus doesn't work, as I explained earlier.
Using a JDialog does not add an icon on the task bar. Only using a JFrame will add the icon to the task bar.
> I tried your suggestions, but those ones doesn't work as I want...
Then you coded it incorrectly.
# 7
> joptionpane and jdialog, I mean, I do not want my
> program excecuting before I close the dialog, and I
check out my StandardDialog class. In particular, check out the static showStandardDialog method, which won't return until the dialog is closed.
To get this to compile you should change the extends from ModalOnParentOnlyDialog to just JDialog
You are welcome to use and modify this class, but please don't change the package or take credit for it as your own code
StandardDialog.java
================
package tjacobs.ui;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JDialog;
/**
* Standard Dialog is a basic dialog with Apply/Ok/Cancel buttons
* To implement it, you have to implement apply() which gets run
* when the user hits either apply or ok, and showApplyButton(),
* which says whether or not to show the apply button
*/
//public abstract class StandardDialog extends JDialog {
public abstract class StandardDialog extends ModalOnParentDialog {
public StandardDialog() throws HeadlessException {
super();
_init();
}
public StandardDialog(Dialog owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Dialog owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Frame owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Frame owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Dialog owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Frame owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal,
GraphicsConfiguration gc) throws HeadlessException {
super(owner, title, modal, gc);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal,
GraphicsConfiguration gc) {
super(owner, title, modal, gc);
_init();
}
/*
public void setVisible(boolean b) {
super.setVisible(b);
if (b) StandardFrame.Watch(this); else StandardFrame.RemoveWatch(this);
}
*/
public void setMainContent(Component c) {
mContent.add(c, BorderLayout.CENTER);
mMainPane = c;
}
protected void _init() {
mContent = getContentPane();
mContent.setLayout(new BorderLayout());
mButtonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
mContent.add(mButtonPane, BorderLayout.SOUTH);
mButtonListener = new ButtonListener();
if (showApplyButton()) {
mApply = new JButton("Apply");
mButtonPane.add(mApply);
mApply.addActionListener(mButtonListener);
}
mOk = new JButton("OK");
mButtonPane.add(mOk);
mOk.addActionListener(mButtonListener);
mCancel = new JButton("Cancel");
mButtonPane.add(mCancel);
mCancel.addActionListener(mButtonListener);
}
public Component getMainPane() {
return mMainPane;
}
public void ok (){
mCancelState = OK;
apply();
close();
}
public void cancel() {
mCancelState = CANCEL;
close();
}
public void close () {
dispose();
}
public abstract boolean showApplyButton();
public abstract void apply();
public void dispose() {
if (mCancelState != OK) {
mCancelState = CANCEL;
}
synchronized(this) {
notifyAll();
}
super.dispose();
}
protected class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
JButton b = (JButton) ae.getSource();
if (b == mApply) {
apply();
}
else if (b == mOk) {
ok();
}
else if (b == mCancel) {
cancel();
}
}
}
public int getCancelState() {
return mCancelState;
}
public static int showStandardDialog(StandardDialog sd) {
Window w = sd.getOwner();
if (w == null) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension ps = sd.getPreferredSize();
return showStandardDialog(sd, (d.width - ps.width) / 2, (d.height - ps.height) / 2);
}
else {
Point p = w.getLocation();
Dimension d = w.getSize();
return showStandardDialog(sd, p.x + 100, p.y + 20);
}
}
public static int showStandardDialog(final StandardDialog sd, int x, int y) {
sd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
sd.cancel();
}
});
sd.setLocation(x, y);
sd.pack();
sd.setVisible(true);
synchronized(sd) {
try {
sd.wait();
}
catch (InterruptedException ex) {}
}
return sd.getCancelState();
}
public static final int OK = 1;
public static final int SHOWN = 0;
public static final int CANCEL = -1;
private int mCancelState = 0;
protected boolean mBlockInCode;
protected Container mContent;
protected Component mMainPane;
protected JButton mApply;
protected JButton mOk;
protected JButton mCancel;
protected JPanel mButtonPane;
protected ActionListener mButtonListener;
}
# 8
Ok, I corrected as you said, I was opening another JFrame, that was the reason of the icon on the toolbar. But now I got another trouble, when I use only a JDialog as you said, and when I open the JDialog and change another window with alt+tab, and return to my window and I close the JDialog, the JFrame get back (I don't want to use setAlwaysOnTop() ), this is the same without doing this actions, changing the windows.I think there is another way to work over this, but can you help me please... this is my code:
/**Clase JDialogTest.java
* Autor Wilfredo Vargas Almendras
* Fecha 30/01/2007
* Hora 04:25:13 PM
* Proyecto Yachay Wasej Punkun
*
*/
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class JDialogTest extends JDialog {
private Toolkit toolkit;
private Dimension dimension;
private int width;
private int height;
private JFrame parent;
private JTextField text;
private JButton ok;
public JDialogTest(){
init();
}
public JDialogTest(JFrame parent) {
super(parent);
this.parent = parent;
init();
}
public void init(){
createComponents();
setProperties();
addComponents();
}
public void createComponents(){
text = new JTextField("Wilfredo");
ok = new JButton("ok");
toolkit = Toolkit.getDefaultToolkit();
dimension = toolkit.getScreenSize();
width = dimension.width;
height = dimension.height;
}
public void setProperties(){
setUndecorated(true);
getRootPane().setWindowDecorationStyle(1);
setBounds(50, 50, 400, 300);
setLayout(null);
text.setBounds(20,50,100,20);
ok.setBounds(20, 80, 80, 20);
ok.addMouseListener( new MouseAdapter(){
public void mouseClicked( MouseEvent e ){
close();
}
});
}
private void addComponents() {
add(text);
add(ok);
}
public void setVisibleDialog( boolean b ){
setVisible(b);
text.requestFocusInWindow();
parent.setEnabled(!b);
}
public void close(){
setVisibleDialog(false);
}
public static void main(String[] args) {
JFrame window = new JFrame();
window.setBounds(100,200,400,300);
window.setTitle("TEST");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("show");
window.add( button );
final JDialogTest test = new JDialogTest(window);
button.addMouseListener(new MouseAdapter(){
public void mouseClicked( MouseEvent e){
test.setVisibleDialog(true);
}
});
window.setVisible(true);
}
}
# 9
I doubt this can be fixed. I've seen this same condition from many non-java applications as well
# 10
Thanks tjacobs01, but it happens the same, with my first solution, I mean with a JOptionPane, it works ok, but I have the focus trouble, and the text.selectAll().. maybe I cannot do this... :( ... I will see another way, but if anybody has a suggestion, please ... help me :) ..
# 11
> and when I open the JDialog and change another window with
> alt+tab, and return to my window and I close the JDialog, the JFrame
> get back (I don't want to use setAlwaysOnTop() ),
When you say "get back", I assume you mean its get hidden behind the other applications. At least that what happens to me when I run your demo. However I also noticed that even if I select the frame from the task bar I can't click on it (I get a "beep sound). So its like the modal dialog is somehow still active.
Anyway, your code is rather confusing . Here is a simple example that works correctly:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=621522
# 12
I modified my code, but have my first trouble, i cannot set focus over the textfield, I mean when I open and close the windows many times.... Here is my code:
/**Clase DialogModal.java
* Autor Wilfredo Vargas Almendras
* Fecha 30/01/2007
* Hora 05:32:17 PM
* Proyecto Yachay Wasej Punkun
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogModal extends JFrame implements ActionListener
{
JDialog dialog = new JDialog(this, "Modal Test", true);
JTextField text = new JTextField("Wilfredo");
JButton boton = new JButton("OK");
public DialogModal()
{
JButton button = new JButton("Show Dialog");
button.addActionListener( this );
getContentPane().add( button );
dialog.setLayout(new FlowLayout());
dialog.add(text);
dialog.add(boton);
boton.addMouseListener( new MouseAdapter(){
public void mouseClicked( MouseEvent e ){
dialog.setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
/* here I can create a JDialog each time, but I have to show the last status, and it would be inefficient to create a JDialog, and this was my first idea to solve my code, but I think this could be better. */
dialog.setSize(300, 300);
dialog.show();
text.requestFocus();
text.grabFocus();
text.requestFocusInWindow();
}
public static void main(String[] args)
{
JFrame frame = new DialogModal();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
Please another suggestion
# 13
Did you read my first reply? Code placed after the setVisible is not executed until after the dialog is closed. Thats why you are using a modal dialog.