0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Anon
  • 25
  • 6
  • Color, text and size can be represented as Strings. So, since you know how to write Strings to files, obviously you can save these states of `JLabel`s to a file. But the more important question is "why". I guess you are trying to persist some domain objects in your application. And your `JLabel`s might be representing those domain objects. But that does not mean you should look at this as "saving `JLabel` states". – Prasad Karunagoda Jan 22 '19 at 17:09

3 Answers3

0

You have to define what you need to save, and how.

What to save

You need to define what is important for you, as an example the color can be the with or without transparency informations.

The text can be a simple string or an object holding informations like character type, size, decorations (bold, underline... ).

How to save

You need to define a format for each information (for example the color can be saved with its name "red" or its hex value "#FF0000" or can be a reference to a custom variable "mainColorTemplate1"). You need also to define a format of the file (xml, json, custom binary, property file, yaml...).

I suggest to try the simplest solution for your knowledge. It is very simple to hold all the data in an object and save it as JSon using libraries like GSon or Faster Jackson.

Here is a possible format in json:

{
  "labels": [
    {
      "text":"My first label",
      "x":2,
      "y":1,
      "color":"#FF0000"
    },
    {
      "text":"My Second label",
      "x":4,
      "y":3,
      "color":"#FFFF00"
    }
  ]
} 
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

One way do it is, by utilizing object Serialization. All Swing components implement Serializable interface. You can save the component and the next time you run the application, the component will be as you left it. Two interesting questions to read about this are: Why is Java Swing serializable? and Swing components and serialization.

George Z.
  • 6,643
  • 4
  • 27
  • 47
0

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... 

}
Rehan Javed
  • 418
  • 3
  • 14
  • Yes you are right, actually i made it to give the idea, but let me edit it. :) Thanks BTW – Rehan Javed Jan 22 '19 at 17:20
  • Is it still possible with GridBagLayout? – Anon Jan 22 '19 at 17:29
  • Yes, all objects are serialized, Modify the code according to your need and give it a try. :) Please don't forget to share your remarks after trying. – Rehan Javed Jan 22 '19 at 17:30
  • @RehanJaved How do I also send the GridBagLayout constraints like gridheight and width (also x,y position) to the file also. Otherwise it works but it doesn't position or set the size correctly when reading from the file. – Anon Jan 23 '19 at 01:24
  • @Anon i updated the code for you, You just need a new class which implements Serializable interface and contains all the data which you want to save. :) Don't forget to share your remarks after trying and accept the answer if it really helps. – Rehan Javed Jan 23 '19 at 08:48
  • @RehanJaved Thank you this is exactly what I was looking for :) – Anon Jan 29 '19 at 17:38