Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {
  2. // Acao de Botao Salvar
  3. String t = (String)jComboBoxLAF.getSelectedItem();
  4.  
  5. JOptionPane.showMessageDialog(null, t);
  6.  
  7. try {
  8.  
  9. if ("Metal".equals(t)){
  10.  
  11. UIManager.setLookAndFeel(new MetalLookAndFeel());
  12. this.setVisible(false);
  13. new TelaJtable().setVisible(true);
  14.  
  15. }else if("Nimbus".equals(t)){
  16. UIManager.setLookAndFeel(new NimbusLookAndFeel());
  17. this.setVisible(false);
  18. new TelaJtable().setVisible(true);
  19.  
  20. }else if("CDE/Motif".equals(t)){
  21. UIManager.setLookAndFeel(new MotifLookAndFeel());
  22. this.setVisible(false);
  23. new TelaJtable().setVisible(true);
  24.  
  25. }else if("Windows".equals(t)){
  26. UIManager.setLookAndFeel(new WindowsLookAndFeel());
  27. this.setVisible(false);
  28. new TelaJtable().setVisible(true);
  29. }
  30. else if("Windows Classic".equals(t)){
  31. UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
  32. this.setVisible(false);
  33. new TelaJtable().setVisible(true);
  34. }
  35.  
  36. } catch (Exception e) {
  37.  
  38. }
  39.  
  40. }
  41.  
  42. public static class Propriedade {
  43.  
  44. private static Properties prop;
  45. //aqui você pode mudar o nome e a extensão do arquivo
  46. private static String path = "config.properties";
  47.  
  48. private static void LoadPropertiesFile() throws IOException {
  49. prop = new Properties();
  50. File f = new File(path);
  51.  
  52. if (f.isFile()) {
  53. FileInputStream file = new FileInputStream(f);
  54. prop.load(file);
  55. } else {
  56. //se o arquivo não existir, cria um novo
  57. f.createNewFile();
  58. FileInputStream file = new FileInputStream(f);
  59. prop.load(file);
  60. //seta um valor inicial em branco pro parametro do LAF
  61. // isso é para evitar NullPointerException ao chamar
  62. // getLookAndFeel() logo após criar o arquivo
  63. setLookAndFeel("");
  64. }
  65. }
  66.  
  67. public static String getLookAndFeel() throws IOException {
  68. LoadPropertiesFile();
  69. return prop.getProperty("lookandfeel.name");
  70. }
  71.  
  72. public static void setLookAndFeel(String name) throws IOException {
  73. LoadPropertiesFile();
  74. prop.setProperty("lookandfeel.name", name);
  75. prop.store(new FileOutputStream(path), "");
  76. }
  77. }
  78.  
  79. import java.awt.*;
  80. import java.awt.event.*;
  81. import java.io.*;
  82. import javax.swing.*;
  83. import java.util.Properties;
  84.  
  85. /**
  86. *
  87. * @author diego.felipe
  88. */
  89. public class SaveLAFTest {
  90.  
  91. private void start() {
  92. final JFrame frame = new JFrame();
  93. frame.setTitle("Frame principal");
  94. frame.setSize(300, 200);
  95. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96. JComboBox comboLAF = new JComboBox();
  97.  
  98. for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  99. comboLAF.addItem(info.getName());
  100. }
  101.  
  102. comboLAF.addItemListener(new ItemListener() {
  103. @Override
  104. public void itemStateChanged(ItemEvent e) {
  105. if (e.getStateChange() == ItemEvent.SELECTED) {
  106. String LAFSelected = (String) e.getItem();
  107. changeLookAndFeel(LAFSelected);
  108. SwingUtilities.updateComponentTreeUI(frame);
  109. }
  110. }
  111. });
  112.  
  113. frame.setLayout(new FlowLayout());
  114. frame.add(comboLAF);
  115. frame.setVisible(true);
  116. frame.setLocationRelativeTo(null);
  117. }
  118.  
  119. private void changeLookAndFeel(String name) {
  120. for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  121. if (info.getName().equalsIgnoreCase(name)) {
  122. try {
  123. UIManager.setLookAndFeel(info.getClassName());
  124. Propriedade.setLookAndFeel(name);
  125. } catch (Exception ex) {
  126. ex.printStackTrace();
  127. }
  128. }
  129. }
  130. }
  131.  
  132. public static void main(String[] args) {
  133.  
  134. try {
  135. String myLAF = Propriedade.getLookAndFeel();
  136. if (myLAF == null || myLAF.isEmpty()) {
  137. Propriedade.setLookAndFeel(UIManager.getLookAndFeel().getName());
  138. } else {
  139.  
  140. for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  141. if (myLAF.equalsIgnoreCase(info.getName())) {
  142. UIManager.setLookAndFeel(info.getClassName());
  143. break;
  144. }
  145. }
  146. }
  147. } catch (Exception ex) {
  148. ex.printStackTrace();
  149. }
  150.  
  151. EventQueue.invokeLater(new Runnable() {
  152. @Override
  153. public void run() {
  154. SaveLAFTest f = new SaveLAFTest();
  155. f.start();
  156. }
  157. });
  158. }
  159.  
  160. public static class Propriedade {
  161.  
  162. private static Properties prop;
  163. private static String path = "config.properties";
  164.  
  165. private static void LoadPropertiesFile() throws IOException {
  166. prop = new Properties();
  167. File f = new File(path);
  168.  
  169. if (f.isFile()) {
  170. FileInputStream file = new FileInputStream(f);
  171. prop.load(file);
  172. } else {
  173. f.createNewFile();
  174. FileInputStream file = new FileInputStream(f);
  175. prop.load(file);
  176. setLookAndFeel("");
  177. }
  178. }
  179.  
  180. public static String getLookAndFeel() throws IOException {
  181. LoadPropertiesFile();
  182. return prop.getProperty("lookandfeel.name");
  183. }
  184.  
  185. public static void setLookAndFeel(String name) throws IOException {
  186. LoadPropertiesFile();
  187. prop.setProperty("lookandfeel.name", name);
  188. prop.store(new FileOutputStream(path), "");
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement