Set the JTABLE option when running

zhaozj2021-02-08  238

Set the JTABLE option when running

Overview The JTABLE class provides a rich grid data display feature. This technique handles issues related to dynamic changes such as rolling bars, columns and high-level appearances. (900 Words; September 14, 2001) (September 20,2001)

Java Basics (JFC) provides a rich component selection for creating a graphical user interface (Guis) of a delicate and interactive. You can use javax.swing.jtable to display data in the table. In this trick, we have studied how JTABLE is a common problem.

Let us first define an initial, basic JTABLE class - MYTABLE: Import javax.swing.Table. *; Import javax.swing. *; Import java.awt. *; Public class myTable Extends jtable {// default construction Function public myTable () {super ();} // uses the specified rantric number to create a table constructor PUBLIC MyTable (Int row, int co) {Super (Row, Col);}}

Quite simple! Our initial MyTable implementation is a normal JTABLE.

In the next section, we will be committed to different JTABLE display options - such as scroll bars, column width, selectivity, and other properties. We will extend MyTable and merge some different ways to support it to support the display characteristics we want. Each section adds a new method to myTable, and finally we get a fully reused JTable.

Rolling Table First, let our JTable display some table format data. I created TableColumnTest demonstration JTABLE's ability:

Import javax.swing.table. *; import javax.swing. *; import java.awt.event. *; import java.awal, slum @hotmail.com*/public class TableColumnTest { Protected Jframe Frame; Protected JscrollPane Scrollpane; Protected MyTable Table; Public TableColumnTest () {// (1) Create a Table Model. DefaultTableModel DM = New DefaultTableModel (); // Name String [] ColumnNames = {"this is going to be a really long column header", "Column B", "Column C", "Column D", "Column D", "Column D", "Column D", "Column D", "Column D", "Column D", "Column D", "Column D" E "," Column F "," Column H "," Column H "," Column I "," Column J "}; // Real Data Value Integer [] [] Data = New Integer [8] [10] ; // Initialize the data matrix. For (int ROW = 0; ROW <8; ROW ) {for (int coL = 0; Col <10; COL) {data [ROW] [col] = new integer (1000000);}} // setting model And data and column association DM.SetDataVector (data, columnnames); // (2) Create a table table = new myTable (); // (3) Connection model and table Table.SetModel (DM); // (4) Create a scroll panel scrollpane = new jscrollpane (Table) for the table; // (5) Display table frame = new jframe (); frame.getContentPane (). Add (scrollpane); frame.setsize (200, 150); frame.setVisible (TRUE); public static void main (string [] args) {TableColumnTest Test = new TableColumnTest ();

Demo procedures are quite simple. We constructed a simple JTable in the following steps:

· Create and set TableModel, which contains information about rows, columns, columns, and data data

Create and set up a JTABLE, which displays data according to the model

Associate the model created by JTABLE and the first step

But there is a misinterlete in this code: the fourth step is added to a scroll panel. We will construct the table in a JFrame inside; see Figure 1

Figure 1. Messy scrolling

Just like Figure 1, it is difficult to distinguish any list or table data. Although we increase the scroll bar, the horizontal scroll bar is not displayed. Let's take a detailed view of JTABLE to reveal the reason. JTable has an automatically resized mode, which will determine if the table is automatically adjusted (to overwrite the entire width of the table) and how to adjust. You can use the following value: · Auto_Resize_off: Do not automatically adjust the column width; use the scroll bar

Auto_RESize_next_column: When a column is adjusted, the reverse adjustment next column

Auto_RESize_SUBSEQUENT_COLUMNS: Change the subsequent columns to maintain the total width during adjustment

Auto_RESize_LAST_COLUMN: Only adjustments to the last column on all adjustments

Auto_RESize_all_columns: Adjust all columns in all adjustments operations.

By default, JTABLE adjusts other columns to maintain the total appearance, just like Figure 1 illustrated. So if we want to use a horizontal scroll bar display column, we need to add a method to MyTable and call it in the constructor:

/ ** This method shows horizontal scroll bar when needed

* Two constructors are invoked * / public void showHorScroll (boolean show) {if (show) {setAutoResizeMode (JTable.AUTO_RESIZE_OFF);} else {setAutoResizeMode (JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);}}

Figure 2 shows a visible horizontal scroll bar:

Figure 2. Visible horizontal scroll bar

Control JTABLE list You can control the width of the column or make them uncomfortable, this section shows how to do it.

Widening columns typically you want a column that will be another wider or narrower. To change the width of the column, you can use TableColumnModel:

/ ** This method sets the width of the column indicated by PCOLUMN to PWIDTH * / PUBLIC VOIDTCOLUMNWIDTH * / PUBLIC VOIDTCOLUMNWIDTH * / PUBLIC VOIDTCOLUMNWIDTH (INT PCOLUMN, INT PWIDTH) {// Get Column Style TableColumnModel Colmodel = getColumnModel (); // Get PCOLUMN columns and then set its most Jia Guan Colmodel.getColumn (Pcolumn) .SetPreferRedwidth (PWIDTH);

You can also add a button to jframe and its listener, click the button to change the width of the table:

JButton resizeButton = new JButton ( "Resize Third Column"); setResizeButton.addActionListener (this); public void actionPerformed (ActionEvent e) {// check which a button is clicked if (e.getActionCommand () equals ( "Resize Third. ")") ") {System.out.println (" Resize Called - Resizes Third Column "); Table.SetColumnWidth (2, 300); // Force GUI refresh Table.inValidate (); frame.invalide (); frame In this example, PColumn is the index value of the column, and the PWIDTH is a new column width. The effect of clicking the Resize button is shown as Figure 3 and 4.

Figure 3. Clicking

Figure 4. Click

Unscrupulous columns For usual applications, you can drag the size of the column adjustment column. The following code sets the table's adjustability according to the value of PisResize. If PisResize is true, the column width can be adjusted; otherwise you can't:

Public void setResizable (int Pcolumn, Boolean Pisresize) {// Get column TableColumnModel Colmodel = getColumnModel (); // Setupable colmodel.getColumn (PCOLUMN) .SetResizable (Pisresize);

In this example, PCOLUMN is an index value that is not adjustable. Get column (GetColumn (.)) And set up a simple property (setResizable (..)) is all to do.

Why is the selectability of the column not to select the entire column when the button is clicked, not just a unit? JTABLE displays whether the unit is selected by the ISCellSelected (int Row, INT COL) method of the calling unit. Overloading this method, you can give you the results, the result, and the parameter Select of the setSelect (int COL, Boolean Select). If false, it will not be selected. The key is to set the column to colselect (), indicate that the column is selected or canceled with the "SELECT" tag:

INT colselect; / ** Set column col for selecting or uncheck * - Decide * / public void setSelect (INT COL, Boolean select) {color = col;} / ** This = SELECT;} / ** method returns whether a cell is selected * / public boolean isCellSelected (int row, int column) throws IllegalArgumentException {// overloaded verride the method for the column set in setSelect () if (colSelect == column) {if (select) Else Return False;} else {return super.iscellselected (row, column);}} Figure 5 shows the results selected by Column D.

Figure 5. Select the entire column

The control list You may notice that the length of the first column list is larger than the column. We deal with this:

/ ** Get the name of the specified column String value = getColumnName (PCOLUMN) = getColumnName (PColumn); // Calculate FontMetrics Metrics = GetGraphics () .getfontmetrics (); int width = metrics.StringWidth (Value) (2 * getColumnModel (). getColumnMargin ()); // Setting the width setColumnWidth (Pcolumn, width);

The result of the upper code is executed after the reset list is shown in FIG.

Figure 6. Completely visible list

Features rich in JTable In this trick, we tes different options on simple JTABLE and change those options after the form is displayed. During this process, we have developed a table that provides rich user interface capabilities. Studying other properties of JTable's to see what kind of interesting form you can create!

转载请注明原文地址:https://www.9cbs.com/read-1907.html

New Post(0)