This site uses cookies own and third. If you continue to browse consider to accept the use of cookies. OK More Info.

Salir y volver a iniciar igual con la misma configuracion

  • 0 Replies
  • 2328 Views

0 Members and 1 Guest are viewing this topic.

Offline ProcessKill

  • *
  • Underc0der
  • Posts: 154
  • Actividad:
    0%
  • Reputación 0
  • Arriba los hackers de Argentina!
    • View Profile
    • Email
Este programa simple pero avanzado muestra como guardar la configuracion del  programa y al salir  iniciar las misma configuracion que dejaste
consta de un button reset para borrar la configuracion.



Code: (java) You are not allowed to view links. Register or Login
//-----------------------------------------------------------
/*
@author L-EYER
@see GlassFish Tools Bundle For Eclipse
  Version: 0.9.9
*/
//----------------------------------------------------------
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//----------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//----------------------------------------------------------
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
//----------------------------------------------------------
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
//----------------------------------------------------------

import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class ConfigGuard
{
private static final long serialVersionUID = 1L;

public static final String  nameFile="configuracion.xml";
public static final File file=new File(nameFile);

public static  JButton buttonGuard;
public static  JButton    buttonReset ;
public static  JCheckBox  check1;
public static  JCheckBox  check2;
public static  JRadioButton radioButton1;
public static  JRadioButton radioButton2;

public static void main(String[] args) {

JFrame Frame = new JFrame(" Salir he iniciar igual la configuracion ");

LookAndFeelInfo[] LookAndFeel=UIManager.getInstalledLookAndFeels();
try {
UIManager.setLookAndFeel(LookAndFeel[3].getClassName() );
SwingUtilities.updateComponentTreeUI( Frame );
}catch (Exception e) {}
Frame.setLayout(new FlowLayout(FlowLayout.CENTER));

buttonGuard    =   new javax.swing.JButton("Guardar");
buttonReset    =   new javax.swing.JButton("Reset");
check1      =   new JCheckBox("Config 1",false);
check2    =   new JCheckBox("Config 2",false);
radioButton1   =   new JRadioButton("RadioButton 1");
    radioButton2   =   new JRadioButton("RadioButton 2");
 
    ConfigGuard.VerifyConfigurationSaved(Frame);
     
final Container container  =   Frame.getContentPane();
container .add(check1);    container .add(check2);
container .add(radioButton1); container .add(radioButton2);
container           .add(buttonGuard);  container           .add(buttonReset);

final listerner listerner = new listerner(Frame);
    buttonGuard         .addActionListener( listerner );
buttonReset         .addActionListener( listerner );

Frame.addWindowListener(new WindowListener() {
@Override public void windowOpened(WindowEvent e) {}
@Override public void windowIconified(WindowEvent e) {}
@Override public void windowDeactivated(WindowEvent e) {}
@Override public void windowClosing(WindowEvent e) {System.out.println("Exit");System.exit(0);}
@Override public void windowClosed(WindowEvent e) {}
    @Override public void windowActivated(WindowEvent e) {}
@Override public void windowDeiconified(WindowEvent e) {
}
   }
);
Frame.setSize(400,100);
Frame.setLocationRelativeTo(new JFrame());
Frame.setVisible(true);
}
public static void VerifyConfigurationSaved(JFrame Frame)
{if( file.exists() ){
new InitConfig( true , Frame , nameFile );}
else{
new InitConfig( false , Frame , nameFile );}
}
}
class listerner implements ActionListener
{
private JFrame GUI;

public Text _C1 = null;
public Text _C2 = null;
public Text _R1 = null;
public Text _R2 = null;

public listerner (JFrame frame){ GUI = frame; }
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("Guardar")){

final DOMImplementation impl = DOMImplementationImpl.getDOMImplementation();
    final  Document XMLdoc = impl.createDocument(null,"Configuracion",null);
   
    Element raiz = XMLdoc.getDocumentElement();
Element config=XMLdoc.createElement("Confifuracion");

final OutputFormat out = new OutputFormat(XMLdoc);
XMLSerializer serializer;

final Element _check1=XMLdoc.createElement("Check1_is_Select");
final Element _check2=XMLdoc.createElement("Check2_is_Select");
final Element _Radio1=XMLdoc.createElement("Radio1_is_Select");
final Element _Radio2=XMLdoc.createElement("Radio2_is_Select");

if(ConfigGuard.check1.isSelected()){
_C1=XMLdoc.createTextNode("true");
     _check1.appendChild(_C1);
     config.appendChild(_check1);
}else{
_C1=XMLdoc.createTextNode("false");
  _check1.appendChild(_C1);
  config.appendChild(_check1);
}
if(ConfigGuard.check2.isSelected()){
    _C2=XMLdoc.createTextNode("true");
_check2.appendChild(_C2);
config.appendChild(_check2);
}else{
_C2=XMLdoc.createTextNode("false");
_check2.appendChild(_C2);
config.appendChild(_check2);
}
if(ConfigGuard.radioButton1.isSelected()){
_R1=XMLdoc.createTextNode("true");
_Radio1.appendChild(_R1);
config.appendChild(_Radio1);
}else{
_R1=XMLdoc.createTextNode("false");
_Radio1.appendChild(_R1);
config.appendChild(_Radio1);
}
if(ConfigGuard.radioButton2.isSelected()){
_R2=XMLdoc.createTextNode("true");
_Radio2.appendChild(_R2);
     config.appendChild(_Radio2);
}else{
_R2=XMLdoc.createTextNode("false");
_Radio2.appendChild(_R2);
config.appendChild(_Radio2);
}    
raiz.appendChild(config);
    try {

    FileOutputStream fileOutputStream = new FileOutputStream("configuracion.xml",false);
    serializer     = new XMLSerializer(fileOutputStream, out);
try {
serializer.serialize(XMLdoc);

fileOutputStream.flush();
fileOutputStream.close();

ConfigGuard.buttonGuard.setEnabled(false);

} catch (IOException e1) {e1.printStackTrace();}
} catch (FileNotFoundException e1) {e1.printStackTrace();
} catch (Exception exception) {exception.printStackTrace();
}
JOptionPane.showMessageDialog(GUI,"Configuracion Cuargada Correctamente!","Guardar",JOptionPane.INFORMATION_MESSAGE);
}
if(e.getActionCommand().equalsIgnoreCase("Reset")){

if(ConfigGuard.file.exists()){
ConfigGuard.file.delete();
}else{
JOptionPane.showMessageDialog(GUI,"Configuracion Borrada!","Reset",JOptionPane.INFORMATION_MESSAGE);
}
_C1=null;_C2=null;
_R1=null;_R2=null;

ConfigGuard.buttonGuard .setEnabled(true);
ConfigGuard.check1 .setSelected(false);
ConfigGuard.check2 .setSelected(false);
ConfigGuard.radioButton1 .setSelected(false);
ConfigGuard.radioButton2 .setSelected(false);
}
}
}
class InitConfig
{
private final com.sun.org.apache.xerces.internal.parsers.DOMParser DOM
= new com.sun.org.apache.xerces.internal.parsers.DOMParser();
private JFrame parent;
public InitConfig( boolean FILEexists , JFrame frame, String file ) {
try {
parent = frame;
if( FILEexists ){

DOM.parse(file);
Document doc=DOM.getDocument();

NodeList _CK1= doc.getElementsByTagName("Check1_is_Select");
NodeList _CK2= doc.getElementsByTagName("Check2_is_Select");
NodeList _RD1= doc.getElementsByTagName("Radio1_is_Select");
NodeList _RD2= doc.getElementsByTagName("Radio2_is_Select");

final Node c1=_CK1.item(0).getFirstChild();
final Node c2=_CK2.item(0).getFirstChild();
final Node r1=_RD1.item(0).getFirstChild();
final Node r2=_RD2.item(0).getFirstChild();

if(c1.getNodeValue().equals("true"))
{ConfigGuard.check1.setSelected(true);

}else{ConfigGuard.check1.setSelected(false);
}
if(c2.getNodeValue().equals("true"))
{ConfigGuard.check2.setSelected(true);

}else{ConfigGuard.check2.setSelected(false);
}
if(r1.getNodeValue().equals("true"))
{ConfigGuard.radioButton1.setSelected(true);

}else{ConfigGuard.radioButton1.setSelected(false);
}
if(r2.getNodeValue().equals("true"))
{ConfigGuard.radioButton2.setSelected(true);

}else{ConfigGuard.radioButton2.setSelected(false);
}
}else
{
JOptionPane.showMessageDialog(parent,"No se ha Guardado Ninguna Configuracion.","Informacion",JOptionPane.INFORMATION_MESSAGE);
}
} catch (SAXException e) {e.printStackTrace();
System.exit(0);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(),"Error al Leer el archivo.","??????????????",JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
System.exit(0);
}
}
}
« Last Edit: April 18, 2013, 12:42:57 pm by Expermicid »