0

I ask for a help please , I'm still very new to programming. I just create a simple JFrame in NeatBeans with some Look And Feel's /Themes available using the JComboBox for the user to choose the LAF/theme what he wants, and also I put the "Save" button to Action , so far I can change the "Theme" perfectly , the problem is that the " LAF / Theme " just chosen is " save " at the moment of execution, when closing and perform the JFrame again the LAF chosen remains unsaved , I would like to know how to create a method to "Save " and Keep the " LAF / theme " chosen . Can someone help me? Thank you very much in advance!

Here's the example:

IMAGE

Here is the button Save Code

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        //Button Save Action
       String t = (String)jComboBoxLAF.getSelectedItem();

        JOptionPane.showMessageDialog(null, t);

        try {

            if ("Metal".equals(t)){

                UIManager.setLookAndFeel(new MetalLookAndFeel());
                this.setVisible(false);
                new TelaJtable().setVisible(true);

            }else if("Nimbus".equals(t)){
                UIManager.setLookAndFeel(new NimbusLookAndFeel());
                this.setVisible(false);
                new TelaJtable().setVisible(true);

            }else if("CDE/Motif".equals(t)){
                UIManager.setLookAndFeel(new MotifLookAndFeel());
                this.setVisible(false);
                new TelaJtable().setVisible(true);

            }else if("Windows".equals(t)){
                UIManager.setLookAndFeel(new WindowsLookAndFeel());
                this.setVisible(false);
                new TelaJtable().setVisible(true);
            }
            else if("Windows Classic".equals(t)){
                UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
                this.setVisible(false);
                new TelaJtable().setVisible(true);
            }

    } catch (Exception e) {

    }

}                                       
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • 1
    Store the `String` representation of the fully qualified class name of the PLAF. Restore it at start-up using [`UIManager.setLookAndFeel(String)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html#setLookAndFeel-java.lang.String-). More generally: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) The way that code snippet is setting PLAFs looks very suspect, we should only deal with the *installed* PLAFs, not every one we've ever heard of! That code will fail on both OS X & *nix. – Andrew Thompson Aug 31 '16 at 08:03
  • 1
    3) `} catch (Exception e) { }` Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 4) The screenshot says `Mindows Classic`. Note there is no `M` in `Windows`. ;) – Andrew Thompson Aug 31 '16 at 08:05
  • 1
    5) `this.setVisible(false); new TelaJtable().setVisible(true);` Instead use [`SwingUtilities.updateComponentTreeUI(Component)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#updateComponentTreeUI-java.awt.Component-). – Andrew Thompson Aug 31 '16 at 08:11
  • 1
    1) "*i would like to know how to create a method to "Save " and Keep the " LAF / theme " chose*" - So I think your question is not how to set it but how to *store* the selected LaF on the system? If so, I think this is too broad and it has probably already been asked before. 2) Additionally to @AndrewThompson 's second point: Use `UIManager.getInstalledLookAndFeels();` to get the info of all available LaFs on the current system. – Lukas Rotter Aug 31 '16 at 08:19
  • Yes Lucas thank you for your compression , I failed the question, That's it what I want to do, store the LAF selected in the system ! ( I have done the configuration ) how can I create such method ? – Américo Jorge Aug 31 '16 at 08:57
  • 1
    @AméricoJorge There are many ways. I'll list a few examples: 1) The [Preferences](https://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html) class, which stores data into a system-dependent backing store. Easy to use for small amounts of data 2) A `.dat` file in which you write the information about objects using `ObjectOutputStream` and `writeObject`. To load the content of file, use an `ObjectInputStream` and `readObject`. 3) XML 4) Dozens of other ways I cannot think of now :P – Lukas Rotter Aug 31 '16 at 09:15
  • 1
    Off-topic advice: Correct me if I'm wrong, but it looks like you're using `null`-layout and `setBounds` to layout the components. Please consider using [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). It will be easier to maintain the code, the GUI and make it work on any window size. – Lukas Rotter Aug 31 '16 at 09:23
  • 1
    An example of @LukasRotter's `Preferences` suggestion is seen [here](http://stackoverflow.com/a/34616583/230513). For more specific guidance, please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 31 '16 at 10:05

0 Answers0