简体   繁体   中英

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 . 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:

     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.

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

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. Please help. Thanks in advance.

  1. I forgot to paste in the Custom JDialog code - (see below):

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

See some of the code updated with comments,

First remove the stanAPP = new Stan_App(""); from the Panel_Custom_Dialog class and pass a reference to it, since it should have been instantiated else where,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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