简体   繁体   中英

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

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

A popup menu contains JMenuItem s or JMenu s. Either way, the ones with an actual action are the JMenuItem s.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM