Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    22

    errore con updateComponentTreeUI()

    salve a tutti.
    Ho creato una interfaccia grafica in java tramite Swing. L'utente può cambiare tema cliccando su un jtree.
    applicando agli oggetti il metodo .updateUI() sembra funziona,ma con per ovvi motivi cerco di utilizzare updateComponentTreeUI().Oltre al fatto che un jdialog interno al programma non cambia d'aspetto(e forse è proprio lui il problema), mi restituisce il seguente errore in esecuzione (su eclipse). :master:
    codice:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 	
    at javax.swing.plaf.basic.BasicTreeUI.isToggleEvent(Unknown Source) 	
    at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(Unknown Source) 	
    at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source) 	
    at java.awt.Component.processMouseEvent(Unknown Source) 	
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 	
    at java.awt.Component.processEvent(Unknown Source) 	
    at java.awt.Container.processEvent(Unknown Source) 	
    at java.awt.Component.dispatchEventImpl(Unknown Source) 	
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 	
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 	
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 	
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 	
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 	
    at java.awt.Component.dispatchEvent(Unknown Source) 	
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 	
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 	
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 	
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 	
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 	
    at java.awt.EventDispatchThread.run(Unknown Source)
    per trovare il riferimento implicito null ho modificato il metodo della libreria java SwingUtilities in questo modo:
    codice:
    public static void  updateComponentTreeUI(Component c) {
    		if(c==null) return;
    		try{
            updateComponentTreeUI0(c);
            c.invalidate();
            c.validate();
            c.repaint();
    		}
            catch(Exception e){
            	System.out.println("beccato");
            }
        }
    
       
        private static void updateComponentTreeUI0(Component c) {
        	if (c==null) return;
        	try{
            if (c instanceof JComponent) {
                JComponent jc = (JComponent) c;
                jc.updateUI(); //aggiornaUI principale
                JPopupMenu jpm =jc.getComponentPopupMenu(); 
                if(jpm != null && jpm.isVisible() && jpm.getInvoker() == jc) {
                    updateComponentTreeUI(jpm);
                }
            }
           
            Component[] children = null;
            if (c instanceof JMenu) {
                children = ((JMenu)c).getMenuComponents();
            }
            else if (c instanceof Container) {
                children = ((Container)c).getComponents();
            }
            if (children != null) {
                for(int i = 0; i < children.length; i++) {
                    updateComponentTreeUI0(children[i]);
                }
            }
        	}
        	catch(Exception e){
        		System.out.println("beccato");
        	}
        }
    ma non ne esco..

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    22
    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");
    		}
    		
    	}

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.