Java development notes (one hundred forty-one) JavaFX lists and tables

Only drop-down box will pop up when you click the drop-down list of all the options, which of course saving the limited screen space, but sometimes they need to show all options are fixed to the window. Like this tiled list control, the control is given the name of Swing ListBox, and JavaFX provides a list view ListView. In the specific coding application, ListView usage ComboBox with almost exactly the same, both list items have the same source data, the same call setStyle method to set the font, and select the listener list items remain the same, the only difference will be is the name of the control from the ComboBox into a ListView.
Since the use of the ListView and ComboBox identical, there is no longer wordy, still fast food list, for example, see if the following ListView using code fragment:

	// Get a list of interface 
	Private void getListView (BorderPane borderPane) { 
		the VBox VBox the VBox new new = (); // create a vertical box 
		// initialize snack list 
		List <String> snackList = Arrays.asList ( " fish-flavored pork rice" "mushrooms slippery chicken rice", "black pepper steak rice", 
				"pickled pork rice", "sweet and sour pork rice", "braised pork rice", "desktop braised pork rice"); 
		// convert the inventory object JavaFX can be recognized as control data objects 
		ObservableList <String> obList = FXCollections.observableArrayList (snackList); 
		the listView <String> = new new listView the listView <String> (obList); // Create a list view based on the specified data 
		//listView.setItems ( obList); // set the data source list view 
		listView.setPrefSize (400, 180); // set the width and height of the recommended list view 
		label label = new label ( "ordering result here"); // Create a tag 
		label .setWrapText (true);// Set the text wrap support 
		vbox.getChildren () addAll (listView, label );. // added to the list and the vertical box with label// Set the label text supports word wrap 
		// set list view selection listener 
		listView.getSelectionModel (). SelectedItemProperty (). AddListener (new new the ChangeListener s <String> () { 
			@Override 
			public void changed (ObservableValue <? The extends String> arg0, old_str String, String new_str) { 
				// getSelectedIndex method available number of the selected item, getSelectedItem available methods target the selected item 
				String desc = String.format ( "% d of your point of entry, fast food is the name S%", 
						listView.getSelectionModel (). getSelectedIndex ( ), 
						listView.getSelectionModel () getSelectedItem () toString ());.. 
				label.setText (desc); // display the currently selected item in the text on the label 
			} 
		}); 
		borderPane.setCenter (VBox); // the vertical boundary box into a central pane 
	}

 

Run the code that contains the above test procedures, the pop-up window, click on the button "Display List", the central interface shows the fast food list view straightforward, as shown below left. Click on the list of some kind of fast food, the bottom window displays the results of the current selection, this time as the effect of the interface shown below at right.


Despite all the options ListView can be listed on the interface, but each line of text only current option can not be combined to show richer information. For example, the restaurant ordering window, in addition to the name of fast food, fast food prices should also show, this way, the entire list is more like a form of fast food, not only branches and points out, it seems in order. JavaFX aims to provide a corresponding grid control named TableView, but because the tables embedded structured information, table of contents require special data entities. In terms of the data structure of the snack, it is assumed that the serial number, the name of a snack, snack price of these three fields, a snack bar will have pre-defined information such as the following:

// definition of fast food 
public class the Snack { 
	Private SimpleStringProperty xuhao; // number 
	private SimpleStringProperty name; // name snack 
	private SimpleStringProperty price; // Price snack 

	public the Snack (xuhao String, String name, String. Price) { 
		this.xuhao new new = SimpleStringProperty (xuhao); 
		this.name = new new SimpleStringProperty (name); 
		this.price new new SimpleStringProperty = (. price); 
	} 

	public String getXuhao () {// Get Reference 
		return xuhao.get (); 
	} 
	public void setXuhao (String xuhao ) {// set number 
		this.xuhao.set (xuhao); 
	} 
	public String getName () {// Get the name of a snack 
		return name.get (); 
	} 
	public void the setName (String name) {// set the name of a snack 
		this.name.set (name); 
	} 
	public String the getPrice () {// Get Price snack 
		return price.get (); 
	} 
	public void setPrice (String. price) { // set the fast food price 
		this.price.set (. price); 
	} 
}

Note that the above-defined fields snack attributes, their data types as a table dedicated SimpleStringProperty, only the type of data to be automatically filled into the table cell.

