要从CSV填充JavaFX tableview

时间:2017-01-20 13:13:20

标签: javafx

我是JavaFX的新手,我只想尝试从CSV文本文件填充FXML中定义的tableview。我的代码如下:

package student;

import java.net.URL;
import java.util.*;
import javafx.fxml.*;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;


public class StudInfoController implements Initializable {

    @FXML
    private Tab profile;
    @FXML
    private Label namelbl;
    @FXML
    private Label clslbl;
    @FXML
    private Label divlbl;
    @FXML
    private Label rnolbl;
    @FXML
    private Label maillbl;
    @FXML
    private Label moblbl;
    @FXML
    private Label altlbl;
    @FXML
    private Tab exams;
    @FXML
    private Tab result;
    @FXML
    private TableView<Exam> ExamTable;
    @FXML
    private TableView<Result> ResultTable;
    @FXML
    private TableColumn<Exam, String> eSub;
    @FXML
    private TableColumn<Exam, String> eDate;
    @FXML
    private TableColumn<Exam, String> eDur;
    @FXML
    private TableColumn<Exam, String> eMarks;
    @FXML
    private TableColumn<Result, String> rSub;
    @FXML
    private TableColumn<Result, String> rDate;
    @FXML
    private TableColumn<Result, String> rTT;
    @FXML
    private TableColumn<Result, String> rTE;
    @FXML
    private TableColumn<Result, String> rMarks;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        eSub.setCellValueFactory(new PropertyValueFactory<>("eSub"));

