In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how TableModelExample simplifies Swing". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how TableModelExample simplifies Swing.
The purpose of this example is to point out the main advantages of using the TMF framework instead of traditional TableModel design. The application in the example displays multiple tables on the screen and can add or delete tables that can contain different types of information (String type, int type, Boolean type, and BigDecimal type) and, most importantly, configurable column information that must be changed periodically.
The code for the sample application is separated from the J2X package, and you can find the source code in the src directory of the HR folder. You can also double-click the compiled JAR file in the build/lib file to run the application through JRE.
In the sample application, there are two classes that can be exchanged, one called TableModelFreeExample and the other called TableModelExample. These two classes do the same thing in the application, causing the application to produce the same behavior. However, they are designed differently, one using the TMF framework and the other using the traditional TableModel. The only thing you notice from them may be the TMF class TableModelFreeExample, which consists of 63 lines of code, compared with 285 lines in the traditional TableModel version of TableModelExample.
Evil HR Director application
The example application I'm going to use is the Evil HR Director application, which allows the director of human resources (possibly scary, bespectacled) to look at a list of potential employees in JTable and then select the people to be hired from the table. The data of the newly hired employee is transferred to the two JTable used by the current employee; one table contains personal information and the other table contains financial information. In the current employee table, the director is free to choose who to fire. You can see the UI of the application in figure 1.
Figure 1. Evil HR Director application
To further demonstrate the simplicity of the TMF framework, take a look at listing 6. This listing contains only three lines of required code to create a model of the three tables contained in the Evil HR Director application. This code can be found in TableModelFreeExample.
Listing 1. The code required to create a model in an Evil HR Director application
TableUtilities.setViewToModel ("demo/hr/resources/evil_hr_table.xml", "Hire", hireTable, candidates); TableUtilities.setViewToModel ("demo/hr/resources/evil_hr_table.xml", "Personal", personalTable, employees); TableUtilities.setViewToModel ("demo/hr/resources/evil_hr_table.xml", "Financial", financialTable, employees)
For comparison purposes, TableModelExample contains the code needed to model the three tables using traditional TableModel methods. Look at the code in the sample package. However, I don't want to list all the code here, because it has 205 lines!
Demonstrate the flexibility of the TMF framework
One of the great advantages of the TMF framework is that it makes it easier for JTable-based applications to modify after their release. To prove this, let's look at two possible scenarios that can occur every day in an Evil HR Director application. In each scenario, you will see how the framework makes it easier for applications to adapt to changing user needs.
Scenario 1: the company's policy has changed and it is illegal to view private marriage information in the company's application.
TMF: the end user needs to remove Married?married from the XML configuration file.
Traditional TableModel: developers must delve into the Java code and modify getColumnName () so that it cannot return the column name "Married?"; modify getColumnCount () so that it returns one column less than previously returned; and modify getValueAt () so that it does not return isMarried (). The developer must then recompile the Java code and redeploy the application.
Scenario 2: the company's strategy changes and the company feels the need to include information about the state where it resides in the potential employee table.
TMF:: end users need to add Statestate to the XML configuration file.
Traditional TableModel: developers must delve into the Java code, modify getColumnName (), add a new column called "State", modify getColumnCount () to add 1 to the number of columns it returns, and modify getValueAt () so that it returns getState (). The developer must then recompile the Java code and redeploy the application.
You can see that editing the XML file is much easier than redeploying the entire application when the tables in the application change (especially when you have a boss who is always constantly changing).
Use the code
Before you rush over to delete all the TableModel code, I think I'll take a minute to explain the contents of the j2x.zip file and how you can use it in your own projects. Keep in mind that TMF-specific code can be found in the com.ibm.j2x.swing.table package; you will also find my previous article "Go state-of-the-art with IFrame." in the J2X package. Other code described in.)
The j2x.zip file contains two upper folders:
Src-- contains the source code used in this article. Within the src folder, there are also two folders: one is HR, which contains the source code that makes up the Evil HR Director application, and the other is J2X, which contains all the source code used in J2X projects.
Build-- contains the compiled class files of the Evil HR Director application and the J2X project. The lib folder in this folder contains the JAR files for HR applications and J2X projects.
The lib.zip file contains the following folders:
Lib-- contains all the third-party JAR files that are required to run applications or any project that uses J2X projects. In this folder, you will also find licenses for third-party projects.
The docs.zip file contains the following folders:
Docs-- contains all the JavaDoc information for the J2X project.
To use the J2X package in your application, you need to point the CLASSPATH to the j2x.jar in the build/lib folder and all three third-party JAR files contained in the lib file. The license terms of third-party packages allow you to redistribute all packages included in this article, but if you are interested in making changes to these packages, please read the license terms.
At this point, I believe you have a deeper understanding of "how TableModelExample simplifies Swing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.