Java Tree and table experience

Source: Internet
Author: User

In customizing tables and trees is the model is important, the definition is never blurred

Such as:

DefaultTableModel mode = new DefaultTableModel ();

JTable table = new JTable (mode);

It is difficult for the next tree to correspond with the table in one by one, and it is not conducive to the display in JScrollPane.


Here's a good way to do it, and it's easy to do it with a tree that's compatible with the table:

DefaultTableModel mode;

JTable table = new JTable ();

mode = (DefaultTableModel mode) Table.getmodel ();


In the next program, you can change the format of the table by simply using the model;


Example:

Package tabletree;
Import java.awt.*;
Import javax.swing.*;
Import javax.swing.event.TreeSelectionEvent;
Import Javax.swing.event.TreeSelectionListener;
Import Javax.swing.table.DefaultTableModel;
Import javax.swing.tree.*;


public class Tabletree extends JPanel implements Treeselectionlistener {

/**
* @param args
*/
JPanel Myjpanel;
JTable table;
JScrollPane myjstree,myjstable;
JTree Myjtree;
Defaultmutabletreenode root,temp;
Object Nodepath;
Defaulttreemodel rs;
String strpath;
int row,col;
Treeselectionmodel Selectionmodel;
DefaultTableModel model;
JFrame Myjframe;

Public Tabletree () {

Super ();
Myjframe = new JFrame ("treetable");

Table = new JTable ();
Model = (DefaultTableModel) Table.getmodel ();
This.setlayout (New BorderLayout ());
This.add (Table,borderlayout.center);
myjstable = new JScrollPane (this);
Myjframe.add (Myjstable,borderlayout.center);


root = new Defaultmutabletreenode ("root");
temp = new Defaultmutabletreenode ("father");
Temp.add (New Defaultmutabletreenode ("Chirld1"));
Temp.add (New Defaultmutabletreenode ("Chirld2"));
Temp.add (New Defaultmutabletreenode ("chirld3"));
Temp.add (New Defaultmutabletreenode ("Chirld4"));
Temp.add (New Defaultmutabletreenode ("Chirld5"));
Root.add (temp);
rs = new Defaulttreemodel (root);

Myjtree = new JTree (RS);
Selectionmodel = Myjtree.getselectionmodel ();
Selectionmodel.setselectionmode (treeselectionmodel.single_tree_selection);
Myjtree.addtreeselectionlistener (this);
Myjstree = new JScrollPane (myjtree);

Myjframe.add (myjstree,borderlayout.west);

Myjframe.setsize (550,400);
Myjframe.setlocation (150, 150);
Myjframe.setvisible (TRUE);

Myjframe.setdefaultcloseoperation (Jframe.exit_on_close);



}


Java Tree and table experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.