        eDate.setCellValueFactory(new PropertyValueFactory<>("eDate"));
        eDur.setCellValueFactory(new PropertyValueFactory<>("eDur"));
        eMarks.setCellValueFactory(new PropertyValueFactory<>("eMarks"));
        rSub.setCellValueFactory(new PropertyValueFactory<>("rSub"));
        rDate.setCellValueFactory(new PropertyValueFactory<>("rDate"));
        rTT.setCellValueFactory(new PropertyValueFactory<>("rTT"));
        rTE.setCellValueFactory(new PropertyValueFactory<>("rTE"));
        rMarks.setCellValueFactory(new PropertyValueFactory<>("rMarks"));
        ExamTable.getItems().setAll(InsertExam());
        ResultTable.getItems().setAll(InsertResult());
    }

    private List<Exam> InsertExam() {
        try {
            FileRead fr = new FileRead();
            String[] l = fr.examRead();
            String[] items;
            int i = 0;
            Exam r = new Exam();
            List<Exam> row = new ArrayList<>();
            for (i = 0; i < l.length; i++) {

                items = l[i].split(",");
                r.setESub(items[0]);
                r.setEDate(items[1]);
                r.setEDur(items[2]);
                r.setEMarks(items[3]);
                row.add(r);
            }

            return row;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<Result> InsertResult() {
        try {
            FileRead fr = new FileRead();
            String[] l = fr.resultRead();
            String[] items;
            Result r = new Result();
            List<Result> row = new ArrayList<>();
            for (int i = 0; i < l.length; i++) {

                items = l[i].split(",");
                r.setRSub(items[0]);
                r.setRDate(items[1]);
                r.setRTT(items[2]);
                r.setRTE(items[3]);
                r.setRMarks(items[4]);
                row.add(r);
            }

            return row;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    class Exam {

        private String eSub;

        private String eDate;

        private String eDur;

        private String eMarks;

        public Exam() {
        }

        public String getESub() {
            return eSub;
        }

        public void setESub(String eSub) {
            this.eSub = eSub;
        }

        public String getEDate() {
            return eDate;
        }

        public void setEDate(String eDate) {
            this.eDate = eDate;
        }

        public String getEDur() {
            return eDur;
        }

        public void setEDur(String eDur) {
            this.eDur = eDur;
        }

        public String getEMarks() {
            return eMarks;
        }

        public void setEMarks(String eMarks) {
            this.eMarks = eMarks;
        }
    }

    class Result {

        private String rSub;

        private String rDate;

        private String rTT;

        private String rTE;

        private String rMarks;

        public String getRSub() {
            return rSub;
        }

        public void setRSub(String rSub) {
            this.rSub = rSub;
        }

        public String getRDate() {
            return rDate;
        }

        public void setRDate(String rDate) {
            this.rDate = rDate;
        }

        public String getRTT() {
            return rTT;
        }

        public void setRTT(String rTT) {
            this.rTT = rTT;
        }

        public String getRTE() {
            return rTE;
        }

        public void setRTE(String rTE) {
            this.rTE = rTE;
        }

        public String getRMarks() {
            return rMarks;
        }

        public void setRMarks(String rMarks) {
            this.rMarks = rMarks;
        }

        public Result() {
        }
    }

}

FXML代码如下:

<TableView fx:id="ResultTable" prefHeight="345.0" prefWidth="697.0">
    <columns>
        <TableColumn fx:id="rSub" editable="false" prefWidth="297.0"   
                     text="SUBJECT"/>
        <TableColumn fx:id="rDate" editable="false" prefWidth="97.0"   
                     text="DATE"   />
        <TableColumn fx:id="rTT" editable="false" prefWidth="123.0"  
                     text="TOTAL   TIME"/>
        <TableColumn fx:id="rTE" editable="false" prefWidth="116.0"  
                     text="TIME    ELAPSED" />
        <TableColumn fx:id="rMarks" editable="false" prefWidth="63.0"   
                     text="MARKS" />
    </columns>
</TableView>
<TableView fx:id="ExamTable" prefHeight="344.0" prefWidth="697.0">
    <columns>
        <TableColumn fx:id="eSub" editable="false" prefWidth="414.0"
                     text="SUBJECT" />
        <TableColumn fx:id="eDate" editable="false" prefWidth="148.0" 
                     text="DATE" />
        <TableColumn fx:id="eDur" editable="false" prefWidth="79.0"    
                     text="DURATION" />
        <TableColumn fx:id="eMarks" editable="false" prefWidth="55.0"    
                     text="MARKS" />
    </columns>
</TableView>

抛出以下异常:

Executing C:\Users\ANIL\Documents\NetBeansProjects\Student\dist\run775021387\Student.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class student.StudInfoController$Result with modifiers "public"
    at com.sun.javafx.property.PropertyReference.get(PropertyReference.java:177)
    at javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:147)
    at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:119)
    at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:563)
    at javafx.scene.control.TableCell.updateItem(TableCell.java:644)
    at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
    at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
    at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
    at javafx.scene.control.Control.impl_processCSS(Control.java:872)
    at javafx.scene.Node.processCSS(Node.java:9058)
    at javafx.scene.Node.applyCss(Node.java:9155)
    at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)
    at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
    at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
    at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
    at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Scene.doLayoutPass(Scene.java:552)
    at javafx.scene.Scene.preferredSize(Scene.java:1646)
    at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
    at javafx.stage.Window$9.invalidated(Window.java:846)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
    at javafx.stage.Window.setShowing(Window.java:922)
    at javafx.stage.Window.show(Window.java:937)
    at javafx.stage.Stage.show(Stage.java:259)
    at student.StudentFXMLController.OKBtnAction(StudentFXMLController.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class student.StudInfoController$Result with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at com.sun.javafx.property.PropertyReference.get(PropertyReference.java:175)
    ... 94 more
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class student.StudInfoController$Result with modifiers "public"
    at com.sun.javafx.property.PropertyReference.get(PropertyReference.java:177)
    at javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:147)
    at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:119)
    at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:563)
    at javafx.scene.control.TableCell.updateItem(TableCell.java:644)
    at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
    at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
    at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
    at javafx.scene.control.Control.impl_processCSS(Control.java:872)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.control.Control.impl_processCSS(Control.java:868)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
    at javafx.scene.control.Control.impl_processCSS(Control.java:868)
    at javafx.scene.Node.processCSS(Node.java:9058)
    at javafx.scene.Node.processCSS(Node.java:9051)
    at javafx.scene.Scene.doCSSPass(Scene.java:545)
    at javafx.scene.Scene.access$3600(Scene.java:159)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class student.StudInfoController$Result with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at com.sun.javafx.property.PropertyReference.get(PropertyReference.java:175)
    ... 37 more

1 个答案:

答案 0 :(得分:0)

问题在于类声明。这堂课必须公开。问题得到解决。谢谢你的帮助