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

151 Views Asked by At

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);
        }));
2

There are 2 best solutions below

2
Hardik Rana On BEST ANSWER

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);
    }));

6
George Z. On

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

A popup menu contains JMenuItems or JMenus. Either way, the ones with an actual action are the JMenuItems.

A JMenuItem is a button as well. You already use resourceButton.doClick(). You can use doClick to a JMenuItem too.

An example:

public class TableTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JTable table = new JTable(new Object[][] { { "something" } }, new String[] { "column" });

            JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("MenuItem");
            menuItem.addActionListener(e -> {
                System.out.println("Popup item clicked.");
            });
            popupMenu.add(menuItem);

            table.setComponentPopupMenu(popupMenu);

            frame.add(new JScrollPane(table), BorderLayout.CENTER);

            JButton button = new JButton("Click me to fire Popupmenu item");
            button.addActionListener(e -> {
                menuItem.doClick();
            });
            frame.add(button, BorderLayout.PAGE_END);

            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }
}