send
Grupo de Telegram
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

Salir y volver a iniciar igual con la misma configuracion

  • 0 Respuestas
  • 1406 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado ProcessKill

  • *
  • Underc0der
  • Mensajes: 154
  • Actividad:
    0%
  • Reputación 0
  • Arriba los hackers de Argentina!
    • Ver Perfil
    • Email
« en: Febrero 24, 2010, 04:13:36 pm »
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.



Código: Java
  1. //-----------------------------------------------------------
  2. /*
  3.         @author L-EYER
  4.         @see GlassFish Tools Bundle For Eclipse
  5.                                                         Version: 0.9.9
  6. */
  7. //----------------------------------------------------------
  8. import java.awt.Container;
  9. import java.awt.FlowLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.WindowEvent;
  13. import java.awt.event.WindowListener;
  14. //----------------------------------------------------------
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. //----------------------------------------------------------
  20. import javax.swing.JButton;
  21. import javax.swing.JCheckBox;
  22. import javax.swing.JFrame;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JRadioButton;
  25. import javax.swing.SwingUtilities;
  26. import javax.swing.UIManager;
  27. import javax.swing.UIManager.LookAndFeelInfo;
  28. //----------------------------------------------------------
  29. import org.w3c.dom.DOMImplementation;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.Element;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.NodeList;
  34. import org.w3c.dom.Text;
  35. import org.xml.sax.SAXException;
  36. //----------------------------------------------------------
  37.  
  38. import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl;
  39. import com.sun.org.apache.xml.internal.serialize.OutputFormat;
  40. import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
  41.  
  42. public class ConfigGuard
  43. {
  44.         private static final long serialVersionUID = 1L;
  45.        
  46.         public static final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login  nameFile="configuracion.xml";
  47.         public static final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login        file=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(nameFile);
  48.        
  49.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login          buttonGuard;
  50.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login          buttonReset ;
  51.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login        check1;
  52.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login        check2;
  53.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login radioButton1;
  54.         public  static  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login radioButton2;
  55.        
  56.         public static void main(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[] args) {
  57.  
  58.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login    = new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(" Salir he iniciar igual la configuracion ");
  59.        
  60.         LookAndFeelInfo[] You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.getInstalledLookAndFeels();
  61.         try {
  62.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.setLookAndFeel(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[3].getClassName() );
  63.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.updateComponentTreeUI( You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login );
  64.         }catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  65.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.setLayout(new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.CENTER));
  66.                
  67.         buttonGuard        =   new javax.swing.You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("Guardar");
  68.         buttonReset    =   new javax.swing.You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("Reset");
  69.         check1             =   new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("Config 1",false);
  70.         check2             =   new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("Config 2",false);
  71.         radioButton1   =   new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("RadioButton 1");
  72.     radioButton2   =   new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("RadioButton 2");
  73.  
  74.     ConfigGuard.VerifyConfigurationSaved(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login);
  75.      
  76.         final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login container  =   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.getContentPane();
  77.         container                       .add(check1);           container                       .add(check2);
  78.         container                       .add(radioButton1); container                   .add(radioButton2);
  79.         container           .add(buttonGuard);  container           .add(buttonReset);
  80.        
  81.         final listerner         listerner       = new listerner(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login);
  82.     buttonGuard         .addActionListener( listerner );
  83.         buttonReset         .addActionListener( listerner );
  84.        
  85.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.addWindowListener(new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login() {
  86.                 @Override public void windowOpened(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  87.                 @Override public void windowIconified(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  88.                 @Override public void windowDeactivated(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  89.                 @Override public void windowClosing(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.out.println("Exit");You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.exit(0);}
  90.                 @Override public void windowClosed(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  91.             @Override public void windowActivated(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {}
  92.                 @Override public void windowDeiconified(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {
  93.                 }
  94.            }
  95.          );    
  96.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.setSize(400,100);
  97.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.setLocationRelativeTo(new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login());
  98.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.setVisible(true);
  99.         }
  100.         public static void VerifyConfigurationSaved(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login)
  101.         {if( file.exists() ){
  102.                         new InitConfig( true , You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login , nameFile );}
  103.         else{
  104.                         new InitConfig( false , You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login , nameFile );}
  105.         }
  106. }
  107. class listerner implements You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
  108. {
  109.         private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login GUI;
  110.        
  111.         public  Text    _C1 =   null;
  112.         public  Text    _C2 =   null;
  113.         public  Text    _R1 =   null;
  114.         public  Text    _R2 =   null;
  115.        
  116.         public listerner (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login frame){ GUI = frame;   }
  117.         @Override
  118.         public void actionPerformed(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {
  119.                 if(e.getActionCommand().equalsIgnoreCase("Guardar")){
  120.                        
  121.                 final DOMImplementation impl    = DOMImplementationImpl.getDOMImplementation();
  122.             final  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login XMLdoc                       = impl.createDocument(null,"Configuracion",null);
  123.            
  124.             You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login raiz = XMLdoc.getDocumentElement();
  125.                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login config=XMLdoc.createElement("Confifuracion");
  126.                  
  127.                 final OutputFormat out = new OutputFormat(XMLdoc);
  128.                 XMLSerializer serializer;
  129.                  
  130.                 final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login _check1=XMLdoc.createElement("Check1_is_Select");
  131.                 final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login _check2=XMLdoc.createElement("Check2_is_Select");
  132.                 final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login _Radio1=XMLdoc.createElement("Radio1_is_Select");
  133.                 final You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login _Radio2=XMLdoc.createElement("Radio2_is_Select");
  134.                        
  135.                 if(ConfigGuard.check1.isSelected()){
  136.                 _C1=XMLdoc.createTextNode("true");
  137.                              _check1.appendChild(_C1);
  138.                              config.appendChild(_check1);
  139.                         }else{
  140.                 _C1=XMLdoc.createTextNode("false");
  141.                                   _check1.appendChild(_C1);
  142.                                   config.appendChild(_check1);
  143.                         }
  144.                 if(ConfigGuard.check2.isSelected()){
  145.             _C2=XMLdoc.createTextNode("true");
  146.                                 _check2.appendChild(_C2);
  147.                                 config.appendChild(_check2);
  148.                         }else{
  149.                 _C2=XMLdoc.createTextNode("false");
  150.                                  _check2.appendChild(_C2);
  151.                                  config.appendChild(_check2);
  152.                         }
  153.                 if(ConfigGuard.radioButton1.isSelected()){
  154.                 _R1=XMLdoc.createTextNode("true");
  155.                                 _Radio1.appendChild(_R1);
  156.                                 config.appendChild(_Radio1);
  157.                         }else{
  158.                 _R1=XMLdoc.createTextNode("false");
  159.                                 _Radio1.appendChild(_R1);
  160.                                 config.appendChild(_Radio1);
  161.                         }
  162.                 if(ConfigGuard.radioButton2.isSelected()){
  163.                                 _R2=XMLdoc.createTextNode("true");
  164.                                 _Radio2.appendChild(_R2);
  165.                              config.appendChild(_Radio2);
  166.                         }else{
  167.                                 _R2=XMLdoc.createTextNode("false");
  168.                                 _Radio2.appendChild(_R2);
  169.                                 config.appendChild(_Radio2);
  170.                         }                  
  171.                         raiz.appendChild(config);
  172.                     try {
  173.  
  174.                         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login fileOutputStream       = new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("configuracion.xml",false);
  175.                         serializer                                                  = new XMLSerializer(fileOutputStream, out);
  176.                                 try {
  177.                                         serializer.serialize(XMLdoc);
  178.                                        
  179.                                         fileOutputStream.flush();
  180.                                         fileOutputStream.close();
  181.                                        
  182.                                         ConfigGuard.buttonGuard.setEnabled(false);
  183.                
  184.                         } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e1) {e1.printStackTrace();}
  185.                         } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e1) {e1.printStackTrace();
  186.                         } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login exception) {exception.printStackTrace();
  187.                 }
  188.                         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.showMessageDialog(GUI,"Configuracion Cuargada Correctamente!","Guardar",You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.INFORMATION_MESSAGE);
  189.         }
  190.         if(e.getActionCommand().equalsIgnoreCase("Reset")){
  191.                
  192.                 if(ConfigGuard.file.exists()){
  193.                         ConfigGuard.file.delete();
  194.                 }else{
  195.                         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.showMessageDialog(GUI,"Configuracion Borrada!","Reset",You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.INFORMATION_MESSAGE);
  196.                 }
  197.                         _C1=null;_C2=null;
  198.                         _R1=null;_R2=null;
  199.        
  200.                         ConfigGuard.buttonGuard         .setEnabled(true);
  201.                         ConfigGuard.check1                      .setSelected(false);
  202.                         ConfigGuard.check2                      .setSelected(false);
  203.                         ConfigGuard.radioButton1        .setSelected(false);
  204.                         ConfigGuard.radioButton2        .setSelected(false);
  205.                 }
  206.         }
  207. }
  208. class InitConfig
  209. {
  210.         private final com.sun.org.apache.xerces.internal.parsers.DOMParser DOM
  211.         = new com.sun.org.apache.xerces.internal.parsers.DOMParser();
  212.         private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login parent;
  213.         public InitConfig( boolean FILEexists , You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login frame, You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login file ) {
  214.         try {
  215.         parent  =       frame;
  216.                 if( FILEexists ){
  217.                                        
  218.                 DOM.parse(file);
  219.                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login doc=DOM.getDocument();
  220.                                        
  221.                 NodeList _CK1= doc.getElementsByTagName("Check1_is_Select");
  222.                 NodeList _CK2= doc.getElementsByTagName("Check2_is_Select");
  223.                 NodeList _RD1= doc.getElementsByTagName("Radio1_is_Select");
  224.                 NodeList _RD2= doc.getElementsByTagName("Radio2_is_Select");
  225.                                
  226.                         final Node c1=_CK1.item(0).getFirstChild();
  227.                         final Node c2=_CK2.item(0).getFirstChild();
  228.                         final Node r1=_RD1.item(0).getFirstChild();
  229.                         final Node r2=_RD2.item(0).getFirstChild();
  230.                        
  231.                         if(c1.getNodeValue().equals("true"))
  232.                                 {ConfigGuard.check1.setSelected(true);
  233.                                
  234.                                         }else{ConfigGuard.check1.setSelected(false);
  235.                                 }
  236.                         if(c2.getNodeValue().equals("true"))
  237.                                 {ConfigGuard.check2.setSelected(true);
  238.                                
  239.                                         }else{ConfigGuard.check2.setSelected(false);
  240.                                 }
  241.                         if(r1.getNodeValue().equals("true"))
  242.                                 {ConfigGuard.radioButton1.setSelected(true);
  243.                                
  244.                                         }else{ConfigGuard.radioButton1.setSelected(false);
  245.                                 }
  246.                         if(r2.getNodeValue().equals("true"))
  247.                                 {ConfigGuard.radioButton2.setSelected(true);
  248.                                
  249.                                         }else{ConfigGuard.radioButton2.setSelected(false);
  250.                                 }
  251.                                         }else
  252.                                 {
  253.                                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.showMessageDialog(parent,"No se ha Guardado Ninguna Configuracion.","Informacion",You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.INFORMATION_MESSAGE);
  254.                                 }
  255.                         } catch (SAXException e) {e.printStackTrace();
  256.                                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.exit(0);
  257.                         } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {
  258.                                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.showMessageDialog(new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(),"Error al Leer el archivo.","??????????????",You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.INFORMATION_MESSAGE);
  259.                                 e.printStackTrace();
  260.                                 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.exit(0);
  261.                         }
  262.                 }
  263.         }
  264.  
« Última modificación: Abril 18, 2013, 12:42:57 pm por Expermicid »

 

¿Te gustó el post? COMPARTILO!