如何在JavaFx中将数组插入到TableView中?

huangapple go评论107阅读模式
英文:

How to insert an array into a TableView in JavaFx?

问题

public void RPserch(ActionEvent event) throws IOException {

    ObservableList<Person> data;

    TableColumn remark = new TableColumn("Check");
    remark.setMinWidth(40);
    TableColumn Purchplan = new TableColumn("PurchplanID");
    Purchplan.setMinWidth(100);
    TableColumn Producplan = new TableColumn("Producplan");
    Producplan.setMinWidth(150);
    TableColumn RPID = new TableColumn("RPID");
    RPID.setMinWidth(110);
    TableColumn itemname = new TableColumn("itemname");
    itemname.setMinWidth(100);
    TableColumn RPamount = new TableColumn("RPamount");
    RPamount.setMinWidth(100);
    TableColumn RPdate = new TableColumn("RPdate");
    RPdate.setMinWidth(200);
    TableColumn RPlocation = new TableColumn("RPlocation");
    RPlocation.setMinWidth(100);
    TableColumn RPperson = new TableColumn("RPperson");
    RPperson.setMinWidth(100);
    TableColumn buyer = new TableColumn("buyer ");
    buyer.setMinWidth(100);
    TableColumn condition = new TableColumn("condition");
    condition.setMinWidth(100);

    RPTableview.getColumns().addAll(remark, Purchplan, Producplan, RPID, itemname, RPamount, RPdate, RPlocation,RPperson, buyer, condition);

    Purchplan.setCellValueFactory(new PropertyValueFactory<Person, String>("PurchplanID"));
    Producplan.setCellValueFactory(new PropertyValueFactory<Person, String>("Producplan"));
    RPID.setCellValueFactory(new PropertyValueFactory<Person, String>("RPID"));
    itemname.setCellValueFactory(new PropertyValueFactory<Person, String>("itemname"));
    RPamount.setCellValueFactory(new PropertyValueFactory<Person, String>("RPamount"));
    RPdate.setCellValueFactory(new PropertyValueFactory<Person, String>("RPdate"));
    RPlocation.setCellValueFactory(new PropertyValueFactory<Person, String>("RPlocation"));
    RPperson.setCellValueFactory(new PropertyValueFactory<Person, String>("RPperson"));
    buyer.setCellValueFactory(new PropertyValueFactory<Person, String>("buyer"));
    condition.setCellValueFactory(new PropertyValueFactory<Person, String>("condition"));

    String RPserchf = RPserchfield.getText();

    try {
        String search = RPserchf + "|" + "1|||";
        search = search.trim();
        System.out.println(search);
        os = cltSocket.getOutputStream();
        os.write(search.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        in = cltSocket.getInputStream();
        String strRcvMsg = "";
        byteBuff = new byte[1024];
        int nRcvLen = in.read(byteBuff);
        strRcvMsg = new String(byteBuff, 0, nRcvLen, "EUC-KR");
        Thread.sleep(500);
        System.out.println(strRcvMsg);
        String[] testmsg = strRcvMsg.split("&");

        TableView<Person> RPTableview = new TableView<Person>();

        data = FXCollections.observableArrayList(new Person("", testmsg[0], testmsg[1], testmsg[2], testmsg[3],testmsg[4], testmsg[5], testmsg[6], testmsg[7], testmsg[8], testmsg[9]));

        for (int i = 0; i < testmsg.length - 1; i++) {
            System.out.println(testmsg[i]);
        }
        RPTableview.setItems(data);
    } catch (Exception e) {
        System.out.println("[EXP] Tableview " + e.getLocalizedMessage());
    }
}
英文:

I want insert an array into a Table View. I've tried and searched several times, but I can't solve it.

When I press the search button, I want to convert messages from the server to an array using split and put each array in each row of the table view.

	public void RPserch(ActionEvent event) throws IOException {
ObservableList&lt;Person&gt; data;
TableColumn remark = new TableColumn(&quot;Check&quot;);
remark.setMinWidth(40);
TableColumn Purchplan = new TableColumn(&quot;PurchplanID&quot;);
Purchplan.setMinWidth(100);
TableColumn Producplan = new TableColumn(&quot;Producplan&quot;);
Producplan.setMinWidth(150);
TableColumn RPID = new TableColumn(&quot;RPID&quot;);
RPID.setMinWidth(110);
TableColumn itemname = new TableColumn(&quot;itemname&quot;);
itemname.setMinWidth(100);
TableColumn RPamount = new TableColumn(&quot;RPamount&quot;);
RPamount.setMinWidth(100);
TableColumn RPdate = new TableColumn(&quot;RPdate&quot;);
RPdate.setMinWidth(200);
TableColumn RPlocation = new TableColumn(&quot;RPlocation&quot;);
RPlocation.setMinWidth(100);
TableColumn RPperson = new TableColumn(&quot;RPperson&quot;);
RPperson.setMinWidth(100);
TableColumn buyer = new TableColumn(&quot;buyer &quot;);
buyer.setMinWidth(100);
TableColumn condition = new TableColumn(&quot;condition&quot;);
condition.setMinWidth(100);
RPTableview.getColumns().addAll(remark, Purchplan, Producplan, RPID, itemname, RPamount, RPdate, RPlocation,RPperson, buyer, condition);
Purchplan.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;PurchplanID&quot;));
Producplan.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;Producplan&quot;));
RPID.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;RPID&quot;));
itemname.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;itemname&quot;));
RPamount.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;RPamount&quot;));
RPdate.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;RPdate&quot;));
RPlocation.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;RPlocation&quot;));
RPperson.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;RPperson&quot;));
buyer.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;buyer&quot;));
condition.setCellValueFactory(new PropertyValueFactory&lt;Person, String&gt;(&quot;condition&quot;));

