Reputation: 3
GUI Save Feature so that when the GUI is closed, when it reopens it has the same data visible. Right now the GUI works fine, and the logic segment is unfinished but that doesn't affect the problem at hand. Thanks lads.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import java.lang.Math;
public class abdul {
public static void main(String[] args) {
JFrame frame = new FutureValueFrame();
frame.setVisible(true); } }
class FutureValueFrame extends JFrame {
public FutureValueFrame() {
setTitle("Loan Calculator");
setSize(300, 300);
centerWindow(this);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new FutureValuePanel();
this.add(panel); }
private void centerWindow(Window w) {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2); } }
class FutureValuePanel extends JPanel implements ActionListener {
private JTextField paymentText, rateText, yearsText, loanText;
private JLabel paymentLabel, rateLabel, yearsLabel, loanLabel;
private JButton calculateButton, exitButton, paymentButton, loanButton;
public FutureValuePanel() { // display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout( new FlowLayout(FlowLayout.RIGHT));
loanLabel = new JLabel("Loan Amount:");
displayPanel.add(loanLabel);
//hello
loanText = new JTextField(10);
displayPanel.add(loanText);
//////
///////
rateLabel = new JLabel("Yearly Interest Rate:");
displayPanel.add(rateLabel);
rateText = new JTextField(10);
displayPanel.add(rateText);
////////
yearsLabel = new JLabel("Number of Years:");
displayPanel.add(yearsLabel);
yearsText = new JTextField(10);
displayPanel.add(yearsText);
////////
paymentLabel = new JLabel("Monthly Payment:");
displayPanel.add(paymentLabel);
//hello
paymentText = new JTextField(10);
displayPanel.add(paymentText);
// button panel
JPanel buttonPanel = new JPanel();
JPanel alphaPanel = new JPanel();
;
buttonPanel.setLayout( new FlowLayout(FlowLayout.RIGHT));
alphaPanel.setLayout( new FlowLayout(FlowLayout.RIGHT));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
buttonPanel.add(calculateButton);
paymentButton = new JButton("Monthly Payment");
paymentButton.addActionListener(this);
alphaPanel.add(paymentButton);
loanButton = new JButton("Loan Amount");
loanButton.addActionListener(this);
alphaPanel.add(loanButton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
this.add(alphaPanel, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == exitButton) {
System.exit(0);}
if (source == paymentButton){
paymentText.setEditable(false);
paymentText.setFocusable(false);
paymentText.setText(null);
loanText.setEditable(true);
loanText.setFocusable(true);
}
if (source == loanButton){
loanText.setEditable(false);
loanText.setFocusable(false);
loanText.setText(null);
paymentText.setEditable(true);
paymentText.setFocusable(true);
}
if (source == calculateButton){
NumberFormat currency = NumberFormat.getCurrencyInstance();
// paymentText.setText(currency.format(Double.parseDouble(loanText.getText())));
if()
String the = currency.format(Double.parseDouble(loanText.getText()));
paymentText.setText(the);
}
}
}
Upvotes: 0
Views: 1169
Reputation: 133
I have tried this when I made a simple java code (text) editor. I wanted to store Preferences so the highlighted words had the user-chosen color in all sessions, etc.
I used the Window Listener (the answer above me explains it fairly well) and made the preferences a xml-ish form like:
<preferences>
<fontcolor = "blue">
<fontsize = "11">
...
</preferences>
So what you can do is (in the window listener code) format the data you want to store like that:
<LastState>
<textfield1 = "textfield_1 value at last exit">
<radioButtonGroup1 = "2">
...
</LastState>
and save them in a file (e.g "lastSession.dat" or "lastSession.xml" - if you want it to be modifiable by text editor) using standard IO library (streams)
you can use something like this function to help you
String xmlForm(String tag, String data){
return "<\"+tag = \""+data+"\">";
}
In tag argument you can pass a widget String name , and in "data", obviously, its current state.
Then, every time your program starts you have to make some routine that reads that file (if such file exists) and set the values back to the widgets. (some kind of xml parser).
This method is absolutely stable and gives full control on the location of your file (that improves portability) and on the file format (YOU choose what the file contains and how you can translate it back to widget values - you can use any format you want) . Finally it is totally reusable since xml is a very standard and all-fitting code-like text format! You can attach this (almost untouched) code in other projects with similar needs!
Hope I helped!!!
Upvotes: 0
Reputation: 347194
You'll want to attach a WindowListener
to the FutureValueFrame
and monitor for the windowClosing
event.
When this occurs, you will want to write the settings you want to keep.
When the application is loaded, you would simply read these settings in and apply them to your application
Check out How to write Window Listeners for more details
As for the actual storage, you have a number of options...
You could use the Properties
API which has save
and load
functionality, but is based on a simple key/value pair API.
You could also use the Preferences
API, which has a little more functionality (store primitives), but you lose control over where the data is stored (more or less)
The choice will come down to what it is you want to achieve and the amount of work you want to go to
Upvotes: 1