从自定义 JDialog 输出数据或结果更新 JFrame 中的 JTextArea 文本
Update JTextArea text in JFrame from Custom JDialog output data or results
我在主布局中有一个 JTextArea
,我想使用自定义 JDialog
对其进行更新。很难用这样的话来解释......所以请按照下面概述的代码和解释过程:
我有一个 JPanel,其中包含需要更新的 JTextArea(即原始 JTextArea - 'cancellationPolicTA'):
public class Panel_Other_Information extends JPanel {
private JTextArea cancellationPolicyTA, otherInformationTA;
public final String cancellationPolicyBorderTXT = " Cancellation Policy ";
public final String additionalInformationBorderTXT = " Other Information ";
public Panel_Other_Information () {
// Create and set up the window.
JPanel thisPanel = new JPanel ();
// [1] Define the Dimensions of the Panel.
Dimension panelSize = getPreferredSize();
panelSize.width = 520;
panelSize.height = 100;
setPreferredSize(panelSize);
setBorder(BorderFactory.createTitledBorder(" 15. Additional Information "));
// [2] Use the 'SpringLayout' to set or define the layout of components within this panel.
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(McGyver.APP_THEME_COLOR);
// [3] Define required Label components/controls.
JLabel cancellationPolicyLabel = new JLabel ("Cancellation Policy:");
JLabel otherInformationLabel = new JLabel ("Other Information:");
String canPolTxt = "No Cancellation Policy";
String othInfTxt = "No Additional Information";
// [4] Define required input (TextField) controls/components.
final int widthCB = 230;
final int heightCB = 48;
cancellationPolicyTA = new JTextArea(canPolTxt);
cancellationPolicyTA.setEditable(false);
cancellationPolicyTA.setWrapStyleWord(true);
cancellationPolicyTA.addMouseListener(new CancelPolicyMouseListener());
cancellationPolicyTA.addFocusListener(new CancelPolicyFocusListener());
otherInformationTA = new JTextArea(othInfTxt);
otherInformationTA.setEditable(false);
otherInformationTA.setWrapStyleWord(true);
otherInformationTA.addMouseListener(new OtherInformationMouseListener());
JScrollPane canPolTAScrollPane = new JScrollPane(cancellationPolicyTA);
canPolTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
JScrollPane otherInfoTAScrollPane = new JScrollPane(otherInformationTA);
otherInfoTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
// [5] Define button controls - if needed.
/* JButton saveDataBTN = new JButton("Save"); */
////////////////////////////////////////////////////////////////////////
// Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
/* -- Component 1 - Additional Information - Cancellation Policy Label */
layout.putConstraint(SpringLayout.NORTH, cancellationPolicyLabel, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, cancellationPolicyLabel, 0, SpringLayout.EAST, thisPanel);
/* -- Component 2 - Additional Information - Cancellation Policy Text Area */
layout.putConstraint(SpringLayout.NORTH, canPolTAScrollPane, 5, SpringLayout.SOUTH, cancellationPolicyLabel);
layout.putConstraint(SpringLayout.WEST, canPolTAScrollPane, 0, SpringLayout.WEST, cancellationPolicyLabel);
/* -- Component 1 - Additional Information - Cancellation Policy Label */
layout.putConstraint(SpringLayout.NORTH, otherInformationLabel, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, otherInformationLabel, 30, SpringLayout.EAST, canPolTAScrollPane);
/* -- Component 2 - Additional Information - Cancellation Policy Text Area */
layout.putConstraint(SpringLayout.NORTH, otherInfoTAScrollPane, 5, SpringLayout.SOUTH, otherInformationLabel);
layout.putConstraint(SpringLayout.WEST, otherInfoTAScrollPane, 0, SpringLayout.WEST, otherInformationLabel);
// [4] Add Swing components to content pane.
add(cancellationPolicyLabel);
add(canPolTAScrollPane);
add(otherInformationLabel);
add(otherInfoTAScrollPane);
}
private class CancelPolicyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
String cancelPolicyText = cancellationPolicyTA.getText();
if (cancelPolicyText.length() > 0) {
String dialogTitle = "15. Additional Information";
String borderTitle = cancellationPolicyBorderTXT;
McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText);
}
}
}
} }
请注意上面的最后一段代码 >> 这是触发或调用自定义 JDialog 的地方。
我有运行整个应用程序的主要class:
public class Stan_App extends JFrame {
// [1] Instantiate all SWING components needed for this application.
public static Stan_App stanFrame;
private McGyver mcGyver = new McGyver();
public Panel_Other_Information additionalInformation = new Panel_Other_Information();
public Stan_App (String title) {
super(title);
// [1] Set LayoutManager
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(Color.CYAN);
////////////////////////////////////////////////////////////////////////
// [2] Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
//-- Component 1 - JPanel - Additional Information --//
layout.putConstraint(SpringLayout.NORTH, additionalInformation, 17, SpringLayout.SOUTH, neighbourhoodRating);
layout.putConstraint(SpringLayout.WEST, additionalInformation, 20, SpringLayout.EAST, inRoomFacilities);
// [3] Add Swing components to content pane.
Container conTain = getContentPane();
conTain.add(additionalInformation);
}
public static void main (String[] args) {
SwingUtilities.invokeLater (new Runnable() {
@Override
public void run() {
createStanAppGUI();
}
});
}
private static void createStanAppGUI () {
// Override the default Font for the application.
McGyver.setUIFont(McGyver.APP_GLOBAL_FONT);
// This code chuck lays out the visual components (GUI) of the app.
stanFrame = new Stan_App (" S.T.A. Namibia - Database Manager " + " | THEME: " + McGyver.currentlySelectedAppThemeColor + " |");
// UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Noto Sans", Font.BOLD, 42));
stanFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
stanFrame.setSize(1925, 1050);
stanFrame.setResizable(false);
stanFrame.setVisible(true);
themeColorCB.setSelectedItem(McGyver.currentlySelectedAppThemeColor);
}
public void setCancellationTextFromCustomPopupDialog (String suppliedText) {
additionalInformation.setCancellation_Policy_TextArea_Value(suppliedText);
} }
我有一个特殊的帮手 class - 我将所有有用的代码保存在整个应用程序中:
public class McGyver {
public static JDialog custPopupDialog;
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
Panel_Custom_Dialog custDial = new Panel_Custom_Dialog(Stan_App.stanFrame, borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
} }
请注意:我显然已经清理了很多代码以仅显示重要部分。
我需要的自定义对话框在单击 JTextArea 时从 JTextArea 中提取所有当前文本,然后允许用户继续在自定义 JDialog(即 custPopupDialog)中的另一个 JTextArea 中编辑文本。当用户单击自定义 JDialog 内的 'OKAY' 按钮时,自定义对话框中的文本应反馈到主要 class.
的原始(即 cancellationPolicTA)JTextArea
一切正常(即数据被拉入自定义 JDialog 并正确显示 JDialog),但是当用户单击自定义 JDialog。请帮忙。提前致谢。
- 我忘记粘贴自定义 JDialog 代码 -(见下文):
public class Panel_Custom_Dialog extends JPanel {
Stan_App stanAPP;
String borderHeaderString, editedCancellationPolicyTXT;
private JTextArea custDialogTA;
private Panel_Other_Information otherInfoPanel = new Panel_Other_Information();
public Panel_Custom_Dialog (Stan_App stanApp, String borderTitle, String inputMessageString) {
stanAPP = stanApp;
// Create and set up the window.
JPanel thisPanel = new JPanel();
borderHeaderString = null;
if (borderTitle == null) {
borderHeaderString = " Section Header Here! ";
} else {
borderHeaderString = borderTitle;
}
// [1] Define the Dimensions of the Panel.
Dimension panelSize = getPreferredSize();
panelSize.width = 500;
panelSize.height = 400;
setPreferredSize(panelSize);
setBorder(BorderFactory.createTitledBorder(borderHeaderString));
// [2] Use the 'SpringLayout' to set or define the layout of components within this panel.
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(McGyver.APP_THEME_COLOR);
// [3] Define the
final int widthCB = 450;
final int heightCB = 300;
// [4] Define the required components for this Panel.
custDialogTA = new JTextArea(inputMessageString);
custDialogTA.setBounds(5, 5, 0, 0);
custDialogTA.setLineWrap(true);
custDialogTA.setWrapStyleWord(true);
JScrollPane custDialogTextAreaScrollPane = new JScrollPane(custDialogTA);
custDialogTextAreaScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
// [3] Define the Button size fields.
final int widthBTN = 100;
final int heightBTN = 25;
JButton affirmativeBTN = new JButton(" OKAY "); affirmativeBTN.addActionListener(new CustomDialogAffirmationActionListener());
JButton cancellationBTN = new JButton(" CANCEL "); cancellationBTN.addActionListener(new CustomDialogCancellationActionListener());
affirmativeBTN.setPreferredSize(new Dimension(widthBTN, heightBTN));
cancellationBTN.setPreferredSize(new Dimension(widthBTN, heightBTN));
////////////////////////////////////////////////////////////////////////
// Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
/* -- Component 1 - Additional Information - Custom Dialog TextArea */
layout.putConstraint(SpringLayout.NORTH, custDialogTextAreaScrollPane, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, custDialogTextAreaScrollPane, 0, SpringLayout.EAST, thisPanel);
/* -- Component 2 - Additional Information - Custom Dialog Affirmation Button */
layout.putConstraint(SpringLayout.NORTH, affirmativeBTN, 10, SpringLayout.SOUTH, custDialogTextAreaScrollPane);
layout.putConstraint(SpringLayout.WEST, affirmativeBTN, 0, SpringLayout.EAST, thisPanel);
/* -- Component 3 - Additional Information - Custom Dialog Cancellation Button */
layout.putConstraint(SpringLayout.NORTH, cancellationBTN, 0, SpringLayout.NORTH, affirmativeBTN);
layout.putConstraint(SpringLayout.WEST, cancellationBTN, 25, SpringLayout.EAST, affirmativeBTN);
// [5] Add the components defined above to the panel.
add(custDialogTextAreaScrollPane);
add(affirmativeBTN);
add(cancellationBTN);
}
public String getCust_Dialog_TextArea_Value () {
return custDialogTA.getText();
}
public void setCust_Dialog_TextArea_Value (String inputString) {
custDialogTA.setText(inputString);
}
private class CustomDialogAffirmationActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (borderHeaderString.equals(otherInfoPanel.cancellationPolicyBorderTXT)) {
editedCancellationPolicyTXT = custDialogTA.getText();
stanAPP.setCancellationTextFromCustomPopupDialog(editedCancellationPolicyTXT);
McGyver.custPopupDialog.dispose();
} else if (borderHeaderString.equals(otherInfoPanel.additionalInformationBorderTXT)) {
editedCancellationPolicyTXT = custDialogTA.getText();
stanAPP.setOtherInformationTextFromCustomPopupDialog(editedCancellationPolicyTXT);
McGyver.custPopupDialog.dispose();
}
}
}
private class CustomDialogCancellationActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
McGyver.custPopupDialog.dispose();
}
} }
查看一些更新了注释的代码,
首先删除stanAPP = new Stan_App("");来自 Panel_Custom_Dialog class 并传递对它的引用,因为它应该在其他地方实例化,
McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText);
//* the popCustomDialog is Modal, this code above should block until the user hits ok or cancel,
String updatedText = McGyver.getInputString();
cancellationPolicyTA.setText(updatedText);
在 Mcgyver class 和 popCustomDialogTExtAreaObject 中,您需要 return 更新文本的方法。请参阅下面添加的方法和注释,
public class McGyver {
public static JDialog custPopupDialog;
public static Panel_Custom_Dialog custDial
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
custDial = new Panel_Custom_Dialog(borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
}
//* add a method that will return the updated string.
//* for this to work, Panel_Custom_Dialog should have a method that will return the updated text
public static string getInputString()
{
return custDial.getInputString();
}
}
我在主布局中有一个 JTextArea
,我想使用自定义 JDialog
对其进行更新。很难用这样的话来解释......所以请按照下面概述的代码和解释过程:
我有一个 JPanel,其中包含需要更新的 JTextArea(即原始 JTextArea - 'cancellationPolicTA'):
public class Panel_Other_Information extends JPanel { private JTextArea cancellationPolicyTA, otherInformationTA; public final String cancellationPolicyBorderTXT = " Cancellation Policy "; public final String additionalInformationBorderTXT = " Other Information "; public Panel_Other_Information () { // Create and set up the window. JPanel thisPanel = new JPanel (); // [1] Define the Dimensions of the Panel. Dimension panelSize = getPreferredSize(); panelSize.width = 520; panelSize.height = 100; setPreferredSize(panelSize); setBorder(BorderFactory.createTitledBorder(" 15. Additional Information ")); // [2] Use the 'SpringLayout' to set or define the layout of components within this panel. SpringLayout layout = new SpringLayout(); setLayout(layout); setBackground(McGyver.APP_THEME_COLOR); // [3] Define required Label components/controls. JLabel cancellationPolicyLabel = new JLabel ("Cancellation Policy:"); JLabel otherInformationLabel = new JLabel ("Other Information:"); String canPolTxt = "No Cancellation Policy"; String othInfTxt = "No Additional Information"; // [4] Define required input (TextField) controls/components. final int widthCB = 230; final int heightCB = 48; cancellationPolicyTA = new JTextArea(canPolTxt); cancellationPolicyTA.setEditable(false); cancellationPolicyTA.setWrapStyleWord(true); cancellationPolicyTA.addMouseListener(new CancelPolicyMouseListener()); cancellationPolicyTA.addFocusListener(new CancelPolicyFocusListener()); otherInformationTA = new JTextArea(othInfTxt); otherInformationTA.setEditable(false); otherInformationTA.setWrapStyleWord(true); otherInformationTA.addMouseListener(new OtherInformationMouseListener()); JScrollPane canPolTAScrollPane = new JScrollPane(cancellationPolicyTA); canPolTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB)); JScrollPane otherInfoTAScrollPane = new JScrollPane(otherInformationTA); otherInfoTAScrollPane.setPreferredSize(new Dimension(widthCB, heightCB)); // [5] Define button controls - if needed. /* JButton saveDataBTN = new JButton("Save"); */ //////////////////////////////////////////////////////////////////////// // Define the layout of components - component-by-component // //////////////////////////////////////////////////////////////////////// /* -- Component 1 - Additional Information - Cancellation Policy Label */ layout.putConstraint(SpringLayout.NORTH, cancellationPolicyLabel, 5, SpringLayout.NORTH, thisPanel); layout.putConstraint(SpringLayout.WEST, cancellationPolicyLabel, 0, SpringLayout.EAST, thisPanel); /* -- Component 2 - Additional Information - Cancellation Policy Text Area */ layout.putConstraint(SpringLayout.NORTH, canPolTAScrollPane, 5, SpringLayout.SOUTH, cancellationPolicyLabel); layout.putConstraint(SpringLayout.WEST, canPolTAScrollPane, 0, SpringLayout.WEST, cancellationPolicyLabel); /* -- Component 1 - Additional Information - Cancellation Policy Label */ layout.putConstraint(SpringLayout.NORTH, otherInformationLabel, 5, SpringLayout.NORTH, thisPanel); layout.putConstraint(SpringLayout.WEST, otherInformationLabel, 30, SpringLayout.EAST, canPolTAScrollPane); /* -- Component 2 - Additional Information - Cancellation Policy Text Area */ layout.putConstraint(SpringLayout.NORTH, otherInfoTAScrollPane, 5, SpringLayout.SOUTH, otherInformationLabel); layout.putConstraint(SpringLayout.WEST, otherInfoTAScrollPane, 0, SpringLayout.WEST, otherInformationLabel); // [4] Add Swing components to content pane. add(cancellationPolicyLabel); add(canPolTAScrollPane); add(otherInformationLabel); add(otherInfoTAScrollPane); } private class CancelPolicyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e){ if(e.getClickCount() == 1){ String cancelPolicyText = cancellationPolicyTA.getText(); if (cancelPolicyText.length() > 0) { String dialogTitle = "15. Additional Information"; String borderTitle = cancellationPolicyBorderTXT; McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText); } } } } }
请注意上面的最后一段代码 >> 这是触发或调用自定义 JDialog 的地方。
我有运行整个应用程序的主要class:
public class Stan_App extends JFrame { // [1] Instantiate all SWING components needed for this application. public static Stan_App stanFrame; private McGyver mcGyver = new McGyver(); public Panel_Other_Information additionalInformation = new Panel_Other_Information(); public Stan_App (String title) { super(title); // [1] Set LayoutManager SpringLayout layout = new SpringLayout(); setLayout(layout); setBackground(Color.CYAN); //////////////////////////////////////////////////////////////////////// // [2] Define the layout of components - component-by-component // //////////////////////////////////////////////////////////////////////// //-- Component 1 - JPanel - Additional Information --// layout.putConstraint(SpringLayout.NORTH, additionalInformation, 17, SpringLayout.SOUTH, neighbourhoodRating); layout.putConstraint(SpringLayout.WEST, additionalInformation, 20, SpringLayout.EAST, inRoomFacilities); // [3] Add Swing components to content pane. Container conTain = getContentPane(); conTain.add(additionalInformation); } public static void main (String[] args) { SwingUtilities.invokeLater (new Runnable() { @Override public void run() { createStanAppGUI(); } }); } private static void createStanAppGUI () { // Override the default Font for the application. McGyver.setUIFont(McGyver.APP_GLOBAL_FONT); // This code chuck lays out the visual components (GUI) of the app. stanFrame = new Stan_App (" S.T.A. Namibia - Database Manager " + " | THEME: " + McGyver.currentlySelectedAppThemeColor + " |"); // UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Noto Sans", Font.BOLD, 42)); stanFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); stanFrame.setSize(1925, 1050); stanFrame.setResizable(false); stanFrame.setVisible(true); themeColorCB.setSelectedItem(McGyver.currentlySelectedAppThemeColor); } public void setCancellationTextFromCustomPopupDialog (String suppliedText) { additionalInformation.setCancellation_Policy_TextArea_Value(suppliedText); } }
我有一个特殊的帮手 class - 我将所有有用的代码保存在整个应用程序中:
public class McGyver { public static JDialog custPopupDialog; public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) { Panel_Custom_Dialog custDial = new Panel_Custom_Dialog(Stan_App.stanFrame, borderTitle, inputString); final int widthCB = 500; final int heightCB = 400; custPopupDialog = new JDialog(); custPopupDialog.setTitle(dialogTitle); custPopupDialog.add(custDial); custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */ custPopupDialog.setModal(true); custPopupDialog.setLocationRelativeTo(null); custPopupDialog.setVisible(true); } }
请注意:我显然已经清理了很多代码以仅显示重要部分。
我需要的自定义对话框在单击 JTextArea 时从 JTextArea 中提取所有当前文本,然后允许用户继续在自定义 JDialog(即 custPopupDialog)中的另一个 JTextArea 中编辑文本。当用户单击自定义 JDialog 内的 'OKAY' 按钮时,自定义对话框中的文本应反馈到主要 class.
的原始(即 cancellationPolicTA)JTextArea一切正常(即数据被拉入自定义 JDialog 并正确显示 JDialog),但是当用户单击自定义 JDialog。请帮忙。提前致谢。
- 我忘记粘贴自定义 JDialog 代码 -(见下文):
public class Panel_Custom_Dialog extends JPanel {
Stan_App stanAPP;
String borderHeaderString, editedCancellationPolicyTXT;
private JTextArea custDialogTA;
private Panel_Other_Information otherInfoPanel = new Panel_Other_Information();
public Panel_Custom_Dialog (Stan_App stanApp, String borderTitle, String inputMessageString) {
stanAPP = stanApp;
// Create and set up the window.
JPanel thisPanel = new JPanel();
borderHeaderString = null;
if (borderTitle == null) {
borderHeaderString = " Section Header Here! ";
} else {
borderHeaderString = borderTitle;
}
// [1] Define the Dimensions of the Panel.
Dimension panelSize = getPreferredSize();
panelSize.width = 500;
panelSize.height = 400;
setPreferredSize(panelSize);
setBorder(BorderFactory.createTitledBorder(borderHeaderString));
// [2] Use the 'SpringLayout' to set or define the layout of components within this panel.
SpringLayout layout = new SpringLayout();
setLayout(layout);
setBackground(McGyver.APP_THEME_COLOR);
// [3] Define the
final int widthCB = 450;
final int heightCB = 300;
// [4] Define the required components for this Panel.
custDialogTA = new JTextArea(inputMessageString);
custDialogTA.setBounds(5, 5, 0, 0);
custDialogTA.setLineWrap(true);
custDialogTA.setWrapStyleWord(true);
JScrollPane custDialogTextAreaScrollPane = new JScrollPane(custDialogTA);
custDialogTextAreaScrollPane.setPreferredSize(new Dimension(widthCB, heightCB));
// [3] Define the Button size fields.
final int widthBTN = 100;
final int heightBTN = 25;
JButton affirmativeBTN = new JButton(" OKAY "); affirmativeBTN.addActionListener(new CustomDialogAffirmationActionListener());
JButton cancellationBTN = new JButton(" CANCEL "); cancellationBTN.addActionListener(new CustomDialogCancellationActionListener());
affirmativeBTN.setPreferredSize(new Dimension(widthBTN, heightBTN));
cancellationBTN.setPreferredSize(new Dimension(widthBTN, heightBTN));
////////////////////////////////////////////////////////////////////////
// Define the layout of components - component-by-component //
////////////////////////////////////////////////////////////////////////
/* -- Component 1 - Additional Information - Custom Dialog TextArea */
layout.putConstraint(SpringLayout.NORTH, custDialogTextAreaScrollPane, 5, SpringLayout.NORTH, thisPanel);
layout.putConstraint(SpringLayout.WEST, custDialogTextAreaScrollPane, 0, SpringLayout.EAST, thisPanel);
/* -- Component 2 - Additional Information - Custom Dialog Affirmation Button */
layout.putConstraint(SpringLayout.NORTH, affirmativeBTN, 10, SpringLayout.SOUTH, custDialogTextAreaScrollPane);
layout.putConstraint(SpringLayout.WEST, affirmativeBTN, 0, SpringLayout.EAST, thisPanel);
/* -- Component 3 - Additional Information - Custom Dialog Cancellation Button */
layout.putConstraint(SpringLayout.NORTH, cancellationBTN, 0, SpringLayout.NORTH, affirmativeBTN);
layout.putConstraint(SpringLayout.WEST, cancellationBTN, 25, SpringLayout.EAST, affirmativeBTN);
// [5] Add the components defined above to the panel.
add(custDialogTextAreaScrollPane);
add(affirmativeBTN);
add(cancellationBTN);
}
public String getCust_Dialog_TextArea_Value () {
return custDialogTA.getText();
}
public void setCust_Dialog_TextArea_Value (String inputString) {
custDialogTA.setText(inputString);
}
private class CustomDialogAffirmationActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (borderHeaderString.equals(otherInfoPanel.cancellationPolicyBorderTXT)) {
editedCancellationPolicyTXT = custDialogTA.getText();
stanAPP.setCancellationTextFromCustomPopupDialog(editedCancellationPolicyTXT);
McGyver.custPopupDialog.dispose();
} else if (borderHeaderString.equals(otherInfoPanel.additionalInformationBorderTXT)) {
editedCancellationPolicyTXT = custDialogTA.getText();
stanAPP.setOtherInformationTextFromCustomPopupDialog(editedCancellationPolicyTXT);
McGyver.custPopupDialog.dispose();
}
}
}
private class CustomDialogCancellationActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
McGyver.custPopupDialog.dispose();
}
} }
查看一些更新了注释的代码,
首先删除stanAPP = new Stan_App("");来自 Panel_Custom_Dialog class 并传递对它的引用,因为它应该在其他地方实例化,
McGyver.popCustomDialogTextAreaObject(dialogTitle, borderTitle, cancelPolicyText);
//* the popCustomDialog is Modal, this code above should block until the user hits ok or cancel,
String updatedText = McGyver.getInputString();
cancellationPolicyTA.setText(updatedText);
在 Mcgyver class 和 popCustomDialogTExtAreaObject 中,您需要 return 更新文本的方法。请参阅下面添加的方法和注释,
public class McGyver {
public static JDialog custPopupDialog;
public static Panel_Custom_Dialog custDial
public static void popCustomDialogTextAreaObject (String dialogTitle, String borderTitle, String inputString) {
custDial = new Panel_Custom_Dialog(borderTitle, inputString);
final int widthCB = 500;
final int heightCB = 400;
custPopupDialog = new JDialog();
custPopupDialog.setTitle(dialogTitle);
custPopupDialog.add(custDial);
custPopupDialog.setSize(new Dimension(widthCB, heightCB)); /* Size(550, 450); */
custPopupDialog.setModal(true);
custPopupDialog.setLocationRelativeTo(null);
custPopupDialog.setVisible(true);
}
//* add a method that will return the updated string.
//* for this to work, Panel_Custom_Dialog should have a method that will return the updated text
public static string getInputString()
{
return custDial.getInputString();
}
}