How to change the theme of a Java application in Netbeans? - java

I want to change the look of a Java application. I'm just using Netbeans and I don't like the look of metal in it, Instead I want to change it to the Windows look. Is there any way to do it?
I want to make it look like this.
But instead, when I run it, this is what the default look or theme of it is.
Is there any steps to change the default look of it?

Basically:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Will set the Look and feel to the one that your Operating System uses.
Take a look at this java tutorial which also lists the available themes and how to apply them.

Try this :
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Metal".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(YourJavaApplicationName.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(YourJavaApplicationName.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(YourJavaApplicationName.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(YourJavaApplicationName.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

Related

How to call a JFrame and set look and feel?

My program starts with a class that calls out a JFrame. On the frame, I have the LaF set up correctly, but when the main class calls it, the LaF doesn't take.
If I start only the file, it works. If I start the project, it doesn't.
The code below is under the main method on the frame.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TelaTur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaTur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaTur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaTur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
If the main class calls it:
TelaTur tela = new TelaTur();
tela.setVisible(true);
It doesn't take. Opening just the file (on Netbeans Shift + F6) takes the LaF.
I´m looking for the answer to:
How to set the look and feel from the main class?
This answer is based on what #GilianJoosen wrote on comments.
I created a method named setLookAndFeel() and called it before the initComponents() method on the constructor.
Thanks for the answers.

Adding custom Look & Feel using Netbeans

I need a bit of help. I'm experimenting with Java L&Fs, and I have absolutely no idea how to get Netbeans to actually change the look and feel. I'm using the Synthetica Blue Ice L&F, and in the coding where NetBeans has the Nimbus LF coding, I've commented out the Nimbus set and this is what I inserted (extract from original coding):
import de.javasoft.plaf.synthetica.SyntheticaBlueIceLookAndFeel;
import javax.swing.*;
import java.awt.*;
public MainMenu() {
initComponents();
try {
UIManager.addAuxiliaryLookAndFeel(new SyntheticaBlueIceLookAndFeel());
UIManager.setLookAndFeel(new SyntheticaBlueIceLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
}
Where NetBeans inserts its own Look and Feel coding, I've commented it out and it looks like this:
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
UIManager.addAuxiliaryLookAndFeel(new SyntheticaBlueIceLookAndFeel());
UIManager.setLookAndFeel(new SyntheticaBlueIceLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
// try {
// for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
// JOptionPane.showMessageDialog(null, info.getClassName());
// if ("Nimbus".equals(info.getName())) {
// javax.swing.UIManager.setLookAndFeel(info.getClassName());
// break;
// }
// }
// } catch (ClassNotFoundException ex) {
// java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (InstantiationException ex) {
// java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (javax.swing.UnsupportedLookAndFeelException ex) {
// java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// }
// }
// catch (Exception e){
// java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
// }
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainMenu().setVisible(true);
}
});
}
Yet, when I run the application, it still looks the same as the default LF. I've run a script to check and see what LFs I have installed, and this is what I got:
javax.swing.plaf.metal.MetalLookAndFeel
javax.swing.plaf.nimbus.NimbusLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
I've noticed that there's a Look and Feel tab in the design palette. Why isn't Synthetica showing there?
Seems you need to call UIManager.addAuxiliaryLookAndFeel(LookAndFeel)1 before trying to use it.
Adds a LookAndFeel to the list of auxiliary look and feels. The auxiliary look and feels tell the multiplexing look and feel what other LookAndFeel classes for a component instance are to be used in addition to the default LookAndFeel class when creating a multiplexing UI. The change will only take effect when a new UI class is created or when the default look and feel is changed on a component instance.

My JFrame appears in basic design not in advanced design in java netbeans

Recently I made a desktop application for login system, I always run the program if i changed any code to be sure if this code is working well or not, Anyways when I run the program it always appears as an advanced design, I don't know what you call it.Let the images explain.
Basic design
http://www5.0zz0.com/2013/04/10/14/418370274.jpg
Advanced design
http://www5.0zz0.com/2013/04/10/14/868362737.jpg
Your basic design is basically Metal Look And Field and Advanced Design is Nimbus Look And Field so use the following code before creating object of the JFrame class.
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
This code will change any look and field to Nimbus Look and Field

How to add a flash animation in java desktop application

In my java program I would like to add an animated logo in some of the user interfaces. I used this code but it does not work . I have added the jflashplayer jar also . sill it does not work. can some one suggest a solution ?
try {
FlashPanel fp = new FlashPanel(new File("C:\\Users\\ASHAN\\Desktop\\grandmsz.swf"));
jPanel1.add(fp);
fp.isOpaque();
fp.play();
} catch (JFlashLibraryLoadFailedException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
} catch (JFlashInvalidFlashException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}

Changing Swing progress bar look and feel

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.
Thanks
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.

Categories