[英]Update JTextArea text in JFrame from Custom JDialog output data or results
我要使用自定義JDialog
更新主布局中的JTextArea
。 很難用這樣的詞來解釋...所以請遵循以下概述的代碼和解釋過程:
我有一個包含需要更新的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); } } } } }
請注意上面>>的最后一段代碼,這是自定義JDialog被激發或調用的地方。
我有運行整個應用程序的主類:
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); } }
我有一個特殊的幫助器類-在其中保存所有有用的代碼,以在整個應用程序中實施:
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時,我需要的Custom Dialog從JTextArea中提取所有當前文本,然后允許用戶繼續編輯自定義JDialog中另一個JTextArea(即custPopupDialog)中的文本。 當用戶單擊定制JDialog內的“ OKAY”按鈕時,來自定制對話框的文本應反饋到主類的原始JTextArea(即cancelPolicTA)。
一切正常(例如,數據被拉入自定義JDialog中,並且JDialog正確顯示),但是當用戶單擊自定義JDialog中的“ OKAY”按鈕時,文本數據不會傳遞回原始JTextArea。 請幫忙。 提前致謝。
公共類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();
}
} }
查看一些用注釋更新的代碼,
首先刪除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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.