How to change Netbeans Java (project GUI) look and feel? - java

Every time I run my Java SE(swing) program it runs with Nimbus Look and Feel, but my design is based on Windows Look and Feel, how can I make it so that my default program running has Windows look and feel and not Nimbus?
I have searched in few places, they are saying to change "Nimbus" to "Windows" which does not work for me.
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(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

Replace...
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(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
With something more like...
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
You can read How to Set the Look and Feel more details

Related

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

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);
}

Connect and Disconnect SerialModemGateway SmsLib

Need help i have no idea, how to make serial modem reconnected again when i am disconnect, this is my code connecting to serial modem :
public void connectToPort() throws GatewayException, IOException, TimeoutException, InterruptedException, SMSLibException{
try {
String port=portList.getSelectedItem().toString();
gateway = new SerialModemGateway("Modem "+port, port, 115200, "Wavecom", "M1206B");
Service.getInstance().addGateway(gateway);
gateway.setProtocol(AGateway.Protocols.PDU);
gateway.setInbound(true);
gateway.setOutbound(true);
USSDNotification ussdNotification = new USSDNotification();
Service.getInstance().setUSSDNotification(ussdNotification);
Service.getInstance().startService();
Service.getInstance().S.SERIAL_TIMEOUT=6000;
Service.getInstance().S.MASK_IMSI=false;
btnStart.setEnabled(true);
txtOutput.append("Modem status : "+gateway.getStatus().toString());
this.repaint();
this.revalidate();
// Service.getInstance().S.AT_WAIT_AFTER_RESET=2000;
// System.out.println("IMSI Smslib"+gateway.sendCustomATCommand("AT+CIMI"));
} catch (SMSLibException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
}
}
my code to disconnect modem :
try {
Service.getInstance().stopService();
Service.getInstance().removeGateway(gateway);
txtOutput.append("Modem status : "+gateway.getStatus().toString());
this.repaint();
this.revalidate();
} catch (GatewayException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (SMSLibException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
}
if i calling method connectToPort() the connection is running fine, and then i call disconect and runing fine too, but when i try to reconnect and call connectToPort() again i am getting this error :
java.io.IOException: write error
at com.sun.comm.Win32SerialPort.write(Win32SerialPort.java:677)
at com.sun.comm.Win32SerialOutputStream.write(Win32SerialOutputStream.java:38)
at org.smslib.modem.SerialModemDriver.write(SerialModemDriver.java:166)
at org.smslib.modem.AModemDriver.write(AModemDriver.java:302)
at org.smslib.modem.athandler.ATHandler_Wavecom.done(ATHandler_Wavecom.java:52)
at org.smslib.modem.ModemGateway.stopGateway(ModemGateway.java:201)
at org.smslib.Service.stopService(Service.java:355)
please help.,.what should i do??

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);
}

Java Generic Exception

I am dealing with JaudioTagger API to manipulate MP3 files, I have to repeat the following exceptions over and over again... I was thinking of having a generic exception handler where I could forward each exception maybe with a flag number and the generic method would be deal with it by having different switch cases maybe ? Is it possible ? I would really appreciate if someone could give the method signature or a way to call it
} catch (CannotReadException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (ReadOnlyFileException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAudioFrameException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (TagException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
Pre-JDK 7 all you can do is write a utility function and call it from each of the catch blocks:
private void handle(Exception ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
private void someOtherMethod() {
try {
// something that might throw
} catch (CannotReadException ex) {
handle(ex);
} catch (ReadOnlyFileException ex) {
handle(ex);
} catch (IOException ex) {
handle(ex);
} catch (InvalidAudioFrameException ex) {
handle(ex);
} catch (TagException ex) {
handle(ex);
}
}
Starting in JDK 7, you can use multi-catch:
private void someOtherMethod() {
try {
// something that might throw
} catch (CannotReadException | ReadOnlyFileException | IOException
| InvalidAudioFrameException | TagException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
See "Catching multiple exceptions".
This answer is obsolete starting with Java 7. Use multi-catch like John Watts shows in his answer.
I suggest using
try {
/* ... your code here ... */
} catch (Exception ex) {
handle(ex);
}
And handle it this way: (you have to replace the OtherException that you don't handle or remove the throws)
private static void handle(Exception ex) throws SomeOtherException {
if (ex instanceof CannotReadException || ex instanceof ReadOnlyFileException) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} else if (ex instanceof SomeOtherException) {
throw (SomeOtherException) ex;
} else if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
} else {
throw new RuntimeException(ex);
}
}

Categories