版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、首先来看一下展示界面,JTree的特点有:节点可拖拽,根据输入生成目录,右键有菜单,点击触发事件等 JTable的特点有:含有隐藏列,可以进行翻页,列中含有下载按钮等在来看一下代码吧,基本代码由Eclipse上的WindowBuilder进行拖拽式产生的,所以看起来有点乱,不过没事在使用Wind
2、owBuilder的时候要画JTree和JTable时要注意,这两个有几个共同点,必须放到JScrollPanel中,因为这两个控件的内容长度会发生突然变化,所以要使用JScrollPanel。BTW:如果要使用WindowBuidler,除了在Eclipse上安装插件,最方便的方法是直接下载含有该插件的Eclipse版本:package test;import java.awt.BorderLayout;import java.awt.Component;import java.awt.EventQueue;import java.awt.GridBagConstraints;import
3、java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.List;import javax.swing.AbstractAction;import javax.swing.DefaultCellEditor;import javax.swing.JBu
4、tton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.JTree;import javax.swing.border.EmptyBorder;import
5、 javax.swing.table.AbstractTableModel;import javax.swing.table.TableCellRenderer;import javax.swing.tree.DefaultMutableTreeNode;public class InnerDocsMgt extends JFrame private static final long serialVersionUID = 1L;private JPanel contentPane;private JTable table;private JTree tree;private JLabel c
6、urrentDirLabel;private JLabel totalFilesNumLabel;private List<VEachFile> docFils;private final int maxFilesAPage = 50;private JLabel curPageLabel;private int curPageNum = 0;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run
7、() try InnerDocsMgt frame = new InnerDocsMgt();frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the frame. */public InnerDocsMgt() setTitle("u5185u90E8u8D44u6599u7BA1u7406");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);this.setSi
8、ze(800, 600);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);contentPane.setLayout(new BorderLayout(0, 0);setContentPane(contentPane);JPanel panel = new JPanel();contentPane.add(panel, BorderLayout.WEST);GridBagLayout gbl_panel = new GridBagLayout();gbl_panel.columnWidth
9、s = new int 54, 0, 0, 0, 0, 0, 0, 0 ;gbl_panel.rowHeights = new int 31, 0, 0 ;gbl_panel.columnWeights = new double 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, Double.MIN_VALUE ;gbl_panel.rowWeights = new double 0.0, 1.0, Double.MIN_VALUE ;panel.setLayout(gbl_panel);JLabel lblNewLabel = new JLabel("u5185u
10、90E8u8D44u6599u76EEu5F55u5217u8868");GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();gbc_lblNewLabel.gridwidth = 5;gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);gbc_lblNewLabel.gridx = 0;gbc_lblNewLabel.gridy = 0;panel.add(lblNewLabel, gbc_lblNewLabel);JButton button_4 = new JBu
11、tton("u4FDDu5B58u76EEu5F55u64CDu4F5C");GridBagConstraints gbc_button_4 = new GridBagConstraints();gbc_button_4.insets = new Insets(0, 0, 5, 5);gbc_button_4.gridx = 5;gbc_button_4.gridy = 0;panel.add(button_4, gbc_button_4);JScrollPane scrollPane = new JScrollPane();GridBagConstraints gbc_s
12、crollPane = new GridBagConstraints();gbc_scrollPane.gridwidth = 7;gbc_scrollPane.fill = GridBagConstraints.BOTH;gbc_scrollPane.gridx = 0;gbc_scrollPane.gridy = 1;panel.add(scrollPane, gbc_scrollPane);DefaultMutableTreeNode top = new DefaultMutableTreeNode("/");createNodes(top);tree = new J
13、Tree(top);scrollPane.setViewportView(tree);tree.setEditable(true);tree.setDragEnabled(true);this.setPopupMenu();try tree.setTransferHandler(new DocsTreeTransferHanlder(); catch (ClassNotFoundException e) e.printStackTrace();JPanel panel_1 = new JPanel();contentPane.add(panel_1, BorderLayout.CENTER);
14、GridBagLayout gbl_panel_1 = new GridBagLayout();gbl_panel_1.columnWidths = new int 708, 0 ;gbl_panel_1.rowHeights = new int 556, 0 ;gbl_panel_1.columnWeights = new double 1.0, Double.MIN_VALUE ;gbl_panel_1.rowWeights = new double 1.0, Double.MIN_VALUE ;panel_1.setLayout(gbl_panel_1);JTabbedPane tabb
15、edPane = new JTabbedPane(JTabbedPane.TOP);GridBagConstraints gbc_tabbedPane = new GridBagConstraints();gbc_tabbedPane.fill = GridBagConstraints.BOTH;gbc_tabbedPane.gridx = 0;gbc_tabbedPane.gridy = 0;panel_1.add(tabbedPane, gbc_tabbedPane);JPanel panel_2 = new JPanel();tabbedPane.addTab("u6587u4
16、EF6u4E0Eu76EEu5F55u7BA1u7406", null,panel_2, null);GridBagLayout gbl_panel_2 = new GridBagLayout();gbl_panel_2.columnWidths = new int 166, 59, 0, 0, 0, 0, 0 ;gbl_panel_2.rowHeights = new int 30, 0, 49, 0, 0, 0 ;gbl_panel_2.columnWeights = new double 1.0, 0.0, 0.0, 0.0, 0.0,0.0, Double.MIN_VALUE
17、 ;gbl_panel_2.rowWeights = new double 0.0, 0.0, 1.0, 0.0, 0.0,Double.MIN_VALUE ;panel_2.setLayout(gbl_panel_2);currentDirLabel = new JLabel("u5F53u524Du76EEu5F55uFF1Aone->two->three");GridBagConstraints gbc_lblonetwothree = new GridBagConstraints();gbc_lblonetwothree.gridwidth = 6;gb
18、c_lblonetwothree.anchor = GridBagConstraints.WEST;gbc_lblonetwothree.insets = new Insets(0, 0, 5, 0);gbc_lblonetwothree.gridx = 0;gbc_lblonetwothree.gridy = 0;panel_2.add(currentDirLabel, gbc_lblonetwothree);JButton button_3 = new JButton("u5168u9009/u5168u4E0Du9009");GridBagConstraints gb
19、c_button_3 = new GridBagConstraints();gbc_button_3.insets = new Insets(0, 0, 5, 5);gbc_button_3.gridx = 3;gbc_button_3.gridy = 1;panel_2.add(button_3, gbc_button_3);JButton button = new JButton("u5220u9664");GridBagConstraints gbc_button = new GridBagConstraints();gbc_button.insets = new I
20、nsets(0, 0, 5, 5);gbc_button.gridx = 4;gbc_button.gridy = 1;panel_2.add(button, gbc_button);JButton button_1 = new JButton("u79FBu52A8u5230");button_1.setToolTipText("u8BF7u5148u9009u62E9u8981u79FBu52A8u7684u6587u4EF6uFF0Cu7136u540Eu518Du9009u62E9u4E00u4E2Au5DE6u4FA7u76EEu5F55");
21、GridBagConstraints gbc_button_1 = new GridBagConstraints();gbc_button_1.insets = new Insets(0, 0, 5, 0);gbc_button_1.gridx = 5;gbc_button_1.gridy = 1;panel_2.add(button_1, gbc_button_1);JScrollPane scrollPane_1 = new JScrollPane();GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();gbc_sc
22、rollPane_1.gridwidth = 6;gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);gbc_scrollPane_1.fill = GridBagConstraints.BOTH;gbc_scrollPane_1.gridx = 0;gbc_scrollPane_1.gridy = 2;panel_2.add(scrollPane_1, gbc_scrollPane_1);String headNames = new String "u9009u62E9","u6587u4EF6u540D"
23、, "u5927u5C0FuFF08KBuFF09","u6587u4EF6u65E5u671F", "u4F5Cu8005", "", "u4E0Bu8F7D" ;Object tableDatas = new Object507;for (int i = 0; i < 50; i+) for (int j = 0; j < 7; j+) tableDatasij = null;table = new JTable(new DocsTableModel(headNames, tab
24、leDatas);table.getColumnModel().getColumn(0).setCellEditor(table.getDefaultEditor(Boolean.class);table.getColumnModel().getColumn(0).setCellRenderer(table.getDefaultRenderer(Boolean.class);table.getColumnModel().getColumn(0).setPreferredWidth(38);table.getColumnModel().getColumn(1).setPreferredWidth
25、(206);table.getColumnModel().getColumn(2).setPreferredWidth(53);table.getColumnModel().getColumn(3).setPreferredWidth(134);table.getColumnModel().getColumn(4).setPreferredWidth(58);/ hide the 5th column,it contans the filepathtable.getTableHeader().getColumnModel().getColumn(5).setMaxWidth(0);/ hide
26、table.getTableHeader().getColumnModel().getColumn(5).setMinWidth(0);/ hidetable.getTableHeader().getColumnModel().getColumn(5).setPreferredWidth(0);table.getTableHeader().getColumnModel().getColumn(5).setWidth(0);table.getColumnModel().getColumn(6).setCellRenderer(new ButtonCellRenderer();table.getC
27、olumnModel().getColumn(6).setCellEditor(new ButtonCellEditor(table);scrollPane_1.setViewportView(table);totalFilesNumLabel = new JLabel("u6587u4EF6u603Bu6570uFF1A0");GridBagConstraints gbc_totalFilesNumLabel = new GridBagConstraints();gbc_totalFilesNumLabel.anchor = GridBagConstraints.WEST
28、;gbc_totalFilesNumLabel.insets = new Insets(0, 0, 0, 5);gbc_totalFilesNumLabel.gridx = 0;gbc_totalFilesNumLabel.gridy = 4;panel_2.add(totalFilesNumLabel, gbc_totalFilesNumLabel);curPageLabel = new JLabel("u5F53u524Du9875u6570uFF1A0| u603Bu9875u6570uFF1A0");GridBagConstraints gbc_curPageLab
29、el = new GridBagConstraints();gbc_curPageLabel.gridwidth = 2;gbc_curPageLabel.insets = new Insets(0, 0, 0, 5);gbc_curPageLabel.gridx = 2;gbc_curPageLabel.gridy = 4;panel_2.add(curPageLabel, gbc_curPageLabel);JButton btnNewButton = new JButton("u4E0Au4E00u9875");btnNewButton.addActionListen
30、er(new ActionListener() public void actionPerformed(ActionEvent e) prePage(););GridBagConstraints gbc_btnNewButton = new GridBagConstraints();gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);gbc_btnNewButton.gridx = 4;gbc_btnNewButton.gridy = 4;panel_2.add(btnNewButton, gbc_btnNewButton);JButton but
31、ton_2 = new JButton("u4E0Bu4E00u9875");button_2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) nextPage(););GridBagConstraints gbc_button_2 = new GridBagConstraints();gbc_button_2.gridx = 5;gbc_button_2.gridy = 4;panel_2.add(button_2, gbc_button_2);JPanel
32、 panel_3 = new JPanel();tabbedPane.addTab("u7F16u8F91u6587u6863", null, panel_3, null);GridBagLayout gbl_panel_3 = new GridBagLayout();gbl_panel_3.columnWidths = new int 0 ;gbl_panel_3.rowHeights = new int 0 ;gbl_panel_3.columnWeights = new double Double.MIN_VALUE ;gbl_panel_3.rowWeights =
33、 new double Double.MIN_VALUE ;panel_3.setLayout(gbl_panel_3);private void createNodes(DefaultMutableTreeNode top) List<DefaultMutableTreeNode> nodes = DocsMgtRmi.getINS().getFirstLevelDirs();for (DefaultMutableTreeNode one : nodes) top.add(one);public void setPopupMenu() final JPopupMenu pop =
34、 new JPopupMenu();pop.add(new AbstractAction("添加子目录") private static final long serialVersionUID = 1L;public void actionPerformed(ActionEvent e) System.out.println("Tree Add"););pop.add(new AbstractAction("删除目录") private static final long serialVersionUID = 1L;public vo
35、id actionPerformed(ActionEvent e) System.out.println("Delete"););tree.addMouseListener(new MouseAdapter() Overridepublic void mouseReleased(MouseEvent e) if (e.isMetaDown() pop.show(tree, e.getX(), e.getY();public void mouseClicked(MouseEvent evt) if (evt.getClickCount() = 1) VEachDir eDir
36、 = (VEachDir) getSelectedNode().getUserObject();docFils = DocsMgtRmi.getINS().getFilesViaDirid(eDir.getDirId();int filesNum = docFils.size();currentDirLabel.setText("u5F53u524Du76EEu5F55uFF1Aone->two->three,=>DirID:"+ eDir.getDirId();totalFilesNumLabel.setText("u6587u4EF6u603
37、Bu6570uFF1A"+ filesNum);int maxNum = Math.min(maxFilesAPage, filesNum);if (filesNum <= maxFilesAPage) curPageLabel.setText("u5F53u524Du9875u6570uFF1A 1 | u603Bu9875u6570uFF1A 1"); else int totalNum = filesNum / maxFilesAPage;if (totalNum * maxFilesAPage < filesNum) totalNum+;cur
38、PageLabel.setText("u5F53u524Du9875u6570uFF1A 1 | u603Bu9875u6570uFF1A "+ totalNum);curPageNum = 0;setDocTableValues(docFils.subList(0, maxNum););public void prePage() if (this.curPageNum <= 0) return;int filesNum = docFils.size();curPageNum-;int totalPage = filesNum / maxFilesAPage;if (
39、totalPage * maxFilesAPage < filesNum) totalPage+;curPageLabel.setText("u5F53u524Du9875u6570uFF1A " + (curPageNum + 1)+ " | u603Bu9875u6570uFF1A " + totalPage);setDocTableValues(docFils.subList(curPageNum * maxFilesAPage,Math.min(curPageNum + 1) * maxFilesAPage, filesNum);publi
40、c void nextPage() int filesNum = docFils.size();int totalPage = filesNum / maxFilesAPage;if (totalPage * maxFilesAPage < filesNum) totalPage+;if (curPageNum >= totalPage - 1) return;curPageNum+;setDocTableValues(docFils.subList(curPageNum * maxFilesAPage,Math.min(curPageNum + 1) * maxFilesAPag
41、e, filesNum);curPageLabel.setText("u5F53u524Du9875u6570uFF1A " + (curPageNum + 1)+ " | u603Bu9875u6570uFF1A " + totalPage);public void setDocTableValues(List<VEachFile> subDocs) int i = 0;for (; i < subDocs.size(); i+) table.getModel().setValueAt(false, i, 0);table.getMo
42、del().setValueAt(subDocs.get(i).getFileName(), i, 1);table.getModel().setValueAt(subDocs.get(i).getFileSize(), i, 2);table.getModel().setValueAt(subDocs.get(i).getFileDate().toLocaleString(), i, 3);table.getModel().setValueAt(subDocs.get(i).getFileAuthor(), i, 4);table.getModel().setValueAt(subDocs.
43、get(i).getFilePath(), i, 5);if (subDocs.size() < maxFilesAPage) for (; i < maxFilesAPage; i+) table.getModel().setValueAt(false, i, 0);table.getModel().setValueAt(null, i, 1);table.getModel().setValueAt(null, i, 2);table.getModel().setValueAt(null, i, 3);table.getModel().setValueAt(null, i, 4)
44、;table.getModel().setValueAt(null, i, 5);table.updateUI();table.repaint();public DefaultMutableTreeNode getSelectedNode() return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();class DocsTableModel extends AbstractTableModel private String headName;private Object obj;private Class colum
45、nTypes = new Class Boolean.class,Object.class, Object.class, Object.class, Object.class,Object.class, Object.class ;public DocsTableModel() super();public DocsTableModel(String headName, Object obj) this();this.headName = headName;this.obj = obj;public int getColumnCount() return headName.length;pub
46、lic int getRowCount() return obj.length;public Object getValueAt(int r, int c) return objrc;public String getColumnName(int c) return headNamec;public Class<?> getColumnClass(int columnIndex) return columnTypescolumnIndex.getClass();Overridepublic boolean isCellEditable(int rowIndex, int colum
47、nIndex) return true;public void setValueAt(Object value, int row, int col) objrowcol = value;fireTableCellUpdated(row, col);class ButtonCellEditor extends DefaultCellEditor private static final long serialVersionUID = -6546334664166791132L;private JButton button;private JTable table;public ButtonCel
48、lEditor(JTable table) super(new JTextField();this.setClickCountToStart(1);this.initButton();this.table = table;private void initButton() this.button = new JButton();this.button.setSize(50, 15);this.button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ButtonCellEdi
49、tor.this.fireEditingCanceled();System.out.println("Selected Column:"+ table.getSelectedColumn()+ ",row:"+ table.getSelectedRow()+ ",and filePath:"+ table.getModel().getValueAt(table.getSelectedRow(), 5););Overridepublic Component getTableCellEditorComponent(JTable table
50、, Object value,boolean isSelected, int row, int column) this.button.setText("下载");return this.button;Overridepublic Object getCellEditorValue() return this.button.getText();class ButtonCellRenderer implements TableCellRenderer private JButton button;public ButtonCellRenderer() this.button
51、= new JButton();public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) this.button.setText("下载");return this.button;关于上面的TableCellRenderer子类和CellEditor子类,这两个都被设置到table的ColumnModel中了, ButtonCellRenderer在每次GUI渲染的时候
52、都会调用,ButtonCellEditor只会在点击到这个Cell的时候才会调用,并且如果失去焦点后return的Component将消失,所以ButtonCellRenderer这个很重要。table.getColumnModel().getColumn(7).setCellRenderer(new ButtonCellRenderer();table.getColumnModel().getColumn(7).setCellEditor(new ButtonCellEditor(table); 可以在ButtonCellRenderer中加上一些逻辑,用来判断是否显示按钮,比如在
53、初始化的时候不要显示按钮,比如可以可以根据隐藏列的值显示按钮,只要修改getTableRenderComponent方法即可。public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) String filePath = (String) table.getModel().getValueAt(row, 7);if (filePath = null) return null; else this.button.setText("下载");return this.button;下面讲一下支持Tree拖拽的事件响应器DocsTreeTransferHanlder,TransferHanlder用来处理Transferable一个Swing Component对象上的transfer to和from,就是拖拽,而Transferable对象代表通过剪切到,拷贝自剪贴板上的数据对象。这几个对象被用来实现拖拽,TansferHanlder对拖拽支持良好,开发者只需要实现一个具体的Transfer对象,然后将hanld
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年工人受伤赔偿协议书(610字)
- 2024年理疗项目承包合同
- 2024年住宅楼买卖合同
- 2024年幼儿园协议书模板
- 2024年青岛房地产房屋买卖合同
- 2024年标准版工程资料承包合同
- 2024年暑期辅导班协议书范本
- 2024年司机劳动合同
- 2024年司机劳动合同范文
- 事业单位人员聘用制度2024年
- 2024年2024年离婚协议书模板电子版
- 中国慢性冠脉综合征患者诊断及管理指南2024版解读
- 中国法律史-第二次平时作业-国开-参考资料
- (高清版)JTGT D81-2017 公路交通安全设施设计细则
- (正式版)SHT 3551-2024 石油化工仪表工程施工及验收规范
- 消化道出血的PBL教学查房
- 营销总监绩效考核表(完整版)
- 灌浆平洞专项施工方案(完整版)
- 房屋修缮工程施工方案(完整版)
- 我国主要山脉高原盆地平原
- IQ测试题和答案
评论
0/150
提交评论