JScrollPane macht mir zu schaffen

MScalli

Erfahrenes Mitglied
Hi leutz.
ich habe mittels JDesktopPane und JInternalPane ne art Desktop erzeugt.
dort rufe ich jetzt ein JSPlitPane auf, dort soll auf dem JPanel links eine art explorer erzeugt werden
da dieser aber grösser werden kann soll er auf nem JScrollPane liegen.
Ohne die JScrollPane gehts auch einwandfrei.
Ich poste mal meine versuche OHNE die JSrollPane denn da hab ich solche Probleme.. das sieht man erst wenn man drauf klickt.
Ich würde auch am liebsten auf setLayout(null) verzichten.. doch dann sieht man gar nichts.
kann sich das mal wer anschaun?

also die Klasse WindowInWindow ruft Verwaltung auf

Code:
// holt sich breite und höhe des JInternalFrame merkt sich maximum der Range für das JInternalFrame
int begin_width 	= desktop.getWidth() - Verwaltung.getWidthFrame();
int begin_height 	= desktop.getHeight() - Verwaltung.getHeightFrame();
new Verwaltung("Verwaltung" , desktop, begin_width, begin_height);

diese setzt dann das JInternalFrame durch Zufallsgenerator in den "desktop"
hier hab ich es mit dem JScrollPane auch versucht :(

Code:
import static java.lang.Math.random;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class  Verwaltung{
	private static final long serialVersionUID = 3001190420235484550L;
	static JInternalFrame frame;
	static int frameWidth = 800;
	static int frameHeight = 600;
	public Verwaltung(String title, Container desktop, int _x, int _y) {
		// Frame-Initialisierung
		frame = new JInternalFrame();

		frame.setSize(frameWidth, frameHeight);
		int x = _x;
		int y = _y ;
		frame.setLocation((int) (random() * x), (int)(random() * y));	
		frame.setIconifiable(true);
		frame.setMaximizable(true);
		frame.setFrameIcon(new ImageIcon(MyParam.getPathPics() + "\\klein_App1.png"));
		frame.setBorder(BorderFactory.createLineBorder(new Color(111,111,111), 2));
		frame.setClosable(true);
		frame.setTitle("Verwaltung");
		
		Container cp = frame.getContentPane();
// ******************************************************************************************************************************		
		JSplitPane splitpane=new JSplitPane();
		cp.add(splitpane);
		
		JPanel left = new JPanel();
		left.setMinimumSize(new Dimension(200,200));
		left.setLayout(null); 
		
		JPanel right = new JPanel();

		left.add(new DirTree());
		
	    splitpane.add(left,JSplitPane.LEFT);
	    splitpane.add(right,JSplitPane.RIGHT);

// ******************************************************************************************************************************
		frame.setResizable(false);
		frame.setVisible(true);
		desktop.add(frame);
	}
	public static JInternalFrame getInternalFrame(){
		return frame;
	}
	public static int getHeightFrame(){
		return frameHeight;
	}
	public static int getWidthFrame(){
		return frameWidth;
	}
}// ENDE

auf JPanel left soll jetzt das JPanel DirTree also der Explorer

Code:
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public class DirTree extends JPanel{
    public static final String APP_NAME = "Directories Tree";
    public static final ImageIcon ICON_DOCBOOK = new ImageIcon("k:\\book.png");
    public static final ImageIcon ICON_DISK = new ImageIcon("k:\\folders.png");
    public static final ImageIcon ICON_FOLDER = new ImageIcon("k:\\folder_closed.png");
    public static final ImageIcon ICON_EXPANDEDFOLDER = new ImageIcon("k:\\\\folder_open.png");
    protected JTree  m_tree;
    protected DefaultTreeModel m_model;
  
    public DirTree() {
    	this.setLayout(null);
        this.setSize(400, 700);
        
        DefaultMutableTreeNode top = new DefaultMutableTreeNode( new IconData(ICON_DOCBOOK, null, "DokBook"));

        DefaultMutableTreeNode node;

        // Hier der Ordner wo ich "beginne" Name = DocBook
        File dir = new File("k:\\DocBook");
        File[] roots = dir.listFiles();
        
        for (int k=0; k<roots.length; k++) {
            node = new DefaultMutableTreeNode(new IconData(ICON_DISK, null, new FileNode(roots[k])));
            top.add(node);
            node.add( new DefaultMutableTreeNode(new Boolean(true)));
        }
        m_model = new DefaultTreeModel(top);
        m_tree = new JTree(m_model);
        m_tree.putClientProperty("JTree.lineStyle", "Angled");
        IconCellRenderer renderer = new IconCellRenderer();
        m_tree.setCellRenderer(renderer);
        m_tree.addTreeExpansionListener(new DirExpansionListener());
        m_tree.addTreeSelectionListener(new DirSelectionListener());
        m_tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION);
        m_tree.setShowsRootHandles(true);
        m_tree.setEditable(false);

        m_tree.setBounds(0, 0, 400, 700);
        
        this.add(m_tree);
        this.setVisible(true);
    }
    DefaultMutableTreeNode getTreeNode(TreePath path) {
        return (DefaultMutableTreeNode)(path.getLastPathComponent());
    }
    FileNode getFileNode(DefaultMutableTreeNode node) {
        if (node == null)
            return null;
        Object obj = node.getUserObject();
        if (obj instanceof IconData)
            obj = ((IconData)obj).getObject();
        if (obj instanceof FileNode)
            return (FileNode)obj;
        else
            return null;
    }
    // Make sure expansion is threaded and updating the tree model
    // only occurs within the event dispatching thread.
    class DirExpansionListener implements TreeExpansionListener {
        public void treeExpanded(TreeExpansionEvent event) {
            final DefaultMutableTreeNode node = getTreeNode(
                event.getPath());
            final FileNode fnode = getFileNode(node);
            Thread runner = new Thread() {
                public void run() {
                    if (fnode != null && fnode.expand(node)) {
                        Runnable runnable = new Runnable() {
                            public void run() {
                                m_model.reload(node);
                            }
                        };
                        SwingUtilities.invokeLater(runnable);
                    }
                }
            };
            runner.start();
        }
        public void treeCollapsed(TreeExpansionEvent event) {}
    }
    class DirSelectionListener
        implements TreeSelectionListener {
        public void valueChanged(TreeSelectionEvent event) {
            DefaultMutableTreeNode node = getTreeNode(
                event.getPath());
            FileNode fnode = getFileNode(node);
        }
    }

}
class IconCellRenderer
    extends    DefaultTreeCellRenderer {
    public IconCellRenderer() {
        setLeafIcon(null);
        setOpenIcon(null);
    }
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
        int row, boolean hasFocus) {
        // Invoke default implementation
        Component result = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        Object obj = node.getUserObject();
        setText(obj.toString());
        if (obj instanceof Boolean)
            setText("Retrieving data...");
        if (obj instanceof IconData) {
            IconData idata = (IconData)obj;
            if (expanded)
                setIcon(idata.getExpandedIcon());
            else
                setIcon(idata.getIcon());
        }
        else
            setIcon(null);
        return result;
    }
}
class IconData {
    protected Icon   m_icon;
    protected Icon   m_expandedIcon;
    protected Object m_data;
    public IconData(Icon icon, Object data) {
        m_icon = icon;
        m_expandedIcon = null;
        m_data = data;
    }
    public IconData(Icon icon, Icon expandedIcon, Object data) {
        m_icon = icon;
        m_expandedIcon = expandedIcon;
        m_data = data;
    }
    public Icon getIcon() {
        return m_icon;
    }
    public Icon getExpandedIcon() {
        return m_expandedIcon!=null ? m_expandedIcon : m_icon;
    }
    public Object getObject() {
        return m_data;
    }
    public String toString() {
        return m_data.toString();
    }
}
class FileNode {
    protected File m_file;
    public FileNode(File file) {
        m_file = file;
    }
    public File getFile() {
        return m_file;
    }
    public String toString() {
        return m_file.getName().length() > 0 ? m_file.getName() :
            m_file.getPath();
    }
    // Alternatively we copud sub-class TreeNode
    public boolean expand(DefaultMutableTreeNode parent) {
        DefaultMutableTreeNode flag =
            (DefaultMutableTreeNode)parent.getFirstChild();
        if (flag==null)   // No flag
            return false;
        Object obj = flag.getUserObject();
        if (!(obj instanceof Boolean))
            return false;      // Already expanded
        parent.removeAllChildren();  // Remove Flag
        File[] files = listFiles();
        if (files == null)
            return true;
        Vector v = new Vector();
        for (int k=0; k<files.length; k++) {
            File f = files[k];
            if (!(f.isDirectory())){
				System.out.println(f);
                continue;
			}
            FileNode newNode = new FileNode(f);
            boolean isAdded = false;
            for (int i=0; i<v.size(); i++) {
                FileNode nd = (FileNode)v.elementAt(i);
                if (newNode.compareTo(nd) < 0) {
                    v.insertElementAt(newNode, i);
                    isAdded = true;
                    break;
                }
            }
            if (!isAdded)
                v.addElement(newNode);
        }
        for (int i=0; i<v.size(); i++) {
            FileNode nd = (FileNode)v.elementAt(i);
            IconData idata = new IconData(DirTree.ICON_FOLDER,
                DirTree.ICON_EXPANDEDFOLDER, nd);
            DefaultMutableTreeNode node = new
                DefaultMutableTreeNode(idata);
            parent.add(node);
            if (nd.hasSubDirs())
                node.add(new DefaultMutableTreeNode(
                    new Boolean(true) ));
        }
        return true;
    }
    public boolean hasSubDirs() {
        File[] files = listFiles();
        if (files == null)
            return false;
        for (int k=0; k<files.length; k++) {
            if (files[k].isDirectory())
                return true;
        }
        return false;
    }
    public int compareTo(FileNode toCompare) {
        return  m_file.getName().compareToIgnoreCase(
            toCompare.m_file.getName() );
    }
    protected File[] listFiles() {
        if (!m_file.isDirectory())
            return null;
        try {
            return m_file.listFiles();
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                "Error reading directory "+m_file.getAbsolutePath(),
                DirTree.APP_NAME, JOptionPane.WARNING_MESSAGE);
            return null;
        }
    }
}

Meine Frage:
Wie bekomme ich jetzt den explorer auf ne JSrollPane, evtl. ohne den LayoutManager auszuschalten(muss aber net sein ;))
 
Zurück