简体   繁体   English

从自定义JDialog输出数据或结果更新JFrame中的JTextArea文本

[英]Update JTextArea text in JFrame from Custom JDialog output data or results

I have a JTextArea inside a main layout that I want to update using a custom JDialog . 我要使用自定义JDialog更新主布局中的JTextArea Difficult to explain in words like this...so please just follow code and explanation process as outlined below: 很难用这样的词来解释...所以请遵循以下概述的代码和解释过程:

  1. I have a JPanel that contains the JTextArea (ie original JTextArea - 'cancellationPolicTA') that needs to be updated: 我有一个包含需要更新的JTextArea(即原始JTextArea-'cancellationPolicTA')的JPanel:

     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); } } } } } 

Please take NOTE of the last bit of code above >> this is where the custom JDialog gets fired or called from. 请注意上面>>的最后一段代码,这是自定义JDialog被激发或调用的地方。

  1. I have the main class that runs the whole application: 我有运行整个应用程序的主类:

     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 (" STA 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); } } 
  2. I have a special helper class - where I keep all my useful code for implementation throughout the application: 我有一个特殊的帮助器类-在其中保存所有有用的代码,以在整个应用程序中实施:

     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); } } 

Please NOTE: I have obviously cleaned up the code a lot to show only the important parts. 请注意:我显然已经清理了很多代码,只显示了重要部分。

The Custom Dialog I need pulls all the current text from the JTextArea when the JTextArea is clicked on and then allows the user to continue editing the text inside another JTextArea in the custom JDialog (ie custPopupDialog). 当单击JTextArea时,我需要的Custom Dialog从JTextArea中提取所有当前文本,然后允许用户继续编辑自定义JDialog中另一个JTextArea(即custPopupDialog)中的文本。 When the user clicks on the 'OKAY' button inside the custom JDialog the text from the custom dialog should be fed back to the original (ie cancellationPolicTA) JTextArea of the main class. 当用户单击定制JDialog内的“ OKAY”按钮时,来自定制对话框的文本应反馈到主类的原始JTextArea(即cancelPolicTA)。

Everything works fine (ie data gets pulled into the custom JDialog and JDialog is displayed properly), but the text data is NOT handed back to the original JTextArea when the user clicks on the 'OKAY' button inside the custom JDialog. 一切正常(例如,数据被拉入自定义JDialog中,并且JDialog正确显示),但是当用户单击自定义JDialog中的“ OKAY”按钮时,文本数据不会传递回原始JTextArea。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

  1. I forgot to paste in the Custom JDialog code - (see below): 我忘了粘贴自定义JDialog代码-(请参见下文):

public class Panel_Custom_Dialog extends JPanel { 公共类Panel_Custom_Dialog扩展了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();
    }
} }

See some of the code updated with comments, 查看一些用注释更新的代码,

First remove the stanAPP = new Stan_App(""); 首先删除stanAPP = new Stan_App(“”); from the Panel_Custom_Dialog class and pass a reference to it, since it should have been instantiated else where, 从Panel_Custom_Dialog类中传递并引用它,因为它应该在其他地方实例化,

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);

In the Mcgyver class and the popCustomDialogTExtAreaObject you need methods that will return the updated text. 在Mcgyver类和popCustomDialogTExtAreaObject中,您需要将返回更新后的文本的方法。 see the added method below with comments, 请参阅以下带有注释的添加方法,

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();  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM