-1

How do I change jButton text and save it when Java application is closed?

I would like to change the name of a jButton at runtime, then save these new values for future use, is it possible?

Thank you

AcDSa
  • 1
  • Use Java properties as per this [related question](http://stackoverflow.com/questions/1318347/how-to-use-java-property-files). – Hovercraft Full Of Eels Mar 21 '15 at 12:46
  • thank you! i'm reading on that now, seems exactly what I want to do – AcDSa Mar 21 '15 at 12:54
  • @HovercraftFullOfEels i was thinking to store the name of the button in a textfile and read that to set the name of the button. Probably another button with a textfield to enter and submit a new name and overwrite the existing name in the file and set the new name. – WonderWorld Mar 21 '15 at 12:54
  • Hi just want to say thank you, after countless attempts, I've managed to get it working now! Just the perfect thing I was looking for – AcDSa Mar 22 '15 at 02:22

1 Answers1

0

You Could try Serialize the button object after you click it and then insert code (in the main method) to initialize the button from the serialized file. I modified some code to illustrate this. Hope it helps!

import javax.swing.*;
import java.awt.*;

import java.io.*;
import java.nio.file.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class ButtonFrame extends JFrame 
{
    public static JButton bChange ; //button that appears in window
    public static JTextField bText ; //text field that appears in window

    // constructor for ButtonFrame
    ButtonFrame(String title, JButton button) 
    {
        super( title );                     // invoke the JFrame constructor
        setLayout( new FlowLayout() );      // set the layout manager

        //initialize textfield
        bText = new JTextField(40);
        bText.setText("place new button name here and click button");
        add(bText);

        //initialize button in window with text from file
        bChange = new JButton(button.getText()); 
        add( bChange );            

        //add event listener to button to change text of button in window
        bChange.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                bChange.setText(bText.getText());


                //save the new button text to file
                try {
                    FileOutputStream fileOut =
                            new FileOutputStream("button.ser");
                    ObjectOutputStream out = new ObjectOutputStream(fileOut);
                    out.writeObject(bChange);
                    out.close();
                    fileOut.close();
                } catch(Exception ex){

                }
            }
        });
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    }
}

public class ButtonDemo 
{
public static JButton jB = null;

    public static void main ( String[] args )
    {
        try
        {
            //check serialized file exists
            Path path = Paths.get("button.ser");

            if (Files.notExists(path)) {
                //create file
                FileOutputStream fileOut =
                    new FileOutputStream("button.ser");
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(new JButton("Click Me!"));
                out.close();
                fileOut.close();
            }

            //initailize button from file 
            FileInputStream fileIn = new FileInputStream("button.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            jB = (JButton) in.readObject();
            in.close();
            fileIn.close();
        }catch(Exception i)
        {
        }
        ButtonFrame frm = new ButtonFrame("Button Demo", jB);

        frm.setSize( 400, 200 );     
        frm.setVisible( true );   
    }
}
Danoram
  • 8,132
  • 12
  • 51
  • 71