Making several synth LookAndFeels coexist at the same time in a java swing application

0 votes

I am working on a java swing application using synth Look and Feel. There are already styles for every possible swing component

I must change the whole application's LookAndFeel, redefining different styles for every possible swing component.

I am now working on a sandbox, launched outside of the application. The sandbox loads my new set of styles, while the application still loads the old ones. No problems for now

However, I must then integrate it 'progressively' in the application. Meaning that in the same java application, some HMIs must use the old set of styles, while some must use the new ones

The difficulty being that each set of styles define synth "region" styles that automatically apply to the corresponding component, and I don't know how deal with several region styles that correspond to the same component type

Anybody has an idea of how I can do this ? I saw that in swing's UIManager, one can change the LookAndFeel, but it then changes for the whole application

Only workaround I saw on the internet was to change the LookAndFeel before instanciating a Component, then change it back, which looks like an awful solution

Thanks in advance

Apr 27, 2023 in UI UX Design by Ashwini
• 5,430 points
611 views

1 answer to this question.

0 votes

To make several Synth LookAndFeels coexist at the same time in a Java Swing application, you can create multiple instances of the SynthLookAndFeel class and assign each instance a different set of styles. Then, you can selectively apply each instance of the SynthLookAndFeel to the components that you want to style with that particular set of styles.

Here is a general approach to achieve this:

  1. Create multiple instances of SynthLookAndFeel, each with a different set of styles. For example:
SynthLookAndFeel oldLookAndFeel = new SynthLookAndFeel();
oldLookAndFeel.load(getClass().getResourceAsStream("old_styles.xml"), getClass());

SynthLookAndFeel newLookAndFeel = new SynthLookAndFeel();
newLookAndFeel.load(getClass().getResourceAsStream("new_styles.xml"), getClass());
  1. For each component that you want to style with a particular set of styles, create a new instance of SynthStyleFactory and set the appropriate LookAndFeel:
JButton oldButton = new JButton("Old button");
SynthStyleFactory oldStyleFactory = new SynthStyleFactory() {
    public SynthStyle getStyle(JComponent c, Region r) {
        return oldLookAndFeel.getStyle(c, r);
    }
};
SynthLookAndFeel.setStyleFactory(oldStyleFactory);
JButton newButton = new JButton("New button");
SynthStyleFactory newStyleFactory = new SynthStyleFactory() {
    public SynthStyle getStyle(JComponent c, Region r) {
        return newLookAndFeel.getStyle(c, r);
    }
};
SynthLookAndFeel.setStyleFactory(newStyleFactory);
  1. You can then switch between the different LookAndFeels as needed for each component:
// Apply old LookAndFeel to oldButton
SynthLookAndFeel.setStyleFactory(oldStyleFactory);
oldButton.putClientProperty("Nimbus.Overrides", oldButton.getClientProperty("Synthetica.style"));

// Apply new LookAndFeel to newButton
SynthLookAndFeel.setStyleFactory(newStyleFactory);
newButton.putClientProperty("Nimbus.Overrides", newButton.getClientProperty("Synthetica.style"));
Note that this approach may require additional modifications to handle the different sets of styles in a more fine-grained manner.

answered Apr 27, 2023 by pooja

Related Questions In UI UX Design

0 votes
1 answer

UI/UX Design in Real Time Applications

When designing for real-time applications like those ...READ MORE

answered Apr 25, 2023 in UI UX Design by vinayak
577 views
0 votes
1 answer

UI/UX design or audit - how to measure the costs?

When it comes to charging for a ...READ MORE

answered Apr 25, 2023 in UI UX Design by Tej
644 views
0 votes
1 answer
0 votes
1 answer

Outsourcing UI/UX while keeping the app code secret

If you want to outsource your UI/UX ...READ MORE

answered Apr 25, 2023 in UI UX Design by vishalini
556 views
0 votes
1 answer

Switch JPanels inside a JFrame

Instead of trying to add an remove ...READ MORE

answered Feb 14, 2019 in Java by developer_1
• 3,350 points
13,083 views
0 votes
0 answers

Track the exact location of IP address

I looked into IP addresses and a ...READ MORE

Feb 14, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
885 views
0 votes
1 answer
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,190 points
1,994 views
0 votes
1 answer

How to create Custom UITabBarController in Swift with round button in the center?

Your implementation is almost correct, you just ...READ MORE

answered Apr 27, 2023 in UI UX Design by vishalini
2,333 views
0 votes
1 answer

Is there a way to create a list that auto populates with the the required articles?

Yes, there are several ways to auto-populate ...READ MORE

answered Apr 27, 2023 in UI UX Design by Anitha
662 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP