JavaFX Scene Builder TableView and TableColumn

Source Code : Class mysqlconnect.java package tableviewexample; /** * * @author amir */ import java.sql.*; import javafx.collecti...



Source Code :

Class mysqlconnect.java

package tableviewexample; /** * * @author amir */ import java.sql.*; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javax.swing.*; public class mysqlconnect { Connection conn = null; public static Connection ConnectDb(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/market", "root", ""); JOptionPane.showMessageDialog(null, "Connection Established10"); return conn; }catch (Exception e){ JOptionPane.showMessageDialog(null, e); return null; } } public static ObservableList<Person>getDataPerson(){ Connection con =ConnectDb(); ObservableList<Person>list =FXCollections.observableArrayList(); try { PreparedStatement ps = con.prepareStatement("select * from users"); ResultSet rs = ps.executeQuery(); while(rs.next()){ list.add(new Person(Integer.parseInt(rs.getString("user_id")), rs.getString("username"), rs.getString("lname"), rs.getString("gmail"), rs.getString("yahoo"), rs.getString("phone"), rs.getString("country"))); } } catch (Exception e) { }finally{ try { } catch (Exception e) { } } return list; } } Class FXMLDocumentController.java
package tableviewexample;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 *
 * @author amir
 */
public class FXMLDocumentController implements Initializable {
    

      @FXML
    private TableView<Person> table;

    @FXML
    private TableColumn<Person, Integer> id;

    @FXML
    private TableColumn<Person, String> name;

    @FXML
    private TableColumn<Person, String> lname;

    @FXML
    private TableColumn<Person, String> gmail;

    @FXML
    private TableColumn<Person, String> yahoo;

    @FXML
    private TableColumn<Person, String> phone;

    @FXML
    private TableColumn<Person, String> country;
    
    ObservableList<Person>listF;
    ObservableList<Person> getProductList(){
    ObservableList<Person> pers = FXCollections.observableArrayList();
    
        
        return pers;
    }
     int indexM = -1;
     
     Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /*  
    ObservableList<Person> list = FXCollections.observableArrayList(
    
            new Person(1, "Amir", "code", "codeamirquestion@gmail.com", "mail@yahoo.com", "0254554444444","USA"),
            new Person(2, "Yassin", "code", "ab@gmail.com", "b@yahoo.com", "0254554444444","UK"),
            new Person(3, "Adam", "code", "bv@gmail.com", "c@yahoo.com", "0254555544444","Russia"),
            new Person(4, "Panda", "code", "d@gmail.com", "d@yahoo.com", "0254554444444","China"),
            new Person(5, "Sara", "code", "e@gmail.com", "e@yahoo.com", "0254554444444","France")
    );*/
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        id.setCellValueFactory(new PropertyValueFactory<Person,Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<Person,String>("name"));
        lname.setCellValueFactory(new PropertyValueFactory<Person,String>("lname"));
        gmail.setCellValueFactory(new PropertyValueFactory<Person,String>("gmail"));
        yahoo.setCellValueFactory(new PropertyValueFactory<Person,String>("yahoo"));
        phone.setCellValueFactory(new PropertyValueFactory<Person,String>("phone"));
        country.setCellValueFactory(new PropertyValueFactory<Person,String>("country"));
        
        //table.setItems(list);
          listF = mysqlconnect.getDataPerson();
        table.setItems(listF);
        
        
    }    
    

}



Class TableViewEXample.java

package tableviewexample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author amir
 */
public class TableViewEXample extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    

}

Class Person.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tableviewexample;

/**
 *
 * @author amir
 */
public class Person {
    
    int id;
    String name,lname,gmail,yahoo,phone,country;

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCountry() {
        return country;
    }

    public Person(int id, String name, String lname, String gmail, String yahoo, String phone, String country) {
        this.id = id;
        this.name = name;
        this.lname = lname;
        this.gmail = gmail;
        this.yahoo = yahoo;
        this.phone = phone;
        this.country = country;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public void setGmail(String gmail) {
        this.gmail = gmail;
    }

    public void setYahoo(String yahoo) {
        this.yahoo = yahoo;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public String getLname() {
        return lname;
    }

    public String getGmail() {
        return gmail;
    }

    public String getYahoo() {
        return yahoo;
    }

    public String getPhone() {
        return phone;
    }
    
    
}


FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewexample.FXMLDocumentController">
   <children>
      <TableView fx:id="table" layoutX="14.0" prefHeight="400.0" prefWidth="710.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="id" prefWidth="86.0" text="ID" />
          <TableColumn fx:id="name" prefWidth="103.0" text="Name" />
            <TableColumn fx:id="lname" prefWidth="75.0" text="Last Name" />
            <TableColumn prefWidth="174.0" text="Email">
               <columns>
                  <TableColumn fx:id="gmail" prefWidth="75.0" text="gmail" />
                  <TableColumn fx:id="yahoo" prefWidth="75.0" text="yahoo" />
               </columns>
            </TableColumn>
            <TableColumn fx:id="phone" prefWidth="144.0" text="phone" />
            <TableColumn fx:id="country" prefWidth="109.0" text="Country" />
        </columns>
      </TableView>
   </children>
</AnchorPane>






The Table View control will be used to display the list of issues. 1 - In the Controls section of the Library panel, select Table View. Drag and drop it above the Split Pane (Vertical Flow) element's divider line 2- From the Menu bar, choose Modify and then Fit to Parent. 3 - Click the Code section of the Inspector panel and select table from the fx:id field's drop-down list of available instance variables. 4 - Set the properties of the two columns in the table view. In the Hierarchy panel, select the row for the first TableColumn component under the row for the TableView component. In the Code section of the Inspector panel, select colName from the fx:id field's drop-down list. Click the tab for the Properties section and set the Text property to Name:. Back in the Hierarchy panel, select the row for the second Table Column component. In the Code section of the Inspector panel, select colStatus from the fx:id field's drop-down list. Click the tab for the Properties section again and set the Text property to Status. 5 - Add another column to the table. Select the Table Column control from the Library panel. Drag and drop it inside the table view in the Content panel. The new column is added to the right of the Status tab. In the Code section of the Inspector panel, select colSynopsis from the fx:id field's drop-down list of available instance variables. Click the tab for the Properties section again and set the Text property to Synopsis 6 - From the Menu bar, choose View and then Show Sample Data. Notice that the list view and the table view elements in the Content panel are populated with sample data. Choose View and then Hide Sample Data from the Menu bar to turn off the display of the sample data.





COMMENTS

Name

CSS,3,HTML,3,Java,59,JavaFX,59,JFoenix,15,PHP,4,Scene Builder,29,SQL,9,
ltr
item
Code Amir: JavaFX Scene Builder TableView and TableColumn
JavaFX Scene Builder TableView and TableColumn
https://i.ytimg.com/vi/A5fQbsJ-iF8/0.jpg
https://i.ytimg.com/vi/A5fQbsJ-iF8/0.jpg
Code Amir
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-tableview-and.html
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-tableview-and.html
true
405304041427069011
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy