This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

help me I can't work the tree well

 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is recommended that we not mix AWT and Swing components. Your application extends Frame and uses Menus which are AWT components. If you want a JTree and JComboBox you could change everything over to Swing ("J") components.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not care whether the component is awt or swing,I just want to know
how to change the driver to display in the panel with the right tree
whose root is the content of the combobox.
thanks help
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
ddddfdffd hello world
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//I can't make the combobox work well,I want to chang the driver or another way how can I list a tree structure with all drivers in the computer 。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.File;
import javax.swing.border.*;
import java.util.*;


public class Explorer extends Frame
{
DefaultMutableTreeNode top;
JLabel picture;
JComboBox combo = new JComboBox ();
String rootvalue;
JTree tree;
JScrollPane treepane;
JPanel leftpanefortree ;
JPanel rightpanefordisp ;
JSplitPane splitPane;
public void Change (JTree atree)
{
this.tree = atree;
}
public Explorer ()
{

try
{
// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
}
catch (Exception exc)
{
System.err.println ("Error loading L&F: " + exc);
}

MenuBar mbar = new MenuBar ();
setMenuBar (mbar);

Menu file = new Menu ("File");
MenuItem item1, item2, item3, item4;

Menu subf = new Menu ("New....", true);
MenuItem item11, item12;
subf.add (item11 = new MenuItem ("Folder"));
subf.add (item12 = new MenuItem ("Text Document"));
file.add (subf);

item2 = new MenuItem ("Open");
file.add (item2);

item3 = new MenuItem ("Close");
file.add (item3);

item4 = new MenuItem ("Quit....");
//file.add(item4 = new MenuItem("Quit...."));
file.add (item4);

mbar.add (file);

Menu edit = new Menu ("Edit");
MenuItem item5, item6, item7, item8, item9;
edit.add (item5 = new MenuItem ("Undo"));
edit.add (item6 = new MenuItem ("Cut"));
edit.add (item7 = new MenuItem ("Copy"));
edit.add (item8 = new MenuItem ("Paste"));
edit.add (new MenuItem ("-"));
edit.add (item9 = new MenuItem ("Select All"));

mbar.add (edit);

Menu view = new Menu ("View");
MenuItem item20, item14, item15, item16, item17;
Menu subv = new Menu ("Explorer Bar.....", true);
MenuItem item10, item13;
subv.add (item10 = new MenuItem ("History"));
subv.add (item13 = new MenuItem ("Folders"));
view.add (subv);
view.add (new MenuItem ("-"));

view.add (item14 = new MenuItem ("Large Icons"));
view.add (item15 = new MenuItem ("Small Icons"));
view.add (item16 = new MenuItem ("List"));
view.add (item17 = new MenuItem ("Details"));
view.add (new MenuItem ("-"));

Menu subv1 = new Menu ("Arrange icons.....", true);
MenuItem item18, item19;
subv1.add (item18 = new MenuItem ("by Name"));
subv1.add (item19 = new MenuItem ("by Type"));
view.add (subv1);
view.add (new MenuItem ("-"));

view.add (item20 = new MenuItem ("Refresh"));
mbar.add (view);

Menu go = new Menu ("Go");
MenuItem item21, item22, item23;
go.add (item21 = new MenuItem ("Back"));
go.add (item22 = new MenuItem ("Forward"));
go.add (new MenuItem ("-"));
go.add (item23 = new MenuItem ("My Computer"));
mbar.add (go);

Menu tools = new Menu ("Tools");
MenuItem item24;
tools.add (item24 = new MenuItem ("Find(files/folders)"));
mbar.add (tools);

Menu help = new Menu ("Help");
MenuItem item25, item26;
help.add (item25 = new MenuItem ("Help Topic"));
mbar.add (help);

MyMenuHandler handler = new MyMenuHandler (this);


//item1.addActionListener(handler);
item2.addActionListener (handler);
item3.addActionListener (handler);
item4.addActionListener (handler);
item5.addActionListener (handler);
item6.addActionListener (handler);
item7.addActionListener (handler);
item8.addActionListener (handler);
item9.addActionListener (handler);
item10.addActionListener (handler);
item11.addActionListener (handler);
item12.addActionListener (handler);
item13.addActionListener (handler);
item14.addActionListener (handler);
item15.addActionListener (handler);
item16.addActionListener (handler);
item17.addActionListener (handler);
item18.addActionListener (handler);
item19.addActionListener (handler);
item20.addActionListener (handler);


File[]roots = File.listRoots ();




rootvalue = roots[1].toString();
tree = new JTree (createTreeModel (rootvalue));
treepane = new JScrollPane (tree);
FileNodeRenderer renderer = new FileNodeRenderer ();
AbstractBorder border = new EtchedBorder ();

tree.addTreeSelectionListener (new TreeSelectionListener ()
{
public void
valueChanged (TreeSelectionEvent e)
{
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.
getLastSelectedPathComponent ();
if (node == null) return;
System.out.println (node + "&&&&&&& " +
e.
getNewLeadSelectionPath
() + " ???" +
e.getPath ().
toString ());
String dir =
((File) node.getUserObject ()).
getAbsolutePath ();
//TreePath path = getLeadSelectionPath();
//FileNode node = (FileNode)path.getLastPathComponent();
System.out.println (dir);
ImageIcon newImage = new ImageIcon (dir);
picture.setIcon (newImage);
//picture.setText(dir);
// Object nodeInfo = node.getUserObject();
//...
/* React to the node selection. */
//...
}
}
);
tree.setEditable (true);
tree.setCellRenderer (renderer);

// getContentPane().add(treepane, BorderLayout.CENTER);

setLayout (new GridLayout (1, 2));
leftpanefortree = new JPanel ();
//add(leftpanefortree);
leftpanefortree.setBorder (border);
leftpanefortree.setLayout (new BorderLayout ());

for (int i = 0; i < roots.length; i++)
{
combo.addItem (roots[i].toString ());
}
combo.setSelectedIndex (1);
combo.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
System.out.println (e);
rootvalue = combo.getSelectedItem ().toString ();
}
}
);
leftpanefortree.add (combo, BorderLayout.NORTH);

