开发者

Changing Swing progress bar look and feel

开发者 https://www.devze.com 2023-03-02 17:06 出处:网络
I keep trying to chang the UIManager to make these stupid looking green squares go away.how do i change the look and feel of this to the user?Is it system dependent?Someone else who was compiling my c

I keep trying to chang the UIManager to make these stupid looking green squares go away. how do i change the look and feel of this to the user? Is it system dependent? Someone else who was compiling my code had a constant gradient. Ideally, it would just be a solid square, as opposed to smaller blocks.

Tha开发者_如何学运维nks

Changing Swing progress bar look and feel


Have you tried setting it to the system's (user's) look and feel?

The easiest way to set the look and feel is by launching the GUI after calling:

try
{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
    // ...
}

With that said, the above appears to be the Windows XP theme and may indeed be the system (user) theme. I generally stay away from custom themes in GUIs unless there is a very good reason (e.g., customer/user requirement).

That is to say, the above code makes it system dependent, which is good because it matches the user's expecations.


"These stupid looking green squares" are element of Windows XP's Look nad Feel. If you want them to look different, you can change Look and Feel for this particular component.

Just use below workaround:

try {
    javax.swing.UIManager.setLookAndFeel(/* Look and Feel for your JProgressBar*/);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

MyProgressBar = new javax.swing.JProgressBar();

try {
    javax.swing.UIManager.setLookAndFeel(/* Previous, main Look and Feel */);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

I'm using this code for giving JFileChooser another look and it works perfectly.

You just change Look and Feel before creating component, and restoring previous one, just after that creation.


Edit Code below change the L&F of entire JFrame.

static Main m;//Reference to JFrame to be updated
static String maxOSLookAndFeel = "ch.randelshofer.quaqua.QuaquaLookAndFeel";//Package of particular L&F    
private void MacOSLFjMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MacOSLFjMenuItemActionPerformed
                    // TODO add your handling code here:
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            try {
                                UIManager.setLookAndFeel(maxOSLookAndFeel);
                                SwingUtilities.updateComponentTreeUI(m);
                                m.validate();
                            } catch (ClassNotFoundException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (InstantiationException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (IllegalAccessException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });

                }//GEN-LAST:event_MacOSLFjMenuItemActionPerformed

Secundo: In my opinion (googled a lot and by expirience) it's impossible to affect only one JComponent while you attach a new L&F. You change your L&F in entire JFrame or you can Write your own Swing Component.

Another way to achive your goal is studying java source code and find place where JProgressBar image is beeing added to component and override this method by extending JProgressBar.


A comprehensive description of Java look and feel could be found by Sun Java document, here: Java, Look and Feel Design Guidelines in pdf.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号