Forum Stats

  • 3,854,655 Users
  • 2,264,394 Discussions
  • 7,905,747 Comments

Discussions

Got The Roster On My Chat Application

8d7d6c25-07df-48c6-a7e0-19b8d27a4d7f
edited Sep 15, 2016 9:59AM in JavaFX 2.0 and Later

Hello Everyone,

                              I have some query regarding get the roster. Kindly tell me the best idea to get the roster?? i have used Listview, Vbox ,TableView but i don't know which one is best ?

which one give the fast performance to get the roster? & Is scenebuilder components object is static forever in the code??.

and the most important question :

If i want to show my roster on my scene builder then it will happen after i put some handler, I want to when click my button who call my next scene all the roster comes on next scene automatically.

Kindly give all the responses regarding above mention questions ASAP.

Tagged:

Answers

  • bekwam
    bekwam Member Posts: 6
    edited Sep 15, 2016 9:59AM

    Hi,

    I would use a TableView even if you have only one TableColumn to show.

    Start by creating a .fxml / Controller pair.  The following screenshot shows Scene Builder with a VBox containing a TableView and a refresh Button.

    simpletable_sb.png

    In the Controller, hookup the TableView, TableColumn, and refresh action.  Provide an implementation of refresh that pulls in the data.  The Task wrapper is very important to keep your application responsive.

    Participant is a domain object with a single field "screenName".

    public class SimpleTableController {

       @FXML
       TableColumn<Participant, String> tcScreenName;

       @FXML
       TableView<Participant> tblParticipants;

       @FXML
       public void initialize() {

       tcScreenName.setCellValueFactory(new PropertyValueFactory<Participant,String>("screenName"));

      }

       @FXML
       public void refresh() {

      Task<List<Participant>> task = new Task<List<Participant>>() {

       @Override
       protected List<Participant> call() throws Exception {

       return fetchData();

      }

       @Override
       protected void succeeded() {

       tblParticipants.getItems().clear();

       tblParticipants.getItems().addAll( fetchData() );

      }

      };

       new Thread(task).start();

      }

       private List<Participant> fetchData() {

      List<Participant> participantList = new ArrayList<>();

      participantList.add( new Participant("WALK" + counter++) );

      participantList.add( new Participant("DAVI" + counter++) );

      participantList.add( new Participant("JNYG" + counter++) );

       return participantList;

      }

       private int counter = 1;

    }

    And from your button handler, load the screen using the following code block.  Note the refresh() call made on the Controller to load the initial data set.

    try {

      FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("/SimpleTable.fxml"));

      Parent p = fxmlLoader.load();

      SimpleTableController c = fxmlLoader.getController();

      Scene scene = new Scene( p );

      Stage stage = new Stage();

      stage.setTitle( "SimpleTableApp - TableView");

      stage.setWidth( 667 );

      stage.setHeight( 375 );

      stage.setScene(scene);

      stage.setOnShown( (windowEvent) -> c.refresh() );

      stage.show();

    } catch(Exception exc) {

      exc.printStackTrace();

    }

    Good luck.
This discussion has been closed.