Java development notes (one hundred thirty-two) Swing table

Some combination arrangement previously described simple controls on the program interface, which information is used to contact each other weaker expression down okay, and if used to express strong association information is polymerized insufficient. If just a list of simple information, such as product name list, a list of news headlines, such as the list of names of students, they can still use the list box JList be demonstrated; if the required information listed complex arrangement, such as a list of goods orders, news category list, a list of student achievement and so on, as there is a list of information that a number of details can not be expressed through the list box, but it should be described by Swing table type JTable.
JTable as its name says, its appearance is the branch of disaggregated form, each row is a complete information, and the details of each column is the parameter information. Similar to the list box, before building a Grid control, you have to be initialized as a model object information carrier. JTable with table model named partner DefaultTableModel, the information it contains is divided into two parts, one is the title information table, and the other part is the content information table, thus requiring an array of title and contents of the array are assigned to the table, and then to build from these tables contain information model. Building Code table model specific examples are as follows:

		// Create the table headers array 
		String [] heads = new String [ ] { " number", "Package Name", "package price"}; 
		// create a table of contents of the array 
		Object [] [] values = new Object [] [] { 
				{ "1", "fish-flavored pork rice", "16"}, 
				{ "2", "chicken mushroom slippery", "18"}, 
				{ "3", "Black pepper steak rice", "20"}, 
				{ "4", "pickled pork rice", "17"}, 
				{ "5", "sweet and sour pork rice", "19"}, 
				{ "6", "braised ribs rice" , "17"}, 
				{ "7", "desktop braised pork rice", "15"} 
		}; 
		// the title array and the contents of the array, creating the default table model 
		DefaultTableModel model = new DefaultTableModel (values, heads);

 

With the table model, model objects can be passed in the constructor JTable in order to successfully create a table object. Table creation code object is as follows:

		JTable table = new JTable (model); // create the table according to the model

 

To adjust the appearance of the table, you can call the following method to set the table object:
setFont: Set the table of contents of the text font.
setGridColor: set the color of the grid lines.
setShowGrid: whether to display grid lines. Default Display.
setShowHorizontalLines: whether to show horizontal dividing line. Default Display.
setShowVerticalLines: whether vertical dividing line. Default Display.
setRowHeight: Set the height of each row.
setEnabled: if editing is allowed. Allowed by default.
setAutoResizeMode: setting auto-resize mode. To show the horizontal scroll bar, will have to be set to turn off automatically adjusted.
The above method is mainly for the table of contents styles, in addition, there are three other properties to be additionally provided, including: Table header attributes, alignment of the table of contents, a table column properties. Next were deployed briefly.
1, the table header attributes
for table headers JTableHeader, the need to call getTableHeader method of the table object to obtain the title object, and then call the following method to adjust the title of the title object attributes:
setFont: set the text font header row.
setResizingAllowed: whether to allow changing the width by dragging the title of each column. Allowed by default.
setReorderingAllowed: whether to allow to change the order by dragging between column to column. Allowed by default.
Here is the code example to adjust the table headings related attributes:

		JTableHeader header = table.getTableHeader (); // get the head of the table (i.e., header row) 
		header.setFont (font); // set the font of the title text line 
		// Allow changing the width by dragging the title of each column. Allowed by default 
		header.setResizingAllowed (to false); 
		// change the order by dragging is allowed between the rows and columns. Allowed by default 
		header.setReorderingAllowed (false);

 

2, alignment of the table of contents
for the alignment of the table of contents, the first unit to create a table renderer, and renderer are disposed aligned on both horizontal and vertical directions, and then call a method setDefaultRenderer table object, in order to complete table of contents of the alignment operation. Provided the corresponding code is as follows:

		// create a default table cell renderer 
		DefaultTableCellRenderer The DefaultTableCellRenderer The new new = the render (); 
		// set renderer alignment in the horizontal direction. Default Left align 
		render.setHorizontalAlignment (JLabel.CENTER); 
		// set renderer alignment in the vertical direction. Default vertically centered 
		render.setVerticalAlignment (JLabel.CENTER); 
		// Set default table renderer 
		table.setDefaultRenderer (Object.class, render);

 

3, table column properties
for columns attribute table, the need to invoke a method getColumnModel table object table column model obtained, after traversing the model objects of each column, the attribute values are provided for each column, such as call set the current method setPreferredWidth recommended width of the column, and so on. The following is an example of code for adjusting the width of each column:

		// get table column model 
		the TableColumnModel that of the columnModel table.getColumnModel = (); 
		for (int I = 0; I <columnModel.getColumnCount (); I ++) {// iterate each column model 
			column object // Get the specified location 
			TableColumn column columnModel.getColumn = (I); 
			// set the recommended width of the column. Only in the closed automatic adjustment mode, the width of each column is provided to take effect 
			column.setPreferredWidth (100); 
		}

Also note, JTable does not automatically display scroll bars, scroll bars to appear in the table of contents is out of range, you need to create a JScrollPane objects like JTextArea as to bind the table object. By default only show the vertical scroll bar, if you want an automatic adjustment mechanism to showcase the horizontal scroll bar, you need to turn off the table, that is, call the table object setAutoResizeMode method to modify the mode AUTO_RESIZE_OFF. Scroll bar is added to the key code table object as follows:

		// set the auto-resize mode. To show the horizontal scroll bar, will have to be set to turn off automatically adjusted 
		table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); 
		// first binding way: create a scrollbar, fill in the table object in the constructor 
		JScrollPane scroll = new JScrollPane (the Table); 
		// the second way binding: call setViewportView method to set the scroll bar associated control 
		//scroll.setViewportView(table); 
		// bind a third way: scroll through the view port add an object method Add a table object 
		//scroll.getViewport().add(table); 
		frame.getContentPane () the Add (the scroll);. // add a scroll bar contains a table of contents on the panel window

 

The calling code table above several merged together, the combined operation of the test code, the program window can pretend to distinguish two cases the whole table, the two effects corresponding screen pop-up window will appear. FIG below wherein the first window is not large enough belongs, then appears on the right table of the vertical scroll bar; FIG belonging to the second window is sufficiently large, then the interface shows the complete table of contents, the excess does not appear scroll bar.




See more Java technology articles " Java Development Notes (order) List of chapters "

Guess you like

Origin www.cnblogs.com/pinlantu/p/11294485.html