You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.1 KiB
124 lines
4.1 KiB
/* |
|
* To change this license header, choose License Headers in Project Properties. |
|
* To change this template file, choose Tools | Templates |
|
* and open the template in the editor. |
|
*/ |
|
package propertiesswing; |
|
|
|
import java.awt.Container; |
|
import java.awt.FlowLayout; |
|
import java.awt.GridLayout; |
|
import java.awt.Label; |
|
import java.io.File; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileReader; |
|
import java.io.FileWriter; |
|
import java.io.IOException; |
|
import java.util.Properties; |
|
import java.util.logging.Level; |
|
import java.util.logging.Logger; |
|
import javax.swing.JButton; |
|
import javax.swing.JFileChooser; |
|
import javax.swing.JFrame; |
|
import javax.swing.JLabel; |
|
import javax.swing.JPanel; |
|
import javax.swing.JTextField; |
|
|
|
/** |
|
* |
|
* @author denis |
|
*/ |
|
public class MainFrame extends JFrame{ |
|
|
|
private File file; |
|
private Properties properties; |
|
private JTextField currentField = new JTextField(50); |
|
private JTextField powerField = new JTextField(50); |
|
private JTextField frecField = new JTextField(50); |
|
|
|
public MainFrame(){ |
|
setTitle("Propetries"); |
|
setSize(600, 600); |
|
Container container = getContentPane(); |
|
GridLayout layout = new GridLayout(0, 1); |
|
container.setLayout(layout); |
|
container.add(initManageButtons()); |
|
container.add(initFields("Current: ", currentField)); |
|
container.add(initFields("Power: ", powerField)); |
|
container.add(initFields("Frec: ", frecField)); |
|
|
|
setLocationRelativeTo(null); |
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
|
pack(); |
|
setVisible(true); |
|
} |
|
|
|
private JPanel initFields(String textLabel, JTextField field){ |
|
JPanel panel = new JPanel(new FlowLayout()); |
|
JLabel label = new JLabel(textLabel); |
|
panel.add(label); |
|
panel.add(field); |
|
return panel; |
|
} |
|
|
|
private void initFileChooser(){ |
|
JFileChooser chooser = new JFileChooser(); |
|
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
|
int result = chooser.showOpenDialog(this); |
|
if(result == JFileChooser.APPROVE_OPTION){ |
|
file = chooser.getSelectedFile(); |
|
} |
|
} |
|
|
|
private JPanel initManageButtons(){ |
|
JPanel panel = new JPanel(new FlowLayout(10, 20, 20)); |
|
JButton openButton = new JButton("Open file"); |
|
panel.add(openButton); |
|
openButton.addActionListener(e -> { |
|
initFileChooser(); |
|
if(file!=null && file.exists() && file.canRead()){ |
|
loadProperties(); |
|
if(properties!=null) { |
|
if(currentField!=null) currentField.setText(properties.getProperty("Current")); |
|
if(powerField!=null) powerField.setText(properties.getProperty("Power")); |
|
if(frecField!=null) frecField.setText(properties.getProperty("Frec")); |
|
} |
|
} |
|
}); |
|
JButton saveButton = new JButton("Save file"); |
|
panel.add(saveButton); |
|
saveButton.addActionListener(e -> { |
|
if(file!=null && properties!=null){ |
|
properties.put("Current", currentField.getText()); |
|
properties.put("Power", powerField.getText()); |
|
properties.put("Frec", frecField.getText()); |
|
saveProperties(); |
|
} |
|
}); |
|
return panel; |
|
} |
|
|
|
|
|
|
|
private void loadProperties(){ |
|
properties = new Properties(); |
|
try { |
|
properties.load(new FileReader(file)); |
|
} catch (FileNotFoundException ex) { |
|
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
|
} catch (IOException ex) { |
|
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
|
} |
|
} |
|
|
|
private void saveProperties(){ |
|
if(properties!=null){ |
|
try { |
|
properties.store(new FileWriter(file), null); |
|
} catch (IOException ex) { |
|
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
|
} |
|
} |
|
} |
|
|
|
}
|
|
|