Swing完整實例 JTree(右鍵菜單,可拖拽)+JTable(可翻頁,列中含有按鈕,隱藏列)_第1頁
Swing完整實例 JTree(右鍵菜單,可拖拽)+JTable(可翻頁,列中含有按鈕,隱藏列)_第2頁
Swing完整實例 JTree(右鍵菜單,可拖拽)+JTable(可翻頁,列中含有按鈕,隱藏列)_第3頁
Swing完整實例 JTree(右鍵菜單,可拖拽)+JTable(可翻頁,列中含有按鈕,隱藏列)_第4頁
Swing完整實例 JTree(右鍵菜單,可拖拽)+JTable(可翻頁,列中含有按鈕,隱藏列)_第5頁
已閱讀5頁,還剩18頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、首先來看一下展示界面,JTree的特點有:節(jié)點可拖拽,根據(jù)輸入生成目錄,右鍵有菜單,點擊觸發(fā)事件等                                           JTable的特點有:含有隱藏列,可以進行翻頁,列中含有下載按鈕等在來看一下代碼吧,基本代碼由Eclipse上的WindowBuilder進行拖拽式產(chǎn)生的,所以看起來有點亂,不過沒事在使用Wind

2、owBuilder的時候要畫JTree和JTable時要注意,這兩個有幾個共同點,必須放到JScrollPanel中,因為這兩個控件的內(nèi)容長度會發(fā)生突然變化,所以要使用JScrollPanel。BTW:如果要使用WindowBuidler,除了在Eclipse上安裝插件,最方便的方法是直接下載含有該插件的Eclipse版本:/downloads/packages/eclipse-ide-java-developers/keplerrpackage test;import java.awt.BorderLayout;import java.awt.Com

3、ponent;import java.awt.EventQueue;import java.awt.GridBagConstraints;import 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.swin

4、g.AbstractAction;import javax.swing.DefaultCellEditor;import javax.swing.JButton;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.JTex

5、tField;import javax.swing.JTree;import javax.swing.border.EmptyBorder;import 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

6、 JPanel contentPane;private JTable table;private JTree tree;private JLabel currentDirLabel;private JLabel totalFilesNumLabel;private List<VEachFile> docFils;private final int maxFilesAPage = 50;private JLabel curPageLabel;private int curPageNum = 0;/* * Launch the application. */public static

7、void main(String args) EventQueue.invokeLater(new Runnable() public void run() try InnerDocsMgt frame = new InnerDocsMgt();frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the frame. */public InnerDocsMgt() setTitle("u5185u90E8u8D44u6599u7BA1u7406");setDefaultC

8、loseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);this.setSize(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, BorderLayo

9、ut.WEST);GridBagLayout gbl_panel = new GridBagLayout();gbl_panel.columnWidths = 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_VA

10、LUE ;panel.setLayout(gbl_panel);JLabel lblNewLabel = new JLabel("u5185u90E8u8D44u6599u76EEu5F55u5217u8868");GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();gbc_lblNewLabel.gridwidth = 5;gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);gbc_lblNewLabel.gridx = 0;gbc_lblNewLabel.

11、gridy = 0;panel.add(lblNewLabel, gbc_lblNewLabel);JButton button_4 = new JButton("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_b

12、utton_4);JScrollPane scrollPane = new JScrollPane();GridBagConstraints gbc_scrollPane = 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 t

