All pastes #620455 Raw Edit

Hunter_wow

public java v1 · immutable
·published 2007-07-15 14:31 UTC
rendered paste body
// Program: When pushing the JButton the user input a text and JButtons title change to that text. This text should be save when main is activated another time.import javax.swing.*;import java.awt.event.*;import java.util.prefs.*;public class Storing extends JFrame{    private JButton button;    private Preferences root; // Should store the text on JButton    private String text = "Standard"; // Text on JButton    // Constructor    public Storing(){                // root.put("key",text);  <- Thought NullPointerException was becouse "key" dident exist. But that wasent the case                // Trying to put "text" to the users input        // >>>>>>>>>>>>>>>>>  This gives NullPointerException: null  <<<<<<<<<<<<<<<<        text = root.get("key","Standard"); // API: get(String key, String def)                 // Creating and adding button        button = new JButton(text);        ActionListener change = new Change();        button.addActionListener(change);        add(button);                // JFrame settings        setSize(200,200);        setVisible(true);    }        // Listener for the "button"    public class Change implements ActionListener{        public void actionPerformed(ActionEvent e){                        // Change the text on the button to the input from user            text = JOptionPane.showInputDialog("New key setting: ",text);            button.setText(text);                        // Trying to add the input to the "Preferences"            root.put("key",text);        }    }        // Run program    public static void main(String[] args){        new Storing();    }}