java swing自定义对话框,使用java swing显示两个对话框

i have a situation where i show a dialog where user has to fill some menus and then press OK. It works fine, but now i have another button on this dialog that if user wants to add some certain value, i want another dialog to popup where user fills the additional value and while pressing ok, this dialog disappears and user comes back to the main dialog.

I have tried this, but every time i call the new dialog, the focus does not go away from the main dialog, how can i do such a task.

Is there any relevant example or what is the proper way of doing such things.

EDIT:

public static class EdgeMenu extends JPopupMenu {

// private JFrame frame;

public MyMenu(final JFrame frame) {

super("My Menu");

// this.frame = frame;

this.addSeparator();

this.add(new EdgePropItem(frame));

}

}

//this shows the first dialog, another class because i have some other

//functions to be performed here

public static class EdgePropItem extends JMenuItem{

//...

public EdgePropItem(final JFrame frame) {

super("Edit Properties");

this.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

EdgePropertyDialog dialog = new EdgePropertyDialog(frame, edge);

dialog.setVisible(true);

}

});

}

}

and now in other dialog, in the button even listener i am trying to call another dialog:

private void newDialogHandler(java.awt.event.ActionEvent evt) {

MyNewDialog rdialog = new MyNewDialog(edge);

rdialog.setVisible(true);

}

It appears fine, but the previous dialog, does not leave the focus, and it goes away only if i press finish/done on that dialog, what i want is the new dialog to come in focus, while pressing ok on here, focus should come back to the old main dialog, but it is not working?

解决方案

maybe this code could be demonstate your issues,

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SuperConstructor extends JFrame {

private static final long serialVersionUID = 1L;

public SuperConstructor() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setPreferredSize(new Dimension(300, 300));

setTitle("Super constructor");

Container cp = getContentPane();

JButton b = new JButton("Show dialog");

b.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent evt) {

FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);

}

});

cp.add(b, BorderLayout.SOUTH);

JButton bClose = new JButton("Close");

bClose.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent evt) {

System.exit(0);

}

});

add(bClose, BorderLayout.NORTH);

pack();

setVisible(true);

}

public static void main(String args[]) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

SuperConstructor superConstructor = new SuperConstructor();

}

});

}

private class FirstDialog extends JDialog {

private static final long serialVersionUID = 1L;

FirstDialog(final Frame parent) {

super(parent, "FirstDialog");

setPreferredSize(new Dimension(200, 200));

setLocationRelativeTo(parent);

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);

JButton bNext = new JButton("Show next dialog");

bNext.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent evt) {

SecondDialog secondDialog = new SecondDialog(parent, false);

}

});

add(bNext, BorderLayout.NORTH);

JButton bClose = new JButton("Close");

bClose.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent evt) {

setVisible(false);

}

});

add(bClose, BorderLayout.SOUTH);

pack();

setVisible(true);

}

}

private int i;

private class SecondDialog extends JDialog {

private static final long serialVersionUID = 1L;

SecondDialog(final Frame parent, boolean modal) {

//super(parent); // < --- Makes this dialog

//unfocusable as long as FirstDialog is visible

setPreferredSize(new Dimension(200, 200));

setLocation(300, 50);

setModal(modal);

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

setTitle("SecondDialog " + (i++));

JButton bClose = new JButton("Close");

bClose.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent evt) {

setVisible(false);

}

});

add(bClose, BorderLayout.SOUTH);

pack();

setVisible(true);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值