So basicly I’m a little confused here, I’m using WindowsPro Builder plugin for eclipse, and it makes all the JFrame components in a custom initialize () class. This creates a question for me, normally I define my components in the beginning so I can access them publicly trough my program. No I have a second class, but i cant access my components. For example I cant figure out how to make a unified ActionListener for the whole initialize class.
I also want to get the input from the textarea, but how can I do so? When everything is outside the scope? As you can se i call the class SaveToFile, in that class i want to get the input from the textarea, but how would i do this?
import javax.swing.*;
public class FunctionsGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FunctionsGUI window = new FunctionsGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FunctionsGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize () {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
frame = new JFrame();
frame.setBounds(100, 100, 571, 531);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);
JTextPane textPane = new JTextPane();
springLayout.putConstraint(SpringLayout.NORTH, textPane, 10, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, textPane, 10, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textPane, 462, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textPane, 545, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(textPane);
frame.setLocationRelativeTo(null);
frame.setTitle("Calcolo");
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
final JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SaveToFile sv = new SaveToFile();
}
});
mnFile.add(mntmSave);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
final JMenuItem AboutMenu = new JMenuItem("About");
AboutMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(AboutMenu)) {
JDialog dialog = new JDialog();
dialog.setTitle("Search Dialog");
dialog.getContentPane().add(new JLabel("Just a test"));
dialog.setSize(300,300);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
if (e.getSource().equals(mntmSave));
SaveToFile sv = new SaveToFile ();
}
}
});
mnHelp.add(AboutMenu);
}
}
I would like to show you a way. Have a constructor in SaveToFile class which can accept String as below. Pass the text from you textpane to this constructor or method as String. Or even a method is good. But have any one of these two.
Now change your listener to like like below,
If you don’t want a constructor then,
What I am doing is I’m sending a text from the textpanel to the SaveToFile class. There you can use this String. Make sure that you declare JTextPane as final like below
Now you got a text from textpane whcih is in another class to SaveToFile class. As I said its just “A Way not THE way”.