13、op = new DefaultMutableTreeNode("/");createNodes(top);tree = new JTree(top);scrollPane.setViewportView(tree);tree.setEditable(true);tree.setDragEnabled(true);this.setPopupMenu();try tree.setTransferHandler(new DocsTreeTransferHanlder(); catch (ClassNotFoundException e) e.printStackTrace();

14、JPanel panel_1 = new JPanel();contentPane.add(panel_1, BorderLayout.CENTER);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 d

15、ouble 1.0, Double.MIN_VALUE ;panel_1.setLayout(gbl_panel_1);JTabbedPane tabbedPane = 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, g

16、bc_tabbedPane);JPanel panel_2 = new JPanel();tabbedPane.addTab("u6587u4EF6u4E0Eu76EEu5F55u7BA1u7406", 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_pane

17、l_2.columnWeights = new double 1.0, 0.0, 0.0, 0.0, 0.0,0.0, Double.MIN_VALUE ;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_l

18、blonetwothree = new GridBagConstraints();gbc_lblonetwothree.gridwidth = 6;gbc_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

19、 = new JButton("u5168u9009/u5168u4E0Du9009");GridBagConstraints gbc_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");GridBa

20、gConstraints gbc_button = new GridBagConstraints();gbc_button.insets = new Insets(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("u8BF7u5148u9009u62E9u8981u79FBu52A8u7684u6

21、587u4EF6uFF0Cu7136u540Eu518Du9009u62E9u4E00u4E2Au5DE6u4FA7u76EEu5F55");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 JScroll

22、Pane();GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();gbc_scrollPane_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);Stri

23、ng headNames = new String "u9009u62E9","u6587u4EF6u540D", "u5927u5C0FuFF08KBuFF09","u6587u4EF6u65E5u671F", "u4F5Cu8005", "", "u4E0Bu8F7D" ;Object tableDatas = new Object507;for (int i = 0; i < 50; i+) for (int j = 0; j < 7;

24、j+) tableDatasij = null;table = new JTable(new DocsTableModel(headNames, tableDatas);table.getColumnModel().getColumn(0).setCellEditor(table.getDefaultEditor(Boolean.class);table.getColumnModel().getColumn(0).setCellRenderer(table.getDefaultRenderer(Boolean.class);table.getColumnModel().getColumn(0)

25、.setPreferredWidth(38);table.getColumnModel().getColumn(1).setPreferredWidth(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 filepa

26、thtable.getTableHeader().getColumnModel().getColumn(5).setMaxWidth(0);/ hidetable.getTableHeader().getColumnModel().getColumn(5).setMinWidth(0);/ hidetable.getTableHeader().getColumnModel().getColumn(5).setPreferredWidth(0);table.getTableHeader().getColumnModel().getColumn(5).setWidth(0);table.getCo

27、lumnModel().getColumn(6).setCellRenderer(new ButtonCellRenderer();table.getColumnModel().getColumn(6).setCellEditor(new ButtonCellEditor(table);scrollPane_1.setViewportView(table);totalFilesNumLabel = new JLabel("u6587u4EF6u603Bu6570uFF1A0");GridBagConstraints gbc_totalFilesNumLabel = new

28、GridBagConstraints();gbc_totalFilesNumLabel.anchor = GridBagConstraints.WEST;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("u5F53u524Du987

29、5u6570uFF1A0| u603Bu9875u6570uFF1A0");GridBagConstraints gbc_curPageLabel = 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 btnNewBu

30、tton = new JButton("u4E0Au4E00u9875");btnNewButton.addActionListener(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_bt

31、nNewButton.gridy = 4;panel_2.add(btnNewButton, gbc_btnNewButton);JButton button_2 = new JButton("u4E0Bu4E00u9875");button_2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) nextPage(););GridBagConstraints gbc_button_2 = new GridBagConstraints();gbc_button_2

32、.gridx = 5;gbc_button_2.gridy = 4;panel_2.add(button_2, gbc_button_2);JPanel 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_p

33、anel_3.columnWeights = new double Double.MIN_VALUE ;gbl_panel_3.rowWeights = new double Double.MIN_VALUE ;panel_3.setLayout(gbl_panel_3);private void createNodes(DefaultMutableTreeNode top) List<DefaultMutableTreeNode> nodes = DocsMgtRmi.getINS().getFirstLevelDirs();for (DefaultMutableTreeNode

34、 one : nodes) top.add(one);public void setPopupMenu() final JPopupMenu pop = 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

35、("刪除目錄") private static final long serialVersionUID = 1L;public void 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

36、void mouseClicked(MouseEvent evt) if (evt.getClickCount() = 1) VEachDir eDir = (VEachDir) getSelectedNode().getUserObject();docFils = DocsMgtRmi.getINS().getFilesViaDirid(eDir.getDirId();int filesNum = docFils.size();currentDirLabel.setText("u5F53u524Du76EEu5F55uFF1Aone->two->three,=>D

37、irID:"+ eDir.getDirId();totalFilesNumLabel.setText("u6587u4EF6u603Bu6570uFF1A"+ filesNum);int maxNum = Math.min(maxFilesAPage, filesNum);if (filesNum <= maxFilesAPage) curPageLabel.setText("u5F53u524Du9875u6570uFF1A 1 | u603Bu9875u6570uFF1A 1"); else int totalNum = filesN

38、um / maxFilesAPage;if (totalNum * maxFilesAPage < filesNum) totalNum+;curPageLabel.setText("u5F53u524Du9875u6570uFF1A 1 | u603Bu9875u6570uFF1A "+ totalNum);curPageNum = 0;setDocTableValues(docFils.subList(0, maxNum););public void prePage() if (this.curPageNum <= 0) return;int filesNu

39、m = docFils.size();curPageNum-;int totalPage = filesNum / maxFilesAPage;if (totalPage * maxFilesAPage < filesNum) totalPage+;curPageLabel.setText("u5F53u524Du9875u6570uFF1A " + (curPageNum + 1)+ " | u603Bu9875u6570uFF1A " + totalPage);setDocTableValues(docFils.subList(curPageN

40、um * maxFilesAPage,Math.min(curPageNum + 1) * maxFilesAPage, filesNum);public void nextPage() int filesNum = docFils.size();int totalPage = filesNum / maxFilesAPage;if (totalPage * maxFilesAPage < filesNum) totalPage+;if (curPageNum >= totalPage - 1) return;curPageNum+;setDocTableValues(docFil

41、s.subList(curPageNum * maxFilesAPage,Math.min(curPageNum + 1) * maxFilesAPage, filesNum);curPageLabel.setText("u5F53u524Du9875u6570uFF1A " + (curPageNum + 1)+ " | u603Bu9875u6570uFF1A " + totalPage);public void setDocTableValues(List<VEachFile> subDocs) int i = 0;for (; i &

42、lt; subDocs.size(); i+) table.getModel().setValueAt(false, i, 0);table.getModel().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().setValueA

43、t(subDocs.get(i).getFileAuthor(), i, 4);table.getModel().setValueAt(subDocs.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);tab

44、le.getModel().setValueAt(null, i, 3);table.getModel().setValueAt(null, i, 4);table.getModel().setValueAt(null, i, 5);table.updateUI();table.repaint();public DefaultMutableTreeNode getSelectedNode() return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();class DocsTableModel extends Abstr

45、actTableModel private String headName;private Object obj;private Class columnTypes = 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 = he

46、adName;this.obj = obj;public int getColumnCount() return headName.length;public 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 columnTypescolumnIn

47、dex.getClass();Overridepublic boolean isCellEditable(int rowIndex, int columnIndex) 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 = -65463

48、34664166791132L;private JButton button;private JTable table;public ButtonCellEditor(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(n

49、ew ActionListener() public void actionPerformed(ActionEvent e) ButtonCellEditor.this.fireEditingCanceled();System.out.println("Selected Column:"+ table.getSelectedColumn()+ ",row:"+ table.getSelectedRow()+ ",and filePath:"+ table.getModel().getValueAt(table.getSelectedR

50、ow(), 5););Overridepublic Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) this.button.setText("下載");return this.button;Overridepublic Object getCellEditorValue() return this.button.getText();class ButtonCellRenderer implements Table

51、CellRenderer private JButton button;public ButtonCellRenderer() this.button = new JButton();public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) this.button.setText("下載");return this.button;關(guān)于上面的TableCellRender

52、er子類和CellEditor子類,這兩個都被設(shè)置到table的ColumnModel中了, ButtonCellRenderer在每次GUI渲染的時候都會調(diào)用,ButtonCellEditor只會在點擊到這個Cell的時候才會調(diào)用,并且如果失去焦點后return的Component將消失,所以ButtonCellRenderer這個很重要。table.getColumnModel().getColumn(7).setCellRenderer(new ButtonCellRenderer();table.getColumnModel().getColumn(7).setCellEditor(n

53、ew ButtonCellEditor(table); 可以在ButtonCellRenderer中加上一些邏輯,用來判斷是否顯示按鈕,比如在初始化的時候不要顯示按鈕,比如可以可以根據(jù)隱藏列的值顯示按鈕,只要修改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拖拽的事件響應(yīng)器DocsTreeTransferHanlder,TransferHanlder用來處理Transferable一個Swing Component對象上的transfer to和from,就是拖拽,而Transferable對象代表通過剪切到,拷貝自剪貼板上的數(shù)據(jù)對

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論