posto il codice del Jtree del quale sono dubbioso..
codice:
package mia;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import java.net.URL;
import java.io.IOException;
import java.awt.Dimension;
import java.awt.GridLayout;
public class TreeDemo extends JPanel
implements TreeSelectionListener {
private static final long serialVersionUID = 1L;
private JEditorPane htmlPane;
private JTree tree;
private URL helpURL;
private static boolean DEBUG = false;
private Utility madre;
public TreeDemo( Utility madre) {
super(new GridLayout(1,0));
this.madre=madre;
//Create the nodes.
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("Plugin");
createNodes(top);
//Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Listen for when the selection changes.
tree.addTreeSelectionListener(this);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent evt) {
TreePath[] paths = evt.getPaths();
String selezionato=null;
for (int i = 0; i < paths.length; i++) {
selezionato=paths[i].getPathComponent(paths[i].getPathCount()-1)+"";
if (evt.isAddedPath(i)) {
System.out.println("This node has been selected "+selezionato);
vai(selezionato);
} else {
System.out.println("This node has been deselected "+selezionato);
}
}
}
});
//Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
//Create the HTML viewing pane.
htmlPane = new JEditorPane();
htmlPane.setEditable(false);
initHelp();
JScrollPane htmlView = new JScrollPane(htmlPane);
//Add the scroll panes to a split pane.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(htmlView);
Dimension minimumSize = new Dimension(100, 50);
htmlView.setMinimumSize(minimumSize);
treeView.setMinimumSize(minimumSize);
splitPane.setDividerLocation(150);
splitPane.setPreferredSize(new Dimension(500, 300));
//Add the split pane to this panel.
add(splitPane);
}
private void vai(String selezionato){
madre.cambiaLook(selezionato);
}
/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
BookInfo book = (BookInfo)nodeInfo;
displayURL(book.bookURL);
if (DEBUG) {
System.out.print(book.bookURL + ": \n ");
}
} else {
displayURL(helpURL);
}
if (DEBUG) {
System.out.println(nodeInfo.toString());
}
}
private class BookInfo {
public String bookName;
public URL bookURL;
public BookInfo(String book, String filename) {
bookName = book;
bookURL = getClass().getResource(filename);
if (bookURL == null) {
System.err.println("Couldn't find file: "
+ filename);
}
}
public String toString() {
return bookName;
}
}
private void initHelp() {
String s = "TreeDemoHelp.html";
helpURL = getClass().getResource(s);
if (helpURL == null) {
System.err.println("Couldn't open help file: " + s);
} else if (DEBUG) {
System.out.println("Help URL is " + helpURL);
}
displayURL(helpURL);
}
private void displayURL(URL url) {
try {
if (url != null) {
htmlPane.setPage(url);
} else { //null url
htmlPane.setText("File Not Found");
if (DEBUG) {
System.out.println("Attempted to display a null URL.");
}
}
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
}
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode category2 = null;
DefaultMutableTreeNode book = null;
category = new DefaultMutableTreeNode("Tema");
top.add(category);
book = new DefaultMutableTreeNode(new BookInfo
("Sistem look and feel",
"Sistem look and feel.html"));
category.add(book);
// cartella
category2 = new DefaultMutableTreeNode ("Default java");
book = new DefaultMutableTreeNode(new BookInfo
("Metal",
"Metal.html"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("Nimbus",
"Nimbus.html"));
category2.add(book);
category.add(category2);
book = new DefaultMutableTreeNode(new BookInfo
("Substance",
"Substance.html"));
category.add(book);
// cartella
category2 = new DefaultMutableTreeNode ("Skinfl");
book = new DefaultMutableTreeNode(new BookInfo
("aquatheme",
"aquatheme.html"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("cellshadedtheme",
"cellshadedtheme"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("macostheme",
"macostheme.html"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("moderntheme",
"moderntheme.html"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("theme",
"theme.html"));
category2.add(book);
book = new DefaultMutableTreeNode(new BookInfo
("toxictheme",
"toxictheme.html"));
category2.add(book);
category.add(category2);
}
questo invece è una parte del frame principale che riceve la chiamata e aggiorna la GUI
codice:
//campi interessati
public final String[] defaultJava= {"Metal","Nimbus"};
public final String[] skinfl={"aquatheme","cellshadedtheme","macostheme","moderntheme","theme","toxictheme"};
public final String[] substance={"Substance"};
public final String[] sistemLookAndFeel={"Sistem look and feel"};
// metodo chiamato
public void cambiaLook(String nome){
boolean d=false;
if(nome==null) return;
if (cerca(nome,defaultJava)){
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (nome.equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
}
else if (cerca(nome,skinfl)){
// first tell SkinLF which theme to use
Skin theSkinToUse = null;
try {
theSkinToUse = SkinLookAndFeel.loadThemePack("****/utility/look&fill/skinf's theme/"+nome+"pack.zip");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SkinLookAndFeel.setSkin(theSkinToUse);
// finally set the Skin Look And Feel
try {
UIManager.setLookAndFeel(new SkinLookAndFeel());
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (cerca(nome,sistemLookAndFeel)){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use system look and feel.");
}
}
else if (cerca(nome,substance)){
try {
UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
// UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Substance Raven Graphite failed to initialize");
}
}
else{ System.out.println("errore impossibile trovare il tema");
d=true;
}
if(!d){
aggiornaTutteGui();
}
catch(Exception e){
System.out.println("errore");
e.printStackTrace();
}
}
}
private boolean cerca(String nome,String[] dove){
for(String e:dove){
if(nome.equals(e)){
return true;
}
}
return false;
}
private void aggiornaTutteGui(){
try{
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){
System.out.println("beccato");
}
}