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.
Related
So i am trying to change the look and feel of my java gui from nimbus to windows. Its works when i launch the program on netbeans but does not work when i build it and launch the jar file.
I am using windows 10
my code:
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.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
MainFrm f = new MainFrm();
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
f.setVisible(true);
}
});
}
Thank you!
EDIT: my main project, where I had three forms. Where I changed only two to windows feel, I made all three to windows feel and it worked!. I guess this was the problem.
My Project had three JFrames, Where I changed only two JFrame to windows look and feel, after changing all three JFrames to windows look and feel, the problem was fixed.
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);
}
I have two integers; start and stop
Initially start is 0 and stop is 1. Once the user closes the window start becomes 1.
I have a method which updates my JTable;
private void Update_table(){
try{
String sql ="select * from orders ";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Table_Employee.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
I want to update the table continuously but When I put a while loop inside the void main method the program crashes;
void main;
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//Update_table();
while(start<stop)
new Employee_info().Update_table();
//<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 {
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(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Employee_info().setVisible(true);
// while(1<2)
// rh.Update_table();
// Update_table();
}
});
}
The erro;
com.mysql.jdbc.exceptions.jdbc4.MySQL SyntaxErrorException: User 12345 already has more than 'max_user_connections' active connections
Where 12345 is the user name to connect to the database, is it because I login to the database in different classes and run queries as well?
connection class;
import java.sql.*;
import javax.swing.*;
public class javaconnect {
Connection conn=null;
public static Connection ConnecrDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://XXX.com:3306/XXX_pizza","12345 ","XXXX");
// JOptionPane.showMessageDialog(null, "You got connected");
return conn;
}catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
The Employee_Info class calls to javaconnect class to make a connection;
public Employee_info() {
initComponents();
conn=javaconnect.ConnecrDb();
Update_table();
}
There are a couple things:
1) In a while loop like that, you should probably call Thread.sleep() at some point.
2) In this case, because you're not sleeping and getting a new connection every time you call the Employee_info constructor, you're creating lots of connections, that's likely the direct cause of your error.
3) You shouldn't have a business object like Employee_info make the connection. Instead, you should have a tier in between (usually called a data access layer), with something like this:
public class EmployeeDao {
public Employee_info getEmployeeInfo(){
Connection conn = getConnection();
//do something with the connection, construct employee info
return employeeInfo
}
}
4) You should use a connection pool, instead of instantiating connections by hand. Commons-dbcp is a used, erm, commonly.
5) Follow Java naming conventions.
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
I am trying to set the look and feel (LAF) of a Java applet that is used via a web browser. I wish to set the system default LAF, but when loaded in a browser, the applet returns to the Metal LAF. When I run it as a stand-alone applet, the LAF is applied correctly. The only item I am showing the user is a JFileChooser. I have tried a number of methods to overcome this including:
1) Override the applet's start() method:
#Override
public void start() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
2) Set it in the static initializer of the applet class:
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
3) Set it in the constructor of the applet:
public MyApplet() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
System.out.println("LOOK AND FEEL SET!");
}
catch (Exception ex) {
System.out.println(ex);
}
}
I am using Java 6, but targeting Java 5 on Windows. In every case, LOOK AND FEEL SET! gets printed to the console, so I know that it set it without throwing an exception. This happens irrespective of browser (using Firefox 3.6 and IE7). Why is it doing this and how can I get it to respect the LAF I designate?
I used this code in an applet I developed recently:
public void init() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
// Just accept the default L&F
}
SwingUtilities.updateComponentTreeUI(this);
super.init();
// Now add components...
}
See also Look-and-feel of an applet window changes on subsequent displays (I have not solved this problem because my applet did not need to open pop-up windows.)
So I tried finnw's answer and marked it accepted without realizing that I had also made some other modifications to my code. When I was cleaning out code I removed my mods and left finnw's, but then it was broken again.
These were the changes that had made that worked:
JFileChooser chooser = new JFileChooser();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(chooser);
}
catch (Exception ex) {
System.out.println(ex);
}
So what I ended up doing here is setting the look and feel for the file chooser outright, instead of trying to force the LAF for the whole applet. It's kind of a hack, but the file chooser is the only part of the UI that the user even sees anyway.
There does appear to be one obscure mistake that virtually every applet ever makes. Swing (also AWT components) is being used off the AWT Event Dispatch Thread (EDT). The applet threading model is a little eccentric.
This is the one time when invokeAndWait should be used with this extreme boilerplate:
#Override public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() {
initEDT();
}});
} catch (java.lang.InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (java.lang.reflect.InvocationTargetException exc) {
throw new Error(exc.getCause());
}
}