[Java] jtable component constructor and setting column width

Source: Internet
Author: User
Document directory
  • Use jtable Components
Use the jtable component class hierarchy chart:
java.lang.Object--java.awt.Component--java.awt.Container--javax.swing.JComponent--javax.swing.JTabel
Jtabel Constructor

Before using jtable, let's take a look at its constructor and how to use it:
Jtable (): Creates a new jtables and uses the default model of the system.
Jtable (INT numrows, int numcolumns): creates an empty table with numrows rows and numcolumns columns, which uses defaulttablemodel.
Jtable (object [] [] rowdata, object [] [] columnnames): Creates a table that displays two-dimensional array data and displays the column name.
Jtable (tablemodel DM): Creates a jtable with the default field mode and selection mode, and sets the data mode.
Jtable (tablemodel DM, tablecolumnmodel cm): Creates a jtable, sets the data mode and field mode, and has the default mode.
Jtable (tablemodel DM, tablecolumnmodel cm, listselectionmodel sm): Creates a jtable and sets the data mode, field mode, and selection mode.
Jtable (vector rowdata, vector columnnames): Creates a data table with a vector as the input source to display the name of the row.
We first use the Array Construction Method to illustrate how to use jtable to create a simple table:

Package COM. jtable; import javax. swing. *; import Java. AWT. *; import Java. AWT. event. *; import Java. util. *; public class simpletable {public simpletable () {jframe F = new jframe (); object [] [] playerinfo = {"Stay", new INTEGER (66 ), new INTEGER (32), new INTEGER (98), new Boolean (false)}, {"Stay", new INTEGER (82), new INTEGER (69 ), new INTEGER (128), new Boolean (true) },}; string [] names = {"name", "Chinese", "Mathematics", "total score ", "Pass"}; jtable table = new jtable (playerinfo, names); table. setpreferredscrollableviewportsize (new dimension (550, 30); jscrollpane scrollpane = new jscrollpane (table); F. getcontentpane (). add (scrollpane, borderlayout. center); F. settitle ("simple table"); F. pack (); F. show (); F. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}) ;}public static void main (string [] ARGs) {simpletable B = new simpletable ();}}

The table consists of two parts: the column header and the column object. The row header is obtained using the gettableheader () method provided by jtable.

In this example, we place the jtable in jscrollpane. This way we can display the complete column header and colmn object, because jscrollpane will automatically obtain the column header.

However, if the readers of the literary world remove the above 15th rows and modify the 16th rows:

f.getContentPane().add(table,BorderLayout.CENTER);

Then, you will find that the column header is missing.
If you do not want to use jscrollpane, to solve this problem, you must modify the program as follows:

JTable table=new JTable(p,n);table.setPreferredScrollableViewportSize(new Dimension(550,30));f.getContentPane().add(table.getTableHeader(),BorderLayout.NORTH);f.getContentPane().add(table,BorderLayout.CENTER);

The running result will have the same line title as before. The running result above will show that the width of each field is the same, unless you pull a column width by yourself.

Set the value of column width

To set the value of the column width at the beginning, you can use the setpreferredwidth () method provided by the tablecolumn class.

You can also use the setautoresizemode () method provided by the jtable class to set the variation of other column widths when adjusting a column width.

Let's look at the example below:

Package COM. jtable; import javax. swing. *; import javax. swing. table. *; import Java. AWT. *; import Java. AWT. event. *; import Java. util. *; public class simpletable2 {public simpletable2 () {jframe F = new jframe (); object [] [] P = {"Stay", new INTEGER (66 ), new INTEGER (32), new INTEGER (98), new Boolean (false), new Boolean (false)}, {"Stay", new INTEGER (82 ), new INTEGER (69), new INTEGER (128), new Boolean (true), new Boolean (false) },}; string [] n = {"name ", "language", "Mathematics", "total score", "pass", "cheat"}; tablecolumn column = NULL; jtable table = new jtable (p, n); table. setpreferredscrollableviewportsize (new dimension (550, 30); table. setautoresizemode (jtable. auto_resize_subsequent_columns); // use the getcolumnmodel () method in jtable to obtain the tablecolumnmodel object; Use the tablecolumnmodel interface // getcolumn () method to retrieve the tablecolumn object and use setpreferredwidth () of this object () method to Control the width of the field. for (INT I = 0; I <6; I ++) {Column = table. getcolumnmodel (). getcolumn (I); If (I % 2) = 0) column. setpreferredwidth (150); elsecolumn. setpreferredwidth (50);} jscrollpane scrollpane = new jscrollpane (table); F. getcontentpane (). add (scrollpane, borderlayout. center); F. settitle ("simple table"); F. pack (); F. show (); F. setvisible (true); F. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}) ;}public static void main (string [] ARGs) {New simpletable2 ();}}

Five adjustable column parameters:

Auto_resize_subsequent_columens: When the width of a column is adjusted, the column width of all fields after this field changes. This is the default value. Auto_resize_all_columns: When the width of a column is adjusted, the column width of all fields in this table will change together. Auto_resize_off: When you adjust the width of a column, the column width of all fields in this table will not change. Auto_resize_next_column: When the width of a column is adjusted, the column width of the next field of this field will change, and the rest will not change. Auto_resize_last_column: When the width of a column is adjusted, the column width of the last field will change, and the rest will not change.

The above example shows that it is very easy to construct a table using swing. As long as you use vector or array as the data input of our table, you can fill in the content of vector or array in jtable, A basic table is generated.

However, although jtable (object [] [] rowdata, object [] [] columnnames) and jtable (vector rowdata, vector columnnames) constructors can be used to construct jtable, but there are some disadvantages.

For example, in the previous example, each field (cell) in our table is modifiable by default at the beginning, and users may modify the data;

Second, the Data Types in each cell in the table will be treated as the same. In our example, the data type is displayed as the string type.

Therefore, data declared as Boolean in the original data type will appear in the form of a string instead of a check box.

In addition, if the data we want to display is not fixed, or changes as the situation changes, for example, it is also a transcript, the tables shown by the teacher and the students will not be the same, the display appearance or operation mode may also be different.

To cope with these complex situations, the preceding simple constructor is no longer suitable. Swing provides various models (such as tablemodel, tablecolumnmodel, and listselectionmodel) to solve the preceding inconvenience, to increase the flexibility of our design tables.

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.