1
I need to know how to make one JMenuItem
open a JPanel
, that is, by clicking on JMenuItem
, open the corresponding Jpanel, this in Eclipse, via windowBuilder.
This is the image of my screen with the menu:
!Screenshot of the screen with the menu1
Jpanel call code:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Agenda1 window = new Agenda1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Agenda1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
-- -- -
JPanel panelEditar = new JPanel();
frame.getContentPane().add(panelEditar, "name_5905325036674");
panelEditar.setLayout(new CardLayout(0, 0));
-- -- --
JMenuItem mntmEditar = new JMenuItem("Editar");
mntmEditar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
CardLayout card = (CardLayout) frame.getContentPane().getLayout();
card.show(frame.getContentPane(), "panelEditar");
}
});
The complete code here: http://pastebin.com/6rn7sSG6
How are you building your window? The jpanel will open where? To make it easier to help, edit the question and add a [mcve] because it is so wide.
– user28595
http://postimg.org/image/69bpmqibd/ I want the edit button upstairs to open the panelEdit - tell me if you need more data
– Rui Mendes
Add to the question, with code too, it is difficult to simulate a problem per image.
– user28595
Rui, add to question, by clicking [Edit]. Here in the comments it is embossed. Add both the image and the code and format the code by selecting it and clicking on
{}
– user28595
You want to change the display panel to the panelEdit ?
– user28595
yes by clicking Edit(jmenuitem)
– Rui Mendes
Which panel do you want to add? The Edit pane has already been added. Your question is too confusing. Edit it explaining better, with a code more according to your doubt.
– user28595
I don’t want to add any panel. I want that when running the program the user click event -> edit and open the panelEditar, the image is showing another panel, and when I run I can’t switch to another 'page' or panel, if you can help thank
– Rui Mendes
So the problem is not in this code. In it you are already displaying the panel. Edit the question and add a code more according to your problem.
– user28595
Because I also thought that this part of the code is well done.... I don’t know where else I can be wrong... I can’t find any example where someone does this to see what I have to change... and my code has 300 lines I don’t know that I show you more...
– Rui Mendes
To change the panel displayed, see the code of that answer
– user28595
Is the mistake in the part I put in now?
– Rui Mendes
Which panel is displayed when opening the window?
– user28595
http://postimg.org/image/qy1yzgoc9/
– Rui Mendes
Try
panelEditar.setVisible(true)
within theActionListener
– user28595
did not work :(
– Rui Mendes
Rui, see if you can solve the answer below.
– user28595
did not resolve :(
– Rui Mendes