简体   繁体   中英

How To Make Table View's Specific Column's Cells Not Selectable?

I am trying to create a matrix table where on the X axis we have Courses and on the Y axis we have Student names. Each cell should represent a student's grade in a course.

I haven't found any way to create a matrix table beside to just make the first column as the Student's name. (If there is any other way please let me know)

I was able to make the cells selectable. however I want the Student's name column cells to be unselectable but no luck with that.

Please advise. Thanks !

I think your question consist of two parts

the first one : making a table with x-axis for courses and y-axis for student names

at first we create a simple class that represents our data model and call it Data here's it's code

public class Data{

private String studentName;
private int mathDegree;
private int religionDegree;
private int javaDegree;

public Data(String studentName, int mathDegree, int religionDegree, int javaDegree)
{
    this.studentName = studentName;
    this.mathDegree = mathDegree;
    this.religionDegree = religionDegree;
    this.javaDegree = javaDegree;
}

public String getStudentName()
{
    return studentName;
}

public void setStudentName(String studentName)
{
    this.studentName = studentName;
}

public int getMathDegree()
{
    return mathDegree;
}

public void setMathDegree(int mathDegree)
{
    this.mathDegree = mathDegree;
}

public int getReligionDegree()
{
    return religionDegree;
}

public void setReligionDegree(int religionDegree)
{
    this.religionDegree = religionDegree;
}

public int getJavaDegree()
{
    return javaDegree;
}

public void setJavaDegree(int javaDegree)
{
    this.javaDegree = javaDegree;
}}

and now lets create our table view and fill it with some data

here's its simple code

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JFXION extends Application{
@Override
public void start(Stage stage)
{

    List<Data> dataList = new ArrayList<>();
    dataList.add(new Data("Mohammed", 50, 50, 50));
    dataList.add(new Data("Ahmed", 49, 50, 47));
    dataList.add(new Data("Anas", 45, 49, 49));

    //tell the table that: your data is represented in [Data]objects
    TableView<Data> table = new TableView();

    table.setEditable(true);

    TableColumn<Data, String> studentsColumn = new TableColumn("student");
    //tell the student name column that your data is found in a field called[studentName]
    studentsColumn.setCellValueFactory(
            new PropertyValueFactory<>("studentName")
    );

    TableColumn<Data, Integer> mathColumn = new TableColumn("Maths");
    mathColumn.setCellValueFactory(new PropertyValueFactory<>("mathDegree"));
    TableColumn<Data, Integer> religionColumn = new TableColumn("Religion");
    religionColumn.setCellValueFactory(new PropertyValueFactory<>("religionDegree"));
    TableColumn<Data, Integer> javaColumn = new TableColumn("Java");
    javaColumn.setCellValueFactory(new PropertyValueFactory<>("javaDegree"));
    TableColumn coursesColumns = new TableColumn("courses");
    coursesColumns.getColumns().addAll(mathColumn, religionColumn, javaColumn);
    TablePosition cell = table.getFocusModel().getFocusedCell();

    table.getColumns().addAll(studentsColumn, coursesColumns);

    table.getItems().addAll(dataList);

    Hyperlink link = new Hyperlink("more info");
    link.setOnAction(e ->
    {
        try
        {
            java.awt.Desktop.getDesktop().browse(new URI("http://docs.oracle.com/javafx/2/ui_controls/table-view.htm"));
        } catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    });

    VBox box = new VBox(10, table, link);

    Scene scene = new Scene(box);
    stage.setScene(scene);
    stage.show();
}

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

}

just try this and build your own code and classes

but the second part of your question is not clear

you should also have a look to this link javafx table views

i hope this is useful and if you have any problems just let a comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM