javaswingjtablejpopupmenu

How to automatically right click on jtable and select first JMenu item


I have a swing application where we pass a json to the textfield and after clicking on load button, a table is getting populated.

I am able to load the json and selected the whole table automatically through following code:

  resourceButton.doClick();
  this.table.selectAll();

Now I want to right click on the selected table and choose first option from the popupmenu. Any suggestions?

I want to automate this particular part of UI:


    JMenuItem addToSiteMap = new JMenuItem("Add to site map");
    addToSiteMap
        .addActionListener(e -> IntStream.of(tab.getTable().getSelectedRows()).forEach(row -> {
          int index = (int) tab.getTable()
              .getValueAt(row, tab.getTable().getColumn("#").getModelIndex());
          HttpRequestResponse httpRequestResponse = this.httpRequestResponses.get(index);
          callbacks.addToSiteMap(httpRequestResponse);
        }));

Solution

  • You can use button for this purpose instead of the popup menu. You can add the button and write an action listener on it like below

    button.addActionListener(e -> IntStream.of(this.getTable().getSelectedRows()).forEach(row -> {
          int index = (int) this.getTable()
                  .getValueAt(row, this.getTable().getColumn("#").getModelIndex());
          HttpRequestResponse httpRequestResponse = this.httpRequestResponses.get(index);
          resourceTextField.setText(String.valueOf(index));
      callbacks.addToSiteMap(httpRequestResponse);
        }));