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

时间:2015-05-05 20:21:01

标签: java swing jpanel jtextarea jdialog

我希望使用自定义JTextArea更新主布局中的JDialog。很难用这样的词来解释......所以请按照下面的代码和解释流程进行解释:

  1. 我有一个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);
                }
            }
        }
    } }
    
  2. 请注意上面代码的最后一位>>这是自定义JDialog被触发或调用的地方。

    1. 我有运行整个应用程序的主类:

      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);
      } }
      
    2. 我有一个特殊的帮助类 - 我在整个应用程序中保留了所有有用的代码来实现:

      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);
      } }
      
    3. 请注意:我显然已经清理了很多代码以仅显示重要部分。

      我需要自定义对话框在单击JTextArea时从JTextArea中提取所有当前文本,然后允许用户继续编辑自定义JDialog中另一个JTextArea中的文本(即custPopupDialog)。当用户点击“OKAY”时自定义JDialog中的按钮应该将自定义对话框中的文本反馈给主类的原始(即cancellationPolicTA)JTextArea。

      一切正常(即数据被拉入自定义JDialog并正确显示JDialog),但是当用户点击“OKAY”时,文本数据不会传回原始的JTextArea。自定义JDialog中的按钮。请帮忙。提前致谢。

      1. 我忘了粘贴自定义JDialog代码 - (见下文):
      2.   

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

1 个答案:

答案 0 :(得分:2)

查看一些使用评论更新的代码,

首先删除stanAPP = new Stan_App(“”);来自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);

在Mcgyver类和popCustomDialogTExtAreaObject中,您需要返回更新文本的方法。请参阅下面添加的评论方法,

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