2016-07-10 26 views
1

列のすべての行でテキスト「ハイパーリンク」を挿入しようとしています。ボタンをクリックすると、TabelView行だけが挿入されます。ハイパーリンクも挿入されますが、すべての行には挿入されません。次の行データが追加されると、前の行の空白セルが自動的に取得されます。スクリーンショット: enter image description hereJavaFx TableViewでのハイパーリンクの挿入

ハイパーリンクリスナーが作成され、選択された行がクリックされると削除されます。

このメソッドが呼び出されたときにユーザーがボタンをクリックして、私はリンク作成していますここ:

public void SalesAdd(ActionEvent actionEvent){ 


    if(quantity.getText().isEmpty()){ 
     quantity.setStyle("-fx-border-color: red"); 
     return; 
    } 
    String name = comboBox.getSelectionModel().getSelectedItem(); 
    String batch = batchno.getText(); 
    String exp = expDate.getText(); 
    int qty = Integer.parseInt(quantity.getText()); 
    Double mrp1 = Double.valueOf(mrp.getText()); 
    Double amt = Double.valueOf(amount.getText()); 
    Double mrpAmount = mrp1*qty; 

    PropertyValueFactory<TableData,String> namePro = new PropertyValueFactory<TableData,String>("name"); 
    PropertyValueFactory<TableData,Integer> qtyPro = new PropertyValueFactory<TableData,Integer>("qty"); 
    PropertyValueFactory<TableData,String> expPro = new PropertyValueFactory<TableData,String>("exp"); 
    PropertyValueFactory<TableData,String> batchPro = new PropertyValueFactory<TableData,String>("batch"); 
    PropertyValueFactory<TableData,Double> mrpPro = new PropertyValueFactory<TableData,Double>("mrp"); 
    PropertyValueFactory<TableData,Double> amtPro = new PropertyValueFactory<TableData,Double>("amt"); 
    PropertyValueFactory<TableData,Hyperlink> rmbutton = new PropertyValueFactory<TableData,Hyperlink>("rbutton"); 

    nameColumn.setCellValueFactory(namePro); 
    qtyColumn.setCellValueFactory(qtyPro); 
    expColumn.setCellValueFactory(expPro); 
    batchColumn.setCellValueFactory(batchPro); 
    mrpColumn.setCellValueFactory(mrpPro); 
    amtColumn.setCellValueFactory(amtPro); 
    removeRowColumn.setCellValueFactory(rmbutton); 



    for(TableData data:tableData){ 
     if(data.getName()==comboBox.getEditor().getText() || data.getName() == comboBox.getSelectionModel().getSelectedItem().toString()){ 
      common.dialogAlert("Already in Table!","Already in the Table!","Already Exist,Please change the quantity!"); 
      return; 
     } 

    } 

    tableData.add(new TableData(name,batch,exp,qty,mrp1,mrpAmount, rbutton)); 
    tableView.setItems(tableData); 

    clearInput(); 
    calctotal(); 


} 

TABLEDATAクラス:

public class TableData extends ActionEvent { 
private final SimpleStringProperty name; 
private final SimpleStringProperty batch; 
private final SimpleStringProperty exp; 
private final SimpleIntegerProperty qty; 
private final SimpleDoubleProperty mrp; 
private final SimpleDoubleProperty amt; 
private final Hyperlink rbutton; 

public Hyperlink getRbutton() { 
    return rbutton; 
} 

public TableData(String name, String batch, 
       String exp, int qty, Double mrp, Double amt, Hyperlink rbutton) { 
    this.name = new SimpleStringProperty(name); 
    this.batch = new SimpleStringProperty(batch); 
    this.exp = new SimpleStringProperty(exp); 
    this.qty = new SimpleIntegerProperty(qty); 
    this.mrp = new SimpleDoubleProperty(mrp); 
    this.amt = new SimpleDoubleProperty(amt); 
    this.rbutton = rbutton; 
    this.amt.bind(this.qty.multiply(this.mrp)); 


} 

public String getName() { 
    return name.get(); 
} 

public SimpleStringProperty nameProperty() { 
    return name; 
} 

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

public String getBatch() { 
    return batch.get(); 
} 

public SimpleStringProperty batchProperty() { 
    return batch; 
} 

public void setBatch(String batch) { 
    this.batch.set(batch); 
} 

public String getExp() { 
    return exp.get(); 
} 

public SimpleStringProperty expProperty() { 
    return exp; 
} 

public void setExp(String exp) { 
    this.exp.set(exp); 
} 

public int getQty() { 
    return qty.get(); 
} 

public SimpleIntegerProperty qtyProperty() { 
    return qty; 
} 

public void setQty(int qty) { 
    this.qty.set(qty); 
} 

public double getMrp() { 
    return mrp.get(); 
} 

public SimpleDoubleProperty mrpProperty() { 
    return mrp; 
} 

public void setMrp(double mrp) { 
    this.mrp.set(mrp); 
} 

public double getAmt() { 
    return amt.get(); 
} 

public SimpleDoubleProperty amtProperty() { 
    return amt; 
} 

public void setAmt(double amt) { 
    this.amt.set(amt); 
} 

}私はこれを追加するにはどうすればよい

