How to connect SWING components in different files

xiaoxiao2021-03-06  41

February 2, 2005 3:43:23

The problem is: Now there is a window class: ProjectDialog.java main interface class is: StudentDataManagerui.java How to make a corresponding change after operation in the window? Suppose there is a JButton type button in ProjectDialog.java, called Button StudientDataManager.java has a JTREE called Tree Requires the button to generate downston: 1. Close the window; 2. Newly built a folder in the background; 3.tree Can change. Solution is actually very simple (I didn't think it was?) / ** The first file ProjectDialog.java * / public class projectdialog Extends JDialog {Private JButton Button; Private String FileName; Private ...... ; ..... public projectDialog (JFrame Frame, String Str, Boolean Ismodel) {b = new jbutton ("Button"); ... setsize (300, 400); setvisible (true);} // Through this public method Components in different files link PUBLIC JBUTTON getButton () {return b;} //, etc., the new folder name is from the public string getFileName () {return filename;}}

/ ** another file StudentDataManager.java * / public class StudentDataManager {private JTree tree; private DefaultMutableTreeNode root; private DefaultTreeModel treeModel; private JButton start; private ProjectDialog projectDialog; private JButton projectDialogButton; .... public StudentDataManager () {start = New jbutton (); root = new defaultmutabletreenode (); treemodel = new defaulttreemodel (); tree = new jtree (treemodel); .......

start.addActionListener (new ActionListener () {public actionPerformed (ActionEvent event) {/ * * This is a crucial step, simply produced a new window to a variable on it * / projectDialog = new ProjectDialog (StudentDataManager.this, "New Project", "false") / * * Note the above modality is false, why is it if it is True, the listener adds to the Button * is invalid? * /// The getButton () method can be Here with Button's listener required, ProjectDialogbutton = Project.getButton (); // Generates a buttonhandler buttonHandler buttonHandler buttonHandler ButtonHandler = New ButtonHandler (); / * * Give this button, this, though Button is the components in that class, and * can still operate the components in this class :) * / ProjectDialogbutton.addActionListener (buttonhandler);}});} private class buttonhandler Implements onListener {public actionPerformed (ActionEvent event) {projectDiaog.dispose (); // Close String fileName = projectDialog.getFileName (); File createFile = new File (fileName); creatFile.mkDir (); // create a new folder DefaultMutableTreeNode NewNode = new default (filename); treemode.add (newnot, root, root.getchildcount); // The Tree here makes the corresponding change // complete task :)}}}

转载请注明原文地址:https://www.9cbs.com/read-52260.html

New Post(0)