0

I have a JTabbedPane which contains 3tabs and there are 2 textfields in each tab panel. User enters input/text in textfields of tab1, then goes to tab2 and enters input for textfields of that tab and ...

Is there any way to keep all this data and save all this data to somewhere like notepad by clicking a button? I mean the button exists just at the last tab, but it should take all the data in different tabs. is this possible ?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
SunnY
  • 59
  • 1
  • 4
  • 11
  • When you say 'notepad', you mean save the selected values to a text file. This is certainly possible. Just access the data from the controls & write to file. – Reimeus Sep 13 '12 at 12:14

1 Answers1

0

What you need is this?

public class MainFrame extends JFrame {
private JTabbedPane tabbedPane;
private JButton btnNewButton;
private JPanel panel1;
private JPanel panel2;
private JTextField textField1;
private JTextField textField2;
public MainFrame() {
    initGUI();

    pack();
    setVisible(true);
}
private void initGUI() {
    // WindowBuilder Work
    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setName("tabbedPane");
    getContentPane().add(tabbedPane, BorderLayout.CENTER);
    // WindowBuilder Work
    panel1 = new JPanel();
    panel1.setName("panel1");
    tabbedPane.addTab("New tab", null, panel1, null);
    // WindowBuilder Work
    textField1 = new JTextField();
    textField1.setText("");
    panel1.add(textField1);
    textField1.setColumns(10);
    // WindowBuilder Work
    panel2 = new JPanel();
    panel2.setName("panel2");
    tabbedPane.addTab("New tab", null, panel2, null);
    // WindowBuilder Work
    textField2 = new JTextField();
    textField2.setText("");
    textField2.setColumns(10);
    panel2.add(textField2);
    // WindowBuilder Work
    btnNewButton = new JButton("Save");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            handleActionPerformed(e);
        }
    });
    btnNewButton.setName("btnNewButton");
    getContentPane().add(btnNewButton, BorderLayout.NORTH);
}

/**
 * @param args
 */
public static void main(String[] args) {
    new MainFrame();
}

protected void handleActionPerformed(final ActionEvent e) {
    String value1 = textField1.getText();
    String value2 = textField2.getText();

    // write values to file
}

}

Matteo Gatto
  • 616
  • 11
  • 28
  • but you didnt assign a file/name/path . so how does this work : protected void handleActionPerformed(final ActionEvent e) { String value1 = textField1.getText(); String value2 = textField2.getText(); // write values to file it just gets the content of textfield } – SunnY Sep 13 '12 at 15:01
  • if you need the write/read file procedure read this tutorial: [tutorial](http://docs.oracle.com/javase/tutorial/essential/io/fileOps.html). The code i posted is the graphics way to generate the class. – Matteo Gatto Sep 14 '12 at 08:55