を列内のすべての行に対して同じHyperLinkを使用できますか?

+0

すべての行に同じ 'HyperLink'オブジェクトを使用していると思いますが、' Node'はシーングラフに2回追加されないため、動作しません(最後に追加された行だけがハイパーリンクを表示します)。すべての 'TableData'オブジェクトに対して、別々の' HyperLink'オブジェクトを持たなければなりません。私はあなたのデータモデルがなぜ 'ActionEvent'を拡張するのかも理解していません。ここでは、 'HyperView'を' TableView'に挿入する方法についての良い記事があります:http://www.superglobals.net/create-hyperlink-cell-in-javafx-tableview/ – DVarga

+0

プライベートハイパーリンクrbutton =新しいハイパーリンク); rbutton =新しいハイパーリンク( "Remove");動作していますが、リスナーは動作しません。リスナー:rbutton.setOnAction(新しいイベントハンドラ(){ @Override公共ボイドハンドル(のActionEventイベント){ のSystem.out.println( "をクリック");} })。 – kodr

答えて

1

アイテムクラスにUI要素を含めることはほとんどありません(また、ActionEventを拡張する必要はないようです)。リンクは任意の項目値から独立して動作するため、リンクを使用しないでください。代わりに、空でないときにリンクを表示するセルを使用してください:

public class RemoveCell<T> extends TableCell<T, Void> { 

    private final Hyperlink link; 

    public RemoveCell() { 
     link = new Hyperlink("Remove"); 
     link.setOnAction(evt -> { 
      // remove row item from tableview 
      getTableView().getItems().remove(getTableRow().getIndex()); 
     }); 
    } 

    @Override 
    protected void updateItem(Void item, boolean empty) { 
     super.updateItem(item, empty); 

     setGraphic(empty ? null : link); 
    } 

} 

このセルにはデータはありません。代わりに、リンクは空でないセル(setGraphic(empty ? null : link);)にのみ表示されます。 HyperlinkonActionイベントがトリガされると、TableCellから利用可能なデータを使用して、セルを含むTableViewから対応する要素が削除されます。アイテムの削除時に追加の操作を行う必要がある場合は、ラムダ式の本体に追加のコードを追加することができます。ところで

removeRowColumn.setCellFactory(tc -> new RemoveCell<>()); 


ではなく、単にcellFactory作成RemoveCell Sを使用し、(値型のみnull値を可能としてVoidを選択)removeRowColumnためcellValueFactoryを使用しないでくださいあなたを1つの新しい項目を挿入するボタンをクリックしてcellValueFactoryを再作成しているようです。これは、挿入されたテーブル行ごとに1回ではなく、テーブル全体に対して1回だけ行うほうがよいでしょう。

+0

私はjavaを初めて利用しています。これがどのように機能しているか説明してください。私はこのようなコードを書くことは決してありません:(。私は別の行を計算する方法があります。行を削除した後にそのメソッドを呼び出すにはどうすればいいですか? – kodr

+0

@rpo:あなたのコメントでお聞きしたいのは基本的にチュートリアルです。これは答えの範囲外です。 *より具体的な質問は「これはどういう仕組みですか?」*答えるか、少なくとも私はさらなる読書のためのリンクを投稿することができます。 [The * Cell Factories * section here](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Cell.html)は、 'Cell' /' cellFactoryコンセプト。 – fabian

関連する問題