Search code examples
javaswingjlabelgridbaglayout

How can I save the color/size of a JLabel in a file?


I am trying to save the color, text and size (gridheight/gridwidth) of a JLabel in a file. So whenever the program runs I can view past JLabels.

I know how to read/write Strings onto a file but is it possible to save the state of a JLabel?


Solution

  • JLabel is a serialized object, you can save the whole JLabel object in a file using ObjectOutputStream and read it from ObjectInputStream. Like this.

    UPDATED THE PREVIOUS CODE ACCORDING TO DataObject CLASS:

    public static void main(String[] args) {
    
        // Creating a JLabel
        JLabel label = new JLabel("REHAN JAVED");
        label.setForeground(Color.RED);
        label.setSize(new Dimension(500, 500));
    
        // adding it into the file.
        addItIntoFile(new DataObject(label, 200, 50, 0, 1));
    
        // reading file..
        DataObject dataObj = readDataObject();
        JLabel newLabel = dataObj.getLabel();
        int x = dataObj.getXPosition();
        // and take all the data from getters.
    
        System.out.println(newLabel.getText()+"\n"+newLabel.getForeground()+"\n"+label.getSize());
    
    }
    
    
    public static void addItIntoFile(DataObject dataObj) {
    
        File file = new File("data.txt");
    
        try {
    
            file.createNewFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(dataObj);
            oos.close();
    
            // You can handle the different exceptions according to you. 
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    public static DataObject readDataObject() {
    
        DataObject dataObj = null;
    
        try {
    
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("data.txt")));
            dataObj = (DataObject) ois.readObject();
            ois.close();
    
            // You can handle the different exceptions according to you. 
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // Handling null data..
        if(dataObj == null) {
            dataObj = new DataObject(new JLabel(), 0, 0, 0, 0);
        }
    
        return dataObj;
    
    }
    

    It will works fine with it. :)

    UPDATED VERSION:

    To include the grid constraints like width, height, x, and y positions, create a new class and implements the Serializable interface with it and store it directly into the file.

    public class DataObject implements Serializable {
    
        private JLabel label;
        private int gridWidth;
        private int gridHeight;
        private int gridXPosition;
        private int gridYPosition;
    
        // Your Constructor..
        public DataObject(JLabel label, int gridWidth, int gridHeight,
                int gridXPosition, int gridYPosition) {
            this.label = label;
            this.gridWidth = gridWidth;
            this.gridHeight = gridHeight;
            this.gridXPosition = gridXPosition;
            this.gridYPosition = gridYPosition;
        }
    
        // your getter and setters... 
    
    }