I am working on an application in java FX. I need a data structure similar to JList in java swings.I have to use it in my project for displaying data on a scroll pane. I have tried using observable arraylist, with listview and Vbox. Here is my code snippet of the controller class.

public class Controller_class
    implements Initializable {

@FXML //  fx:id="myButton"
private Button dfctsave; 

@FXML
final TextField dfctname = new TextField();

@FXML
ScrollPane dfctscroll = new ScrollPane ();

static ArrayList<String> jlstDefects=new ArrayList<String>();


@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) 
{


    dfctsave.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            ObservableList ad;
            VBox v;
            String d=dfctname.getText();
            jlstDefects.add(d);
            System.out.println(jlstDefects);
            ad = FXCollections.observableArrayList(jlstDefects);
            System.out.println(ad);
            ListView lv = new ListView();
            lv.setItems(ad);
            v=new VBox(); 
            v.getChildren().addAll(lv);
            dfctscroll.setContent(v);

        }
    });

}}

It worked well and I got entries on to the scrollpane, but I need the index of the selected data items for swapping and further processing on scrollpane. I heard about SwingList which would work like the same. Can anyone explain the best alternates for Jlist in FX or explain how to use SwingList in FX.

有帮助吗?

解决方案

I tried working with List view. Its working fine.For selecting a particular value on the list view and getting its index, I used the following code snippet.

   public ListView<String> jlstDefects ;
   public TextField fldDefectName;

   private void jltDefectsListItemSelected()
   {

    int ndx = jlstDefects.getSelectionModel().getSelectedIndex();
    if (ndxJlstDefectSelector == ndx)
        return;
    ndxJlstDefectSelector = ndx;
    String strSelectedDefectName = lstDefectList.getDefect(ndx);
    fldDefectName.setText(strLocalDefectName);


}

I have assigned the item which I got from the list view to a text field in my application. Thanks for the suggestions provided.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top