Teach you how to use List to transfer student information
Collections are often used in program development. For example, in business methods, student information, product information, etc. are stored in collections, and then returned to the caller as the return value of the method, thereby passing a large amount of ordered data.
This example will use the List collection to transfer student information between methods. The example effect is as follows:
Design process
1) Create a new form class ClassInfo in the project. Add JScrollPane to the form, and then place the table control JTable in the JScrollPane.
2) Write the getTable() method. In this method, create a table object and set the data model of the table, and then call the getStudents() method to obtain the collection object of student information.
While traversing the collection object, add each element to the table model rows and displayed in the table control.
1 private JTable getTable() { 2 if (table == null) { 3 table = new JTable();// 创建表格控件 4 table.setRowHeight(23);// 设置行高度 5 String[] columns = { "姓名", "性别", "出生日期" };// 创建列名数组 6 // 创建表格模型 7 DefaultTableModel model = new DefaultTableModel(columns, 0); 8 table.setModel(model);// 设置表格模型 9 List<string> students = getStudents();// 调用方法传递list集合对象 10 for (String info : students) {// 遍历学生集合对象 11 String[] args = info.split(",");// 把学生信息拆分为数组 12 model.addRow(args);// 把学生信息添加到表格的行 13 } 14 } 15 return table; 16 }</string>
3) Write the getStudents() method, which will pass the List collection object to the caller and add multiple elements to the collection object. Each element value is a student information. , which includes name, gender, and date of birth.
1 private List<string> getStudents(){ 2 //创建List集合对象 3 List<string> list = new ArrayList<string>(); 4 list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开 5 list.add("孙西,女,1990-1-4"); 6 list.add("周五燕,男,1990-1-4"); 7 list.add("王东,女,1990-1-4"); 8 list.add("冯晓,男,1990-1-4"); 9 list.add("陈玉山,女,1990-1-4"); 10 list.add("蒋钦大,男,1990-1-4"); 11 return list; 12 13 }</string></string></string>
Full code
1 package cn.str.opera; 2 3 import java.awt.BorderLayout; 4 5 public class ClassInfo extends JFrame { 6 7 private JPanel contentPane; 8 private JTable table; 9 10 /** 11 * Launch the application. 12 */ 13 public static void main(String[] args) { 14 15 try { 16 UIManager 17 .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 18 } catch (Throwable e) { 19 e.printStackTrace(); 20 } 21 EventQueue.invokeLater(new Runnable() { 22 public void run() { 23 try { 24 ClassInfo frame = new ClassInfo(); 25 frame.setVisible(true); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 }); 31 } 32 33 /** 34 * Create the frame. 35 */ 36 public ClassInfo() { 37 getContentPane().setLayout(new BorderLayout(0, 0)); 38 setTitle("\u7528List\u96C6\u5408\u4F20\u9012\u5B66\u751F\u4FE1\u606F"); 39 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 40 setBounds(100, 100, 450, 300); 41 contentPane = new JPanel(); 42 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 43 contentPane.setLayout(new BorderLayout(0, 0)); 44 setContentPane(contentPane); 45 46 47 JScrollPane scrollPane = new JScrollPane(); 48 contentPane.add(scrollPane,BorderLayout.CENTER); 49 scrollPane.setViewportView(getTable()); 50 51 } 52 53 private JTable getTable() { 54 if(table == null){ 55 table = new JTable();//创建表格控件 56 table.setRowHeight(23); 57 String[] columns = {"姓名","性别","出生日期"};//创建列名数组 58 //创建表格模型 59 DefaultTableModel model = new DefaultTableModel(columns,0); 60 table.setModel(model);//设置表格模型 61 List<string> students = getStudents(); 62 for(String info:students){// 遍历学生集合对象 63 String[] args = info.split(",");// 按英文逗号,把学生信息拆分为数组 64 model.addRow(args); 65 } 66 67 } 68 return table; 69 } 70 71 private List<string> getStudents(){ 72 //创建List集合对象 73 List<string> list = new ArrayList<string>(); 74 list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开 75 list.add("孙西,女,1990-1-4"); 76 list.add("周五燕,男,1990-1-4"); 77 list.add("王东,女,1990-1-4"); 78 list.add("冯晓,男,1990-1-4"); 79 list.add("陈玉山,女,1990-1-4"); 80 list.add("蒋钦大,男,1990-1-4"); 81 return list; 82 83 } 84 85 }</string></string></string></string>
The above is the detailed content of Teach you how to use List to transfer student information. For more information, please follow other related articles on the PHP Chinese website!

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

About SpringCloudAlibaba microservices modular development using SpringCloud...

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

JDBC...

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)