Defined the fast-food category of information, the next and then operating table control TableView, use the control includes the following two steps:
1, the specified data source table view
first create a list of objects snack list, and convert them to be able to identify the JavaFX ObservableList objects, and then create a table based on the view data object, code fragment corresponding to the following example:

		// create a table of contents list 
		List <Snack> snackList = Arrays.asList ( 
				new new Snack ( "1", "fish-flavored pork rice", "16"), 
				new new Snack ( "2", "mushroom chicken slip" , "18 is"), 
				new new the Snack ( ". 3", "Black pepper steak rice", "20 is"), 
				new new the Snack ( ". 4", "pickled pork rice", ". 17"), 
				new new the Snack ( ". 5 "," sweet and sour pork rice ",". 19 "), 
				new new the Snack ( '. 6", "braised ribs rice", ". 17"), 
				new new the Snack ( ". 7", "desktop braised pork rice", "15") ); 
		// convert the inventory object to JavaFX controls to identify data objects 
		ObservableList <Snack> obList = FXCollections.observableArrayList (snackList); 
		tableView <Snack> tableView = new new tableView <Snack> (obList); // create the basis for the specified data table view

 

2, creates the header of each column, and each column of the attribute object associated
cell content Source previous step contains only a table, not including the header row header. Header row by TableColumn need another statement, how many columns the table, you have to declare how many TableColumn objects, the object not only defines the title text when the column also specifies which property when the property name and contents of the column is taken from the first step field name data objects consistent. Fast food information, for example, fast-food objects have the serial number, the name of fast food, fast food price three fields, then give the fast-food allocation table three columns, and these three cell values were taken from the three fields Snack class (xuhao, name , price). Examples of the header and the code written in its associated attributes as follows:

		TableColumn firstColumn = new TableColumn ( "No."); // Create a table column 
		firstColumn.setMinWidth (100); // set the minimum width of the column 
		// Set the attribute value corresponding to the column name. Here the number row to show Snack elements xuhao property values 
		firstColumn.setCellValueFactory (new new PropertyValueFactory <> ( "xuhao")); 
		the TableColumn = new new secondColumn the TableColumn ( "fast food Name"); // Create a table column 
		secondColumn.setMinWidth (200 ); // set the minimum width of the column 
		// set the attribute value corresponding to the column name. Here the name of the column to show Snack element name attribute value 
		secondColumn.setCellValueFactory (new new PropertyValueFactory <> ( "name")); 
		the TableColumn = new new thirdColumn the TableColumn ( "fast food price"); // Create a table column 
		thirdColumn.setMinWidth (110 ); // set the minimum width of the column 
		// set the attribute value corresponding to the column name. Here the price column to show an element of price Snack property values 
		thirdColumn.setCellValueFactory (new PropertyValueFactory <>
		tableView.getColumns().addAll(firstColumn, secondColumn, thirdColumn);

The above two steps to be integrated to form a complete table during operation, the combined view of table calling code is shown below:

	// Get the table interface 
	Private void getTableView (BorderPane borderPane) { 
		the VBox VBox the VBox new new = (); // create a vertical box 
		// create a table of contents list 
		List <the Snack> snackList = Arrays.asList ( 
				new new the Snack ( ". 1 "," fish-flavored pork rice "," 16 "), 
				new new the Snack (" 2 "," chicken mushroom slippery "," 18 is "), 
				new new the Snack (". 3 "," Black pepper steak rice "," 20 "), 
				new new the Snack (". 4 "," pickled pork rice ",". 17 "), 
				new new the Snack (". 5 "," sweet and sour pork rice ",". 19 "), 
				new new the Snack (". 6 "," braised ribs rice ",". 17 "), 
				new new the Snack (". 7 "," desktop braised pork rice "," 15 ")); 
		// converts JavaFX inventory control object can be identified data object 
		ObservableList <Snack> obList = FXCollections.observableArrayList (snackList); 
		the TableView <the Snack> the TableView the tableView new new = <the Snack> (obList); // Create a table view of the data according to the specified  
		//tableView.setItems(obList); // table view is provided a data source
		tableView.setPrefSize (400, 210); // set the width and height of the table view Recommended
		TableColumn firstColumn = new TableColumn ( "No."); // Create a table column 
		firstColumn.setMinWidth (100); // set the minimum width of the column 
		// Set the attribute value corresponding to the column name. Here the number row to show Snack elements xuhao property values 
		firstColumn.setCellValueFactory (new new PropertyValueFactory <> ( "xuhao")); 
		the TableColumn = new new secondColumn the TableColumn ( "fast food Name"); // Create a table column 
		secondColumn.setMinWidth (200 ); // set the minimum width of the column 
		// set the attribute value corresponding to the column name. Here the name of the column to show Snack element name attribute value 
		secondColumn.setCellValueFactory (new new PropertyValueFactory <> ( "name")); 
		the TableColumn = new new thirdColumn the TableColumn ( "fast food price"); // Create a table column 
		thirdColumn.setMinWidth (110 ); // set the minimum width of the column  
		// set the attribute value corresponding to the column name. Here the price column to show an element of price Snack property values
		thirdColumn.setCellValueFactory (new new PropertyValueFactory <> ( ". price")); 
		// add together the number of header row to a table view
		tableView.getColumns () the addAll (firstColumn, secondColumn, thirdColumn);. 
		vbox.getChildren () the Add (the tableView);. // added to form a vertical box 
		borderPane.setCenter (vbox); // put into vertical boundary box the center pane 
	}

 

Run the code that contains the above test procedures, the pop-up window, click on the button "Display table", presents a rich snack table on the interface, the specific results as shown below.

 



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

Guess you like

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