leftpanefortree.add (treepane, BorderLayout.CENTER);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

rightpanefordisp = new JPanel ();
//add(rightpanefordisp);
rightpanefordisp.setLayout (new BorderLayout ());


// Label test=new Label(" (n_n)");
//rightpanefordisp.add(test,BorderLayout.BEFORE_FIRST_LINE);
rightpanefordisp.setBorder (border);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

ImageIcon firstImage = new ImageIcon (" ");
if (firstImage != null)
{
picture = new JLabel (firstImage);
}
else
{
picture = new JLabel (" ");
}
JScrollPane pictureScrollPane = new JScrollPane (picture);
rightpanefordisp.add (pictureScrollPane, BorderLayout.CENTER);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

splitPane =
new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, leftpanefortree, rightpanefordisp);
splitPane.setOneTouchExpandable (true);
splitPane.setDividerLocation (200);
//splitPane.setPreferredSize(new Dimension(200,100));
splitPane.setDividerSize (10);
this.add (splitPane);

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
MyWindowAdapter adapter = new MyWindowAdapter (this);
addWindowListener (adapter);


tree.addTreeExpansionListener (new TreeExpansionListener ()
{
public void
treeCollapsed (TreeExpansionEvent e)
{
}
public void
treeExpanded (TreeExpansionEvent e)
{
TreePath path = e.getPath ();
FileNode node =
(FileNode) path.getLastPathComponent ();
if (!node.isExplored ())
{
DefaultTreeModel model =
(DefaultTreeModel) tree.getModel ();
node.explore ();
model.nodeStructureChanged (node);}
}
}
);
}
private DefaultTreeModel createTreeModel (String rootstr)
{
String dirname[] =
{ "A:/..", "C:/..", "D:/..", "E:/..", "F:/..", "G:/.." };

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

//int anzahl = File.listRoots().length;
//for (int i=0;i<anzahl;i++)
// {DefaultMutableTreeNode node = new DefaultMutableTreeNode(File.listRoots()[i]);
//top.add(node);
//setFolder(node);}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

File root = new File (rootstr);

FileNode rootNode = new FileNode (root), node;



rootNode.explore ();

return new DefaultTreeModel (rootNode);

}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
public static void main (String args[])
{
Explorer f = new Explorer ();
f.setSize (new Dimension (400, 360));
f.setVisible (true);


}
}

class MyWindowAdapter extends WindowAdapter
{
Explorer exp;
public MyWindowAdapter ()
{
}
public MyWindowAdapter (Explorer exp)
{
this.exp = exp;
}
public void windowClosing (WindowEvent we)
{
exp.dispose ();
System.exit (0);
}
}

class MyMenuHandler implements ActionListener, ItemListener
{
Explorer exp1;
public MyMenuHandler (Explorer exp1)
{
this.exp1 = exp1;
}
public void actionPerformed (ActionEvent ae)
{
}
public void itemStateChanged (ItemEvent ie)
{
}

}


class FileNode extends DefaultMutableTreeNode
{
private boolean explored = false, selected = false;

public FileNode (File file)
{
setUserObject (file);
}
public boolean getAllowsChildren ()
{
return isDirectory ();
}
public boolean isLeaf ()
{
return !isDirectory ();
}
public File getFile ()
{
return (File) getUserObject ();
}

public void explore ()
{
explore (false);
}
public boolean isExplored ()
{
return explored;
}

public void setSelected (boolean s)
{
selected = s;
}
public boolean isSelected ()
{
return selected;
}

public boolean isDirectory ()
{
File file = (File) getUserObject ();
return file.isDirectory ();
}
public String toString ()
{
File file = (File) getUserObject ();
String filename = file.toString ();
int index = filename.lastIndexOf ("\\");

return (index != -1 &&
index !=
filename.length () - 1) ? filename.substring (index +
1) : filename;
}
public void explore (boolean force)
{
if (!isExplored () || force)
{
File file = getFile ();
File[]children = file.listFiles ();
if(children==null)
{System.out.println("can't access this root "+file);
return ;
}


for (int i = 0; i < children.length; ++i)
{
FileNode temp = (new FileNode (children[i]));
String tempstr = temp.toString ();
if (temp.isDirectory () || tempstr.endsWith ("gif") ||
tempstr.endsWith ("jpg"))
{
System.out.println (tempstr);
add (new FileNode (children[i]));
}
}

explored = true;
}
}
}
class FileNodeRenderer extends DefaultTreeCellRenderer
{

private JPanel panel = new JPanel ();

public FileNodeRenderer ()
{
panel.setBackground (UIManager.getColor ("Tree.textBackground"));

setOpaque (false);

panel.setOpaque (false);

panel.setLayout (new FlowLayout (FlowLayout.CENTER, 0, 0));
panel.add (this);


}
public Component getTreeCellRendererComponent (JTree tree, Object value,
boolean selected,
boolean expanded,
boolean leaf, int row,
boolean hasFocus)
{
FileNode node = (FileNode) value;
String s = tree.convertValueToText (value, selected,
expanded, leaf, row, hasFocus);

super.getTreeCellRendererComponent (tree, value, selected, expanded,
leaf, row, hasFocus);



return panel;
}
}
//please help me to make all the driver's worke well,thanks a lot
 
ddddfdffd hello world
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works ! thanks a lot!!!
as you can see I'm just a beginner ,thank for your help.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!