This is the table column setting.

The code that runs when the button is pressed from the bottom. The arrangement is well divided, but the arrangement does not seem to go over to the row of the table view.


String RPserchf = RPserchfield.getText();
try {
String search = RPserchf + &quot;|&quot; + &quot;1|||&quot;;
search = search.trim();
System.out.println(search);
os = cltSocket.getOutputStream();
os.write(search.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
try {
in = cltSocket.getInputStream();
String strRcvMsg = &quot;&quot;;
byteBuff = new byte[1024];
int nRcvLen = in.read(byteBuff);
strRcvMsg = new String(byteBuff, 0, nRcvLen, &quot;EUC-KR&quot;);
Thread.sleep(500);
System.out.println(strRcvMsg);
String[] testmsg = strRcvMsg.split(&quot;&amp;&quot;);
TableView&lt;Person&gt; RPTableview = new TableView&lt;Person&gt;();
data = FXCollections.observableArrayList(new Person(&quot;&quot;, testmsg[0], testmsg[1], testmsg[2], testmsg[3],testmsg[4], testmsg[5], testmsg[6], testmsg[7], testmsg[8], testmsg[9]));
for (int i = 0; i &lt; testmsg.length - 1; i++) {
System.out.println(testmsg[i]);
}
RPTableview.setItems(data);
} catch (Exception e) {
System.out.println(&quot;[EXP] Tableview &quot; + e.getLocalizedMessage());
}
}

答案1

得分: 0

ObservableList<Person> data = FXCollections.observableArrayList(
    new Person("", testmsg[0], testmsg[1], testmsg[2], testmsg[3], testmsg[4], testmsg[5], testmsg[6],
    testmsg[7], testmsg[8], testmsg[9]));

remark.setCellValueFactory(cellData -> cellData.getValue().remarkProperty());
Purchplan.setCellValueFactory(cellData -> cellData.getValue().PurchplanProperty());
Producplan.setCellValueFactory(cellData -> cellData.getValue().ProducplanProperty());
RPID.setCellValueFactory(cellData -> cellData.getValue().RPIDProperty());
itemname.setCellValueFactory(cellData -> cellData.getValue().itemnameProperty());
RPamount.setCellValueFactory(cellData -> cellData.getValue().RPamountProperty());
RPdate.setCellValueFactory(cellData -> cellData.getValue().RPdateProperty());
RPlocation.setCellValueFactory(cellData -> cellData.getValue().RPlocationProperty());
RPperson.setCellValueFactory(cellData -> cellData.getValue().RPpersonProperty());
buyer.setCellValueFactory(cellData -> cellData.getValue().buyerProperty());
condition.setCellValueFactory(cellData -> cellData.getValue().conditionProperty());

RPTableview.setItems(data);
英文:

I added a column value to the SceneBulider and let each cell have a value.

ObservableList&lt;Person&gt; data = FXCollections.observableArrayList(
new Person(&quot;&quot;, testmsg[0], testmsg[1], testmsg[2], testmsg[3], testmsg[4], testmsg[5], testmsg[6],
testmsg[7], testmsg[8], testmsg[9]));
remark.setCellValueFactory(cellData -&gt; cellData.getValue().remarkProperty());
Purchplan.setCellValueFactory(cellData -&gt; cellData.getValue().PurchplanProperty());
Producplan.setCellValueFactory(cellData -&gt; cellData.getValue().ProducplanProperty());
RPID.setCellValueFactory(cellData -&gt; cellData.getValue().RPIDProperty());
itemname.setCellValueFactory(cellData -&gt; cellData.getValue().itemnameProperty());
RPamount.setCellValueFactory(cellData -&gt; cellData.getValue().RPamountProperty());
RPdate.setCellValueFactory(cellData -&gt; cellData.getValue().RPdateProperty());
RPlocation.setCellValueFactory(cellData -&gt; cellData.getValue().RPlocationProperty());
RPperson.setCellValueFactory(cellData -&gt; cellData.getValue().RPpersonProperty());
buyer.setCellValueFactory(cellData -&gt; cellData.getValue().buyerProperty());
condition.setCellValueFactory(cellData -&gt; cellData.getValue().conditionProperty());
RPTableview.setItems(data);

huangapple
  • 本文由 发表于 2020年4月10日 11:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定