0

I Write a small program that will switch among number of L&F Just choose L&F from list and button will looks different.

but not change when Second chance

and I am beginner with java :)

this my code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

int selectedIndices[] = jList1.getSelectedIndices();
try {
for (int j = 0; j < selectedIndices.length; j++){
if(j == 0){
  UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
   SwingUtilities.updateComponentTreeUI(this);
             this.pack();
}
if(j == 1){
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
 SwingUtilities.updateComponentTreeUI(this);
              this.pack();
 }
 if(j == 2){
 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
 SwingUtilities.updateComponentTreeUI(this);
             // this.pack();
}
if(j == 3){
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  SwingUtilities.updateComponentTreeUI(this);
             this.pack();
}
}
}
catch (Exception e) {
               } 
     }
skaffman
  • 398,947
  • 96
  • 818
  • 769
imalak
  • 11
  • 1
  • 1
  • 3

3 Answers3

3

If only one item is selected (I suppose it is the case), your code will always select MotifLookAndFeel:

  • selectedIndices.length is 1
  • therefore j, in your for loop, will only take 0 as a value
  • MotifLookAndFeel is selected.

You probably want to do something like this instead:

switch (jList1.getSelectedIndex()) {
    case 0:
       //select 1st L&F
       return;
    case 1:
       //select 2nd L&F
       return;
    case 2:
       //select 3rd L&F
       return;
}
assylias
  • 321,522
  • 82
  • 660
  • 783
1

There are a few issues with this code.

  1. You loop over an array for (int j = 0; j < selectedIndices.length; j++) but you never use the entries in the array selectedIndices[j]. Instead you just use j.
  2. Now you have hardcoded that when j==0 you will use MotifLookAndFeel. Normally you would use the selectedIndex to retrieve the data from the list (=a identifier for the look and feel), and use that identifier to change the look and feel
  3. It is very bad practice to just surround your code with try{} catch( Exception e ){}. For instance you catch all Exceptions, checked exceptions and runtime exceptions. Further, you do not do anything with the exception. At least put a e.printStackTrace() call in the catch block so you actually know that something went wrong
  4. In my personal experience, switching from look-and-feel goes well from the standard look-and-feel (Metal) to the System specific look-and-feel. But switching from Metal - OS specific - Nimbus - Metal - .... leads to weird results.

Just to get you going, I would write that code more like (non-compiling pseudo-code to illustrate my issues mentioned above)

//where the list is created 
//only allow single selection since I cannot set multiple L&F at the same time      
jList1.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

//button handling
int selectedIndex = jList1.getSelectedIndex();
if ( selectedIndex == -1 ) { return; } //no selection
MyLookAndFeelIdentifier identifier = jList1.getModel().getElementAt( selectedIndex );   
try{   
  UIManager.setLookAndFeel( identifier.getLookAndFeel() ); 
} catch ( UnsupportedLookAndFeelException e ){    
   e.printStackTrace(); 
}
Robin
  • 36,233
  • 5
  • 47
  • 99
  • *"OS specific - Nimbus - Metal - .... leads to weird results."* Nimbus is Notorious for introducing weird GUI artifacts. :( – Andrew Thompson Apr 26 '12 at 19:58
  • @AndrewThompson I also encounter this with other L&Fs (Motif, Kunstoff, ... ). Switching from metal to one of those and back is no problem, but switching from non-metal to another non-metal almost always results in artifacts. I have the impression JDK1.6 improved this (1.4 and 1.5 were simply a disaster), but it is still far from perfect – Robin Apr 26 '12 at 20:44
0

The book Java How To Program has a similar program example. Link1, Link2

SuperPrograman
  • 1,814
  • 17
  • 24