There is one panel which contains 4 components; 1 JLabel, 1 JButton, 1 JTextField and 1 JPopupMenu
There is a method "testMethod()" in which first I get the number of components from the panel and then the type of component(JButton, JLabel etc).
Now there are 2 scenarios occurring when the control flow goes to testMethod():
I don't get it what is causing this behavior. I searched a lot on the internet and also read JPopupMenu documents but found nothing about this.
Following is the code:
(it is a part of very big code so I put here only that code which is showing the scenario)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestProject extends JFrame {
JPanel panel;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestProject frame = new TestProject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestProject() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testMethod();
}
});
panel.add(btnNewButton);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
// Creating upper level container
JPopupMenu editMenu = new JPopupMenu("Edit");
JMenuItem item1 = new JMenuItem("Item 1");
item1.setBackground(Color.WHITE);
JMenuItem item2 = new JMenuItem("Item 2");
item2.setBackground(Color.WHITE);
item2.setBackground(Color.WHITE);
// Setting Copy action listener
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testMethod();
}
});
// Adding the menu items on the popup menu
editMenu.add(item1);
editMenu.add(item2);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Added check to open menu only on left click
if (e.getButton() == MouseEvent.BUTTON3)
editMenu.show(panel, e.getX(), e.getY());
}
});
// Adding popup menu to the logAreaPanel
panel.add(editMenu);
}
void testMethod()
{
// Here getting number of components of panel
// When coming through JButton click event, it shows 4 components on panel and after get all 4 components
int items = panel.getComponentCount();
Component c;
// Getting all the trees one by one and storing their data into StringBuilder
for(int loopCounter = 0; loopCounter < items; loopCounter++)
{
// here getting different component types
// When coming through JMenuItem click event, it shows 3 components on panel(not getting JPopupMenu) and after only 3 components(not getting JPopupMenu)
c = panel.getComponent(loopCounter);
}
}
}
Any help would be appreciated. :-)
You don't need to add the popup to the component. The preferred way is to just use
panel.setComponentPopupMenu(editMenu);
As per the tutorial, You could use a MouseListener.
//panel.add(editMenu);
panel.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
editMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
That way the component count will always be three, because it is never added to the panel.