Sunday, 17 June 2018

JavaFX: Hide columns of a table

By setting the visible property to false, you can hide the column of a table.

Ex
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(300);
emailCol.setVisible(false);

Find the below working application.

TableViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewApp extends Application {

	@Override
	public void start(Stage primaryStage) {
		Label label = new Label("My Friends List");
		label.setFont(new Font("Arial", 30));

		TableView tableView = new TableView();

		tableView.setEditable(true);
		TableColumn firstNameCol = new TableColumn("First Name");
		firstNameCol.setMinWidth(300);

		TableColumn lastNameCol = new TableColumn("Last Name");
		lastNameCol.setMinWidth(300);

		TableColumn emailCol = new TableColumn("Email");
		emailCol.setMinWidth(300);
		emailCol.setVisible(false);

		tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

		VBox vBox = new VBox(10, label, tableView);
		vBox.setSpacing(5);
		vBox.setPadding(new Insets(10, 10, 10, 10));

		primaryStage.setScene(new Scene(vBox));
		primaryStage.setTitle("Table View Example");
		primaryStage.setWidth(900);
		primaryStage.setHeight(500);
		primaryStage.show();
	}

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
	public static void main(String args[]) {
		Application.launch(TableViewApp.class, args);
	}
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment