package jtreedemo;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableCellRenderer;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
public class TreeDemo extends JPanel
implements TreeSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel tablePaneView;
private JTextPane textPane;
private JTree tree;
//Optionally play with line styles. Possible values are
//"Angled" (the default), "Horizontal", and "None".
private static boolean playWithLineStyle = false;
private static String lineStyle = "Horizontal";
//Optionally set the look and feel.
private static boolean useSystemLookAndFeel = true;
public TreeDemo() {
super(new GridLayout(1,0));
//Create the nodes.
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("The Book Series");
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);
if (playWithLineStyle) {
System.out.println("line style = " + lineStyle);
tree.putClientProperty("JTree.lineStyle", lineStyle);
}
//Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
//Create the Text viewing pane.
JSplitPane splitContentPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
tablePaneView = new JPanel(new GridLayout(1,0));
textPane = new JTextPane();
textPane.setEditable(false);
JScrollPane textView = new JScrollPane(textPane);
splitContentPane.setTopComponent(tablePaneView);
splitContentPane.setBottomComponent(textView);
splitContentPane.setDividerLocation(300);
//Add the scroll panes to a split pane.
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(splitContentPane);
Dimension minimumSize = new Dimension(100, 50);
textView.setMinimumSize(minimumSize);
treeView.setMinimumSize(minimumSize);
tablePaneView.setMinimumSize(minimumSize);
splitPane.setDividerLocation(300);
splitPane.setPreferredSize(new Dimension(1000, 700));
//Add the split pane to this panel.
add(splitPane);
}
/** 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;
book.displayBookInfo();
book.displayBookTable();
} else {
clearBookInfo();
}
}
private class BookInfo {
public String bookName;
public String bookType;
public String bookAuthor;
public String bookPrice;
public String bookDescription;
public BookInfo(String book, String type, String author, String price) {
bookName = book;
bookType = type;
bookAuthor = author;
bookPrice = price;
bookDescription ="";
}
public void setDescription(String desc) {
bookDescription = desc;
}
public String toString() {
return bookName;
}
public void displayBookInfo() {
String bookinfo;
bookinfo = "bookname = " + bookName + "\r\n"
+ "booktype =" + bookType + "\r\n"
+ "bookauthor =" + bookAuthor + "\r\n"
+ "bookprice =" + bookPrice + "\r\n";
textPane.setText(bookinfo);
}
public void displayBookTable() {
String[] columnNames = {"Property",
"Value"};
Object[][] data = {
{"bookName", bookName},
{"bookType", bookType},
{"bookAuthor", bookAuthor},
{"bookPrice", bookPrice},
{"bookDescription", bookDescription}
};
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//如果JTable的一个列或者JTable窗口自身的大小被重新确定,那么其他列会被相应的缩小或者放大,以适应新的窗口
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer());
//单元格内方格坐标线的缺省颜色是Color.gray。更改这些方格坐标线的颜色
//table.setGridColor(Color.red);
//改变行的高度
//table.setRowHeight(30);
//table.setRowHeight(4, 60);
//设置相邻两行单元格的距离
//table.setRowMargin (5);
//在缺省情况下,内容的前景颜色和背景颜色的选择都是由Swing的所见即所得的实现来确定的
//table.setSelectionBackground(Color.green);
//table.setSelectionForeground(Color.white);
//设置背景颜色
//table.setBackground (Color.blue);
//table.doLayout();
JScrollPane tableView = new JScrollPane(table);
tablePaneView.removeAll();
tablePaneView.add(tableView);
tablePaneView.updateUI();
}
}
private void clearBookInfo() {
String bookinfo;
bookinfo = "";
textPane.setText(bookinfo);
tablePaneView.removeAll();
tablePaneView.updateUI();
}
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode book = null;
category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);
//original Tutorial
BookInfo bookinfo = new BookInfo
("A Short Course on the Basics",
"Java", "zhangshan", "18");
bookinfo.setDescription("11111111111111111111111 "
+ "\r\n" + "22222222222222222222 "
+ "\r\n" + "33333333333333333333 "
+ "\r\n" + "44444444444444444444 "
+ "\r\n" + "55555555555555555555 "
+ "\r\n" + "66666666666666666666 "
);
book = new DefaultMutableTreeNode(bookinfo);
category.add(book);
//Tutorial Continued
book = new DefaultMutableTreeNode(new BookInfo
("The Rest of the JDK",
"Java", "lishi", "26"));
category.add(book);
//JFC Swing Tutorial
book = new DefaultMutableTreeNode(new BookInfo
("A Guide to Constructing GUIs",
"Java", "wangwu", "65"));
category.add(book);
//Bloch
book = new DefaultMutableTreeNode(new BookInfo
("Effective Java Programming Language Guide",
"Java", "zhangshan", "55"));
category.add(book);
//Arnold/Gosling
book = new DefaultMutableTreeNode(new BookInfo
("The Java Programming Language", "Java", "lishi", "67"));
category.add(book);
Java JTree+JTable应用
3星 · 超过75%的资源 需积分: 34 129 浏览量
2016-05-08
12:41:42
上传
评论
收藏 6KB RAR 举报
s31366
- 粉丝: 0
- 资源: 11
最新资源
- 基于CSS的响应式鲜花网站全屏效果设计源码
- 基于JavaScript的访客预约系统设计源码
- 基于Vue和ECharts的工作租房数据可视化系统设计源码
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
- 基于Java的Flink流批一体数据处理快速集成开发框架设计源码
- FirstFilterOrderCompare
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