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?
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?
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"
}
]
}
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.
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...
}