I have created a modal JDialog box with a custom drawing on it and a JButton. When I click the JButton, the JDialog box should close and a value should be returned.
我创建了一个模态JDialog框,其中有一个自定义绘图和一个JButton。当我单击JButton时,应该关闭JDialog框并返回一个值。
I have created a function in the parent JFrame called setModalPiece, which receives a value and sets it to a local JFrame variable.
我在父JFrame中创建了一个名为setModalPiece的函数,该函数接收一个值并将其设置为本地JFrame变量。
The problem is that this function is not visible from the JDialog box (even though the JDialog box has a reference to the parent JFrame).
问题是这个函数在JDialog框中是不可见的(即使JDialog框有对父JFrame的引用)。
Two questions: 1) Is there a better way to return a value from a JDialog box to its parent JFrame?
两个问题:1)是否有更好的方法从JDialog框返回值到其父JFrame?
2) Why can't the reference to the JFrame passed to the JDialog be used to access my JFrame function setModalPiece?
2)为什么不能使用传递给JDialog的JFrame的引用来访问我的JFrame函数setModalPiece?
6 个解决方案
#1
21
You should do the opposite by adding a custom method getValue()
to your custom JDialog
.
相反,应该向自定义JDialog中添加自定义方法getValue()。
In this way you can ask the value of the dialog from the JFrame
instead that setting it by invoking something on the JFrame
itself.
通过这种方式,您可以从JFrame中询问对话框的值,而不是通过调用JFrame本身上的某些东西来设置它。
If you take a look at Oracle tutorial about dialogs here it states
如果您看一下Oracle关于对话框的教程,它会在这里声明
If you're designing a custom dialog, you need to design your dialog's API so that you can query the dialog about what the user chose. For example, CustomDialog has a getValidatedText method that returns the text the user entered.
如果正在设计自定义对话框,则需要设计对话框的API,以便查询对话框中用户选择的内容。例如,CustomDialog有一个getValidatedText方法,它返回用户输入的文本。
(you can find source of CustomDialog
to see how they suppose that you will design your custom dialog)
(你可以找到CustomDialog的源代码,看看他们认为你将如何设计你的自定义对话框)
#2
96
I generally do it like this:
我通常这样做:
Dialog dlg = new Dialog(this, ...);
Value result = dlg.showDialog();
The Dialog.showDialog()
function looks like this:
函数的作用是:
ReturnValue showDialog() {
setVisible(true);
return result;
}
Since setting visibility to true on a JDialog is a modal operation, the OK button can set an instance variable (result
) to the chosen result of the dialog (or null
if canceled). After processing in the OK/Cancel button method, do this:
由于在JDialog中将可视性设置为true是一种模式操作,OK按钮可以将实例变量(结果)设置为对话框所选择的结果(如果取消,则为null)。在处理OK/取消按钮方法后,这样做:
setVisible(false);
dispose();
to return control to the showDialog()
function.
返回控件到showDialog()函数。
#3
4
I dont know if i can explain my method in a cool way... Lets say i need productPrice and ammount from a JDialog whos going to get that info from user, i need to call that from the JFrame.
我不知道我能不能用一种很酷的方式来解释我的方法……假设我需要productPrice和ammount从JDialog中获取用户的信息,我需要从JFrame调用它。
declare productPrice and ammount as public non-static global variables inside the JDialog.
在JDialog中将productPrice和ammount声明为公共非静态全局变量。
public float productPrice;
public int ammount;
* this goes inside the dialog's class global scope.
这将进入对话框的类全局作用域。
add these lines in the JDialog constructor to ensure modality
在JDialog构造函数中添加这些行以确保modality
super((java.awt.Frame) null, true);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
* this goes within the dialog's class constructor
*在对话框的类构造函数中。
lets say your JDialog's class name is 'MyJDialog', when calling do something like this
让我们假设您的JDialog的类名是“jmydialog”,当调用时执行如下操作
MyJDialog question = new MyJDialog();
MyJDialog.setVisible(true);
// Application thread will stop here until MyJDialog calls dispose();
// this is an effect of modality
//
// When question calls for dispose(), it will leave the screen,
// but its global values will still be accessible.
float myTotalCostVar = question.productPrice * question.ammount;
// this is acceptable.
// You can also create public getter function inside the JDialog class,
// its safer and its a good practice.
* this goes in any function within your JFrame and will call JDialog to get infos.
这将进入JFrame中的任何函数,并将调用JDialog来获取信息。
#4
0
When you pass any value to JFrame to JDialog then create parametrized constructor of jdialog and in jframe whenever you want to call. e.g. The parametrized constructor like :
当您将任何值传递到JFrame到JDialog时,就会在JFrame中创建参数化的JDialog构造函数,并在任何时候调用。参数化构造函数如:
public EditProduct(java.awt.Frame parent, boolean modal, int no) {
//int no is number of product want to edit.
//Now we can use this pid in JDialog and perform whatever you want.
}
When you want to pass values from JDialog to JFrame create a bean class with set and get method the the values using vector and get these values in jframe. More info
当您想从JDialog传递值到JFrame时,创建一个带有set的bean类,并使用vector获取值,并在JFrame中获取这些值。更多信息
#5
0
Here is how I usually do it. I wasn't sure, that's why I've created that post:
我通常是这样做的。我不确定,这就是我写这篇文章的原因:
Returning value from JDialog; dispose(), setVisible(false) - example
从JDialog返回值;处理(),setVisible(假)的例子
#6
0
Add an interface to your constructor?
向构造函数添加接口?
public class UploadConfimation extends JDialog {
private final JPanel contentPanel = new JPanel();
public interface GetDialogResponse{
void GetResponse(boolean response);
}
/**
* Create the dialog.
*/
public UploadConfimation(String title, String message, GetDialogResponse result) {
setBounds(100, 100, 450, 300);
setTitle(title);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JLabel lblMessage = new JLabel(message);
contentPanel.add(lblMessage);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("YES");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(true);
dispose();
}
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("NO");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}
}
}
#1
21
You should do the opposite by adding a custom method getValue()
to your custom JDialog
.
相反,应该向自定义JDialog中添加自定义方法getValue()。
In this way you can ask the value of the dialog from the JFrame
instead that setting it by invoking something on the JFrame
itself.
通过这种方式,您可以从JFrame中询问对话框的值,而不是通过调用JFrame本身上的某些东西来设置它。
If you take a look at Oracle tutorial about dialogs here it states
如果您看一下Oracle关于对话框的教程,它会在这里声明
If you're designing a custom dialog, you need to design your dialog's API so that you can query the dialog about what the user chose. For example, CustomDialog has a getValidatedText method that returns the text the user entered.
如果正在设计自定义对话框,则需要设计对话框的API,以便查询对话框中用户选择的内容。例如,CustomDialog有一个getValidatedText方法,它返回用户输入的文本。
(you can find source of CustomDialog
to see how they suppose that you will design your custom dialog)
(你可以找到CustomDialog的源代码,看看他们认为你将如何设计你的自定义对话框)
#2
96
I generally do it like this:
我通常这样做:
Dialog dlg = new Dialog(this, ...);
Value result = dlg.showDialog();
The Dialog.showDialog()
function looks like this:
函数的作用是:
ReturnValue showDialog() {
setVisible(true);
return result;
}
Since setting visibility to true on a JDialog is a modal operation, the OK button can set an instance variable (result
) to the chosen result of the dialog (or null
if canceled). After processing in the OK/Cancel button method, do this:
由于在JDialog中将可视性设置为true是一种模式操作,OK按钮可以将实例变量(结果)设置为对话框所选择的结果(如果取消,则为null)。在处理OK/取消按钮方法后,这样做:
setVisible(false);
dispose();
to return control to the showDialog()
function.
返回控件到showDialog()函数。
#3
4
I dont know if i can explain my method in a cool way... Lets say i need productPrice and ammount from a JDialog whos going to get that info from user, i need to call that from the JFrame.
我不知道我能不能用一种很酷的方式来解释我的方法……假设我需要productPrice和ammount从JDialog中获取用户的信息,我需要从JFrame调用它。
declare productPrice and ammount as public non-static global variables inside the JDialog.
在JDialog中将productPrice和ammount声明为公共非静态全局变量。
public float productPrice;
public int ammount;
* this goes inside the dialog's class global scope.
这将进入对话框的类全局作用域。
add these lines in the JDialog constructor to ensure modality
在JDialog构造函数中添加这些行以确保modality
super((java.awt.Frame) null, true);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
* this goes within the dialog's class constructor
*在对话框的类构造函数中。
lets say your JDialog's class name is 'MyJDialog', when calling do something like this
让我们假设您的JDialog的类名是“jmydialog”,当调用时执行如下操作
MyJDialog question = new MyJDialog();
MyJDialog.setVisible(true);
// Application thread will stop here until MyJDialog calls dispose();
// this is an effect of modality
//
// When question calls for dispose(), it will leave the screen,
// but its global values will still be accessible.
float myTotalCostVar = question.productPrice * question.ammount;
// this is acceptable.
// You can also create public getter function inside the JDialog class,
// its safer and its a good practice.
* this goes in any function within your JFrame and will call JDialog to get infos.
这将进入JFrame中的任何函数,并将调用JDialog来获取信息。
#4
0
When you pass any value to JFrame to JDialog then create parametrized constructor of jdialog and in jframe whenever you want to call. e.g. The parametrized constructor like :
当您将任何值传递到JFrame到JDialog时,就会在JFrame中创建参数化的JDialog构造函数,并在任何时候调用。参数化构造函数如:
public EditProduct(java.awt.Frame parent, boolean modal, int no) {
//int no is number of product want to edit.
//Now we can use this pid in JDialog and perform whatever you want.
}
When you want to pass values from JDialog to JFrame create a bean class with set and get method the the values using vector and get these values in jframe. More info
当您想从JDialog传递值到JFrame时,创建一个带有set的bean类,并使用vector获取值,并在JFrame中获取这些值。更多信息
#5
0
Here is how I usually do it. I wasn't sure, that's why I've created that post:
我通常是这样做的。我不确定,这就是我写这篇文章的原因:
Returning value from JDialog; dispose(), setVisible(false) - example
从JDialog返回值;处理(),setVisible(假)的例子
#6
0
Add an interface to your constructor?
向构造函数添加接口?
public class UploadConfimation extends JDialog {
private final JPanel contentPanel = new JPanel();
public interface GetDialogResponse{
void GetResponse(boolean response);
}
/**
* Create the dialog.
*/
public UploadConfimation(String title, String message, GetDialogResponse result) {
setBounds(100, 100, 450, 300);
setTitle(title);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JLabel lblMessage = new JLabel(message);
contentPanel.add(lblMessage);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("YES");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(true);
dispose();
}
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("NO");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result.GetResponse(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}
}
}