社区
Web 开发
帖子详情
怎样在jsp页面中显示一个javax.swing.tree?
g_yxh
2002-02-27 03:22:45
用的是DefaultMutableTreeNode
Thx
...全文
97
2
打赏
收藏
怎样在jsp页面中显示一个javax.swing.tree?
用的是DefaultMutableTreeNode Thx
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
pengji
2002-02-27
打赏
举报
回复
不可能,除非是用APPLET!
因为JTREE需要用可视化组建,而JSP中是不可以用的!
hexiaofeng
2002-02-27
打赏
举报
回复
JTree ?? 不可能吧??? 显示在 applet 还行
gae报错:
java
x.
swing
.
tree
.
Tree
Node is a restricted class的解决办法
NULL 博文链接:https://romotc-gmail-com.iteye.com/blog/505927
The
Java
Developers Almanac
一大堆
java
基础类的小例子
java
.applet [8 examples]
java
.awt [78 examples]
java
.awt.datatransfer [3 examples]
java
.awt.dnd [3 examples]
java
.awt.event [8 examples]
java
.awt.font [5 examples]
java
.awt.geom [4 examples]
java
.awt.image [21 examples]
java
.awt.print [6 examples]
java
.beans [12 examples]
java
.io [35 examples]
java
.lang [58 examples]
java
.lang.ref [3 examples]
java
.lang.reflect [17 examples]
java
.math [6 examples]
java
.net [27 examples]
java
.nio [27 examples]
java
.nio.charset [2 examples]
java
.rmi [6 examples]
java
.security [30 examples]
java
.security.cert [9 examples]
java
.sql [73 examples]
java
.text [26 examples]
java
.util [50 examples]
java
.util.jar [5 examples]
java
.util.logging [20 examples]
java
.util.prefs [18 examples]
java
.util.regex [26 examples]
java
.util.zip [9 examples]
java
x.accessibility [7 examples]
java
x.crypto [14 examples]
java
x.imageio [6 examples]
java
x.naming [9 examples]
java
x.naming.directory [12 examples]
java
x.naming.event [2 examples]
java
x.naming.ldap [4 examples]
java
x.net.ssl [4 examples]
java
x.print [6 examples]
java
x.print.attribute [4 examples]
java
x.print.attribute.standard [3 examples]
java
x.print.event [3 examples]
java
x.rmi [3 examples]
java
x.security.auth.login [3 examples]
java
x.security.cert [1 examples]
java
x.servlet [11 examples]
java
x.servlet.jsp [18 examples]
java
x.servlet.jsp.jstl.core [6 examples]
java
x.servlet.jsp.tagext [1 examples]
java
x.sound.midi [7 examples]
java
x.sound.sampled [9 examples]
java
x.
swing
[141 examples]
java
x.
swing
.border [3 examples]
java
x.
swing
.colorchooser [10 examples]
java
x.
swing
.event [1 examples]
java
x.
swing
.filechooser [19 examples]
java
x.
swing
.table [62 examples]
java
x.
swing
.text [49 examples]
java
x.
swing
.text.html [2 examples]
java
x.
swing
.
tree
[15 examples]
java
x.
swing
.undo [1 examples]
java
x.xml.parsers [8 examples]
java
x.xml.transform [5 examples]
java
x.xml.transform.sax [1 examples]
org.w3c.dom [30 examples]
org.xml.sax [3 examples]
swing
文件拖拽
实现文件的拖拽功能 import
java
.awt.AlphaComposite; import
java
.awt.Component; import
java
.awt.Graphics2D; import
java
.awt.Point; import
java
.awt.Rectangle; import
java
.awt.Toolkit; import
java
.awt.datatransfer.DataFlavor; import
java
.awt.datatransfer.StringSelection; import
java
.awt.datatransfer.Transferable; import
java
.awt.datatransfer.UnsupportedFlavorException; import
java
.awt.dnd.DnDConstants; import
java
.awt.dnd.DragGestureEvent; import
java
.awt.dnd.DragGestureListener; import
java
.awt.dnd.DragSource; import
java
.awt.dnd.DragSourceDragEvent; import
java
.awt.dnd.DragSourceDropEvent; import
java
.awt.dnd.DragSourceEvent; import
java
.awt.dnd.DragSourceListener; import
java
.awt.dnd.DropTarget; import
java
.awt.dnd.DropTargetDragEvent; import
java
.awt.dnd.DropTargetDropEvent; import
java
.awt.dnd.DropTargetEvent; import
java
.awt.dnd.DropTargetListener; import
java
.awt.event.ActionEvent; import
java
.awt.event.ActionListener; import
java
.awt.event.KeyAdapter; import
java
.awt.event.KeyEvent; import
java
.awt.geom.AffineTransform; import
java
.awt.geom.Rectangle2D; import
java
.awt.image.BufferedImage; import
java
.io.File; import
java
.io.FileInputStream; import
java
.io.FileOutputStream; import
java
.io.IOException; import
java
.util.Iterator; import
java
.util.List; import
java
x.
swing
.Icon; import
java
x.
swing
.JLabel; import
java
x.
swing
.J
Tree
; import
java
x.
swing
.Timer; import
java
x.
swing
.event.
Tree
ExpansionEvent; import
java
x.
swing
.event.
Tree
ExpansionListener; import
java
x.
swing
.filechooser.FileSystemView; import
java
x.
swing
.
tree
.DefaultMutable
Tree
Node; import
java
x.
swing
.
tree
.Default
Tree
CellRenderer; import
java
x.
swing
.
tree
.Default
Tree
Model; import
java
x.
swing
.
tree
.
Tree
Path; class Drag
Tree
extends J
Tree
implements DragGestureListener, DragSourceListener, DropTargetListener { /** * */ private static final long serialVersionUID = 1L; BufferedImage ghostImage; private Rectangle2D ghostRect = new Rectangle2D.Float(); private Point ptOffset = new Point(); private Point lastPoint = new Point(); private
Tree
Path lastPath; private Timer hoverTimer; FileNode sourceNode; public Drag
Tree
() { DragSource dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, // component where // drag originates DnDConstants.ACTION_COPY_OR_MOVE, // actions this); // drag gesture recognizer setModel(create
Tree
Model()); add
Tree
ExpansionListener(new
Tree
ExpansionListener() { public void
tree
Collapsed(
Tree
ExpansionEvent e) { } public void
tree
Expanded(
Tree
ExpansionEvent e) {
Tree
Path path = e.getPath(); if (path != null) { FileNode node = (FileNode) path.getLastPathComponent(); if (!node.isExplored()) { Default
Tree
Model model = (Default
Tree
Model) getModel(); node.explore(); model.nodeStructureChanged(node); } } } }); this.setCellRenderer(new Default
Tree
CellRenderer() { public Component get
Tree
CellRendererComponent(J
Tree
tree
, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Tree
Path tp =
tree
.getPathForRow(row); if (tp != null) { FileNode node = (FileNode) tp.getLastPathComponent(); File f = node.getFile(); try { Icon icon = FileSystemView.getFileSystemView() .getSystemIcon(f); this.setIcon(icon); this.setLeafIcon(icon); this.setOpenIcon(icon); this.setClosedIcon(icon); this.setDisabledIcon(icon); } catch (Exception e) { e.printStackTrace(); } } return super.get
Tree
CellRendererComponent(
tree
, value, selected, expanded, leaf, row, hasFocus); } }); super.setScrollsOnExpand(true); new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this); // Set up a hover timer, so that a node will be automatically expanded // or collapsed // if the user lingers on it for more than a short time hoverTimer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { if (lastPath == null) { return; } if (getRowForPath(lastPath) == 0) return; // Do nothing if we are hovering over the root node if (isExpanded(lastPath)) collapsePath(lastPath); else expandPath(lastPath); } }); hoverTimer.setRepeats(false); // Set timer to one-shot mode this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); int modifiers = e.getModifiers(); if (code == 'v' || code == 'V') { System.out.println("find v"); System.out.println("modifiers:" + modifiers + "\t" + ((modifiers & KeyEvent.CTRL_MASK) != 0)); } if ((modifiers & KeyEvent.CTRL_MASK) != 0 && (code == 'v' || code == 'V')) { Transferable tr = Toolkit.getDefaultToolkit() .getSystemClipboard().getContents(null);
Tree
Path path = getSelectionPath(); if (path == null) { return; } FileNode node = (FileNode) path.getLastPathComponent(); if (node.isDirectory()) { System.out.println("file cp"); try { List list = (List) (tr .getTransferData(DataFlavor.
java
FileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); } catch (Exception ioe) { ioe.printStackTrace(); } updateUI(); } } } }); } public void dragGestureRecognized(DragGestureEvent e) { // drag anything ...
Tree
Path path = getLeadSelectionPath(); if (path == null) return; FileNode node = (FileNode) path.getLastPathComponent(); sourceNode = node; // Work out the offset of the drag point from the
Tree
Path bounding // rectangle origin Rectangle raPath = getPathBounds(path); Point ptDragOrigin = e.getDragOrigin(); ptOffset.setLocation(ptDragOrigin.x - raPath.x, ptDragOrigin.y - raPath.y); // Get the cell renderer (which is a JLabel) for the path being dragged int row = this.getRowForLocation(ptDragOrigin.x, ptDragOrigin.y); JLabel lbl = (JLabel) getCellRenderer().get
Tree
CellRendererComponent( this, //
tree
path.getLastPathComponent(), // value false, // isSelected (dont want a colored background) isExpanded(path), // isExpanded getModel().isLeaf(path.getLastPathComponent()), // isLeaf row, // row (not important for rendering) false // hasFocus (dont want a focus rectangle) ); lbl.setSize((int) raPath.getWidth(), (int) raPath.getHeight()); // <-- // The // layout // manager // would // normally // do // this // Get a buffered image of the selection for dragging a ghost image this.ghostImage = new BufferedImage((int) raPath.getWidth(), (int) raPath.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2 = ghostImage.createGraphics(); // Ask the cell renderer to paint itself into the BufferedImage g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); // Make the image ghostlike lbl.paint(g2); g2.dispose(); // this.getGraphics().drawImage(ghostImage, e.getDragOrigin().x, // e.getDragOrigin().y, this); e.startDrag( null, // cursor ghostImage, new Point(5, 5), new StringSelection(getFilename()), // transferable this); // drag source listener } public void dragDropEnd(DragSourceDropEvent e) { ghostImage = null; sourceNode = null; } public void dragEnter(DragSourceDragEvent e) { } public void dragExit(DragSourceEvent e) { if (!DragSource.isDragImageSupported()) { repaint(ghostRect.getBounds()); } } public void dragOver(DragSourceDragEvent e) { } public void dropActionChanged(DragSourceDragEvent e) { } public String getFilename() {
Tree
Path path = getLeadSelectionPath(); FileNode node = (FileNode) path.getLastPathComponent(); return ((File) node.getUserObject()).getAbsolutePath(); } private Default
Tree
Model create
Tree
Model() { File root = FileSystemView.getFileSystemView().getRoots()[0]; FileNode rootNode = new FileNode(root); rootNode.explore(); return new Default
Tree
Model(rootNode); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragOver(DropTargetDragEvent dtde) { Point pt = dtde.getLocation(); if (pt.equals(lastPoint)) { return; } if (ghostImage != null) { Graphics2D g2 = (Graphics2D) getGraphics(); // If a drag image is not supported by the platform, then draw my // own drag image if (!DragSource.isDragImageSupported()) { paintImmediately(ghostRect.getBounds()); // Rub out the last // ghost image and cue // line // And remember where we are about to draw the new ghost image ghostRect.setRect(pt.x - ptOffset.x, pt.y - ptOffset.y, ghostImage.getWidth(), ghostImage.getHeight()); g2.drawImage((ghostImage), AffineTransform .getTranslateInstance(ghostRect.getX(), ghostRect.getY()), null); } }
Tree
Path path = getClosestPathForLocation(pt.x, pt.y); if (!(path == lastPath)) { lastPath = path; hoverTimer.restart(); } } public void dropActionChanged(DropTargetDragEvent dtde) { } public void drop(DropTargetDropEvent e) { try { DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable tr = e.getTransferable();
Tree
Path path = this.getPathForLocation(e.getLocation().x, e.getLocation().y); if (path == null) { e.rejectDrop(); return; } FileNode node = (FileNode) path.getLastPathComponent(); if (e.isDataFlavorSupported(DataFlavor.
java
FileListFlavor) && node.isDirectory()) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); System.out.println("file cp"); List list = (List) (e.getTransferable() .getTransferData(DataFlavor.
java
FileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); e.dropComplete(true); this.updateUI(); } else if (e.isDataFlavorSupported(stringFlavor) && node.isDirectory()) { String filename = (String) tr.getTransferData(stringFlavor); if (filename.endsWith(".txt") || filename.endsWith(".
java
") || filename.endsWith(".jsp") || filename.endsWith(".html") || filename.endsWith(".htm")) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); File f = new File(filename); if (f.exists()) { f.renameTo(new File(node.getFile(), f.getName())); node.reexplore(); ((FileNode) sourceNode.getParent()).remove(sourceNode); e.dropComplete(true); this.updateUI(); } else { e.rejectDrop(); } } else { e.rejectDrop(); } } else { e.rejectDrop(); } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } finally { ghostImage = null; this.repaint(); } } private void cp(File src, File dest) throws IOException { if (src.isDirectory()) { if (!dest.exists()) { boolean ret = dest.mkdir(); if (ret == false) return; } File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { cp(fs[i], new File(dest, fs[i].getName())); } return; } byte[] buf = new byte[1024]; FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dest); int len; try { while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { in.close(); out.close(); } } public void dragExit(DropTargetEvent dte) { } } class FileNode extends DefaultMutable
Tree
Node { private boolean explored = false; public FileNode(File file) { setUserObject(file); } public boolean getAllowsChildren() { return isDirectory(); } public boolean isLeaf() { return !isDirectory(); } public File getFile() { return (File) getUserObject(); } public boolean isExplored() { return explored; } public boolean isDirectory() { File file = getFile(); return file.isDirectory(); } public String toString() { File file = (File) getUserObject(); String filename = file.toString(); int index = filename.lastIndexOf(File.separator); return (index != -1 && index != filename.length() - 1) ? filename .substring(index + 1) : filename; } public void explore() { if (!isDirectory()) return; if (!isExplored()) { File file = getFile(); File[] children = file.listFiles(); for (int i = 0; i < children.length; ++i) { if (children[i].isDirectory()) add(new FileNode(children[i])); } for (int i = 0; i < children.length; ++i) { if (!children[i].isDirectory()) add(new FileNode(children[i])); } explored = true; } } public void reexplore() { this.removeAllChildren(); explored = false; explore(); } }
java
动态树形(
tree
)
本实例用SqlServer数据库,内有数据库代码。动态的为文本框获取所需要的值,用模型窗体返回。请自己连接数据库的ip地址。
jtable增删改查和j
tree
连接
package BookTest; import
java
.awt.*; import
java
.awt.event.*; import
java
.util.AbstractQueue; import
java
.util.Vector; import
java
x.
swing
.*; import
java
x.
swing
.table.*; import
java
x.
swing
.
tree
.DefaultMutable
Tree
Node; import
java
x.
swing
.
tree
.Default
Tree
CellRenderer; import
java
x.
swing
.
tree
.Default
Tree
Model; import
java
x.
swing
.
tree
.
Tree
SelectionModel; import
java
x.
swing
.event.*; import
java
x.
swing
.table.TableColumnModel; //import DbHelp.OperatorJobs; import Dao.ArrayBook; public class Test extends JFrame { public JPanel jp=null; public String aa; //JTable jTable1 = new JTable(); public JTable jTable1=null; private J
Tree
j
Tree
1; DefaultTableModel mode; private JScrollPane scrollPane = null; DefaultMutable
Tree
Node node; JButton[] btn={new JButton("增加"),new JButton("删除"),new JButton("修改"),new JButton("查询"),new JButton("重置")}; public JTextField[] txt ={new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField()}; public String[] str1={"1","summer","boy","20","",""}; public String[] str2={"2","wulei","girl","19","",""}; public String[] str3={"3","west","boy","20","",""}; public String[] str4={"4","fish","girl","18","",""}; public String[] str5={"5","youku","boy","21","",""}; public String[] str6={"6","aa","boy","21","",""}; public String[][] tempData1={str1,str2,str3,str4,str5,str6}; public String[] aa1={"1","summer","boy","20","123","123"}; public String[] aa2={"2","wulei","girl","19","123",""}; public String[] aa3={"3","west","boy","20","123",""}; public String[] aa4={"4","fish","girl","18","1",""}; public String[] aa5={"5","youku","boy","21","12",""}; public String[] aa6={"6","aa","boy","21","123",""}; public String[][] tempData4={aa1,aa2,aa3,aa4,aa5,aa6}; //设置表头数据 public String[] tempTitle={"ID","标题","内容","作者","时间","是否选
中
"}; //设置表格的数据集合 Vector v1=new Vector(); Vecto
Web 开发
81,122
社区成员
341,744
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章