1: Table (JTable):
1.Basic concepts:
Table (JTable) is a newly added component of Swing. It is mainly used to display data in tabular form. It provides a simple mechanism for displaying large blocks of data.
2. Commonly used construction methods:
* JTable(): Create a JTable instance using the system default model.
* JTable(int numRows, int numColumns): Create an empty table using DefaultTableModel to specify rows and columns.
* JTable(Object[ ][ ] rowData,Object[ ][ ] columnNames): Create a table that displays two-dimensional data.
Tables can be created directly using table column name arrays and table data arrays.
* JTable(TableModel dm): Create a JTable instance specifying the data schema and default field schema.
Objects of the data model class are usually used to save data, and table models are created through table column name arrays and table data arrays.
3. Commonly used methods:
4. Comprehensive case:
Code 1:
Copy the code code as follows:
public class JTableDemo1 extends JFrame {
//define table
JTable table;
//Define the scroll bar panel (to make the table scrollable)
JScrollPane scrollPane;
//Define the object of the data model class (used to save data),
DefaultTableModel tableModel;
public JTableDemo1() {
super();
setTitle("Table model and table");
scrollPane = new JScrollPane();
//Define table column name array
String[] columnNames = { "A", "B", "C" };
//Define table data array
String[][] tableValues = { { "A1", "B1","C1" }, { "A2", "B2","C2" },
{ "A3", "B3", "C3" }, { "A4", "B4", "C4" } };
//Create an object of the table model class specifying table column names and table data
tableModel = new DefaultTableModel(tableValues, columnNames);
//Create a table of the specified table model
table = new JTable(tableModel);
//Set RowSorter (RowSorter is used to provide sorting and filtering of JTable).
table.setRowSorter(new TableRowSorter<DefaultTableModel>(tableModel));
scrollPane.setViewportView(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setBounds(300, 200, 400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JTableDemo1();
}
}
Screenshot 1:
Code 2:
Copy the code code as follows:
import java.awt.*;
import javax.swing.*;
public class JTableDemo3 {
JFrame frame;
JPanel panel;
JScrollPane scrollPane1, scrollPane2, scrollPane3;
JTable jtable1, jtable2, jtable3;
public JTableDemo3() {
frame = new JFrame("JTableDemo");
jtable1 = new JTable(8, 6);
final Object[] columnNames = { "Name", "Gender", "Home Address", // Column names are best modified with final
"Phone number", "Birthday", "Job", "Income", "Marital status", "Relationship status" };
Object[][] rowData = {
{ "ddd", "Male", "Nanjing, Jiangsu", "1378313210", "03/24/1985", "Student", "Parasite",
"Unmarried", "Not" },
{ "eee", "female", "Nanjing, Jiangsu", "13645181705", "xx/xx/1985", "tutor", "unknown",
"Unmarried", "It seems not" },
{ "fff", "male", "Nanjing, Jiangsu", "13585331486", "12/08/1985", "car salesman",
"Unsure", "Unmarried", "Yes" },
{ "ggg", "female", "Nanjing, Jiangsu", "81513779", "xx/xx/1986", "hotel attendant",
"Determined but unknown", "Unmarried", "Yes" },
{ "hhh", "Male", "Nanjing, Jiangsu", "13651545936", "xx/xx/1985", "Student", "In exile",
"Unmarried", "Not after numerous breakups" } };
jtable2 = new JTable(rowData, columnNames);
jtable3 = new JTable(rowData, columnNames);
jtable3.setPreferredScrollableViewportSize(new Dimension(600, 100));//Set the size of the table
jtable3.setRowHeight(30);//Set the height of each row to 20
jtable3.setRowHeight(0, 20);//Set the height of row 1 to 15
jtable3.setRowMargin(5);//Set the distance between two adjacent rows of cells
jtable3.setRowSelectionAllowed(true); // Set whether it can be selected. The default is false
jtable3.setSelectionBackground(Color.white);//Set the background color of the selected row
jtable3.setSelectionForeground(Color.red);//Set the foreground color of the selected row
jtable3.setGridColor(Color.red);//Set the color of the grid lines
jtable3.selectAll();//Select all rows
jtable3.setRowSelectionInterval(0, 2);//Set the initial selection row, here rows 1 to 3 are all in the selected state
jtable3.clearSelection();//Cancel selection
jtable3.setDragEnabled(false); // Don’t understand this
jtable3.setShowGrid(true); // Whether to display grid lines
jtable3.setShowHorizontalLines(true); // Whether to display horizontal grid lines
jtable3.setShowVerticalLines(true); // Whether to display vertical grid lines
jtable3.setValueAt("tt", 0, 0);//Set the value of a certain cell, this value is an object
jtable3.doLayout();
jtable3.setBackground(Color.cyan);
// JTable is best added to JScrollPane to achieve scrolling effect
scrollPane1 = new JScrollPane(jtable1);
scrollPane2 = new JScrollPane(jtable2);
scrollPane3 = new JScrollPane(jtable3);
panel = new JPanel(new GridLayout(0, 1));
panel.setPreferredSize(new Dimension(600, 400));
panel.setBackground(Color.black);
panel.add(scrollPane1);
panel.add(scrollPane2);
panel.add(scrollPane3);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JTableDemo3();
}
}
Screenshot 2:
Two: Tree (JTree):
1.Basic concepts:
Tree (JTree): A control that displays hierarchical data sets as outlines.
2. Commonly used construction methods:
JTree(): Returns a JTree with the sample model.
JTree(Object[] value): Returns a JTree specifying each element of the array as a child node of the new root node that is not displayed.
//Only this constructor can create multiple root nodes
JTree(Object[] value): Returns a JTree specifying each element of the array as a child node of the new root node that is not displayed.
JTree(TreeNode root): Returns a JTree with the specified TreeNode as its root, which displays the root node.
JTree(TreeNode root, boolean asksAllowsChildren): Returns a JTree with the specified TreeNode as its root.
It displays the root node in a specified manner and determines whether the node is a leaf node. (Set true to add child nodes, and then add child nodes).
3. Code demonstration:
Example 1:
Copy the code code as follows:
public class JTreeDemo1 {
JFrame f;
Box box;
JTree jTree1,jTree2;
public JTreeDemo1()
{
f = new JFrame(" JTreeDemo1 ");
box = Box.createHorizontalBox(); //Create Box class object
jTree1 = new JTree();
jTree2 = new JTree();
//Add arbitrary keys/values to this component
jTree1.putClientProperty("JTree.lineStyle", "Angled");
//Add a scroll panel to the Box container
box.add(new JScrollPane(jTree1), BorderLayout.WEST);
box.add(new JScrollPane(jTree2), BorderLayout.EAST);
f.getContentPane().add(box, BorderLayout.CENTER);
f.setSize(300, 240);
//f.pack();
f.setLocation(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JTreeDemo1();
}
}
Screenshot 1:
Example 2:
Copy the code code as follows:
public class JTreeDemo2 {
JFrame f;
JPanel p;
JTree jTree1,jTree2,jTree3,jTree4,jTree5,jTree6,jTree7;
public JTreeDemo2() {
f = new JFrame(" JTreeDemo2 ");
// Constructor: JTree()
jTree1 = new JTree();
// Constructor: JTree(Object[] value)
Object[] letters = { " a ", " b ", " c ", " d ", " e " };
jTree2 = new JTree(letters);
// Constructor: JTree(TreeNode root)(TreeNode empty)
//Create a tree with empty nodes
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(); // Define tree nodes
jTree3 = new JTree(node1); // Use this tree node as a parameter to call the JTree constructor to create a tree with a root node
// Constructor: JTree(TreeNode root) (same as above, except that TreeNode is not empty)
// Create a tree with a root node
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(" Color ");
jTree4 = new JTree(node2); // Nodes cannot be colored, the default is white with black text
jTree4.setBackground(Color.lightGray);
// Constructor: JTree(TreeNode root, boolean
// asksAllowsChildren) (same as above, but TreeNode is different)
// Use the DefaultMutableTreeNode class to first create a tree with a root node, set it to add child nodes, and then add child nodes.
DefaultMutableTreeNode color = new DefaultMutableTreeNode(" Color ",
true);
DefaultMutableTreeNode gray = new DefaultMutableTreeNode(" Gray ");
gray.add(new DefaultMutableTreeNode(" Lightgray "));
gray.add(new DefaultMutableTreeNode(" Darkgray "));
color.add(gray);
color.add(new DefaultMutableTreeNode(" Red "));
color.add(new DefaultMutableTreeNode(" Green "));
jTree5 = new JTree(color);
// Constructor: JTree(TreeNode root) (same as above, except that TreeNode is not empty)
// Create a tree by adding nodes one by one
DefaultMutableTreeNode biology = new DefaultMutableTreeNode(" Biology ");
DefaultMutableTreeNode animal = new DefaultMutableTreeNode(" Animal ");
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode(" Mammal ");
DefaultMutableTreeNode horse = new DefaultMutableTreeNode(" Horse ");
mammal.add(horse);
animal.add(mammal);
biology.add(animal);
jTree6 = new JTree(biology);
horse.isLeaf();
horse.isRoot();
//Constructor:JTree(TreeModel newModel)
// Use the DefaultMutableTreeNodel class to define a node and then use this node as a parameter to define a node using DefaultTreeMode
// Create a tree model, and then use the JTree constructor to create a tree
DefaultMutableTreeNode root = new DefaultMutableTreeNode(" Root1 ");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(" Child1 ");
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(" Child11 ");
DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
" Child111 ");
root.add(child1);
child1.add(child11);
child11.add(child111);
DefaultTreeModel model = new DefaultTreeModel(root);
jTree7 = new JTree(model);
p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setPreferredSize(new Dimension(700, 400));
// JTree must be placed on JScrollPane
p.add(new JScrollPane(jTree1));
p.add(new JScrollPane(jTree2));
p.add(new JScrollPane(jTree3));
p.add(new JScrollPane(jTree4));
p.add(new JScrollPane(jTree5));
p.add(new JScrollPane(jTree6));
p.add(new JScrollPane(jTree7));
f.setContentPane(p);
f.pack();
f.setLocation(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JTreeDemo2();
}
}
Screenshot 2: