I am planning to make a program that in the top of the contentPane has a menubar.
Under this menubar another JPanel, here is what I did (it works), but I don't know if this is the best way:
I made a lot of JPanels with different buttons, I want that a JMenuItem changes the screen(JPanel)
So what I did for each JMenuItem that set the specific JPanel(all panels are in the same position in the GridBagLayout, but all start with .setVisible(false);)
jemnuitem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jpanelItem1.setVisible(true);
jpanelItem2.setVisible(false);
jpanelItem3.setVisible(false);
}
});
jemnuitem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jpanelItem1.setVisible(false);
jpanelItem2.setVisible(true);
jpanelItem3.setVisible(false);
}
});
jemnuitem3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jpanelItem1.setVisible(false);
jpanelItem2.setVisible(false);
jpanelItem3.setVisible(true);
}
});
This works, but I want to know if there is a way better to do this, or can I have a big problem doing this, because if this works, its fine for me work in this way, but I want the help of others that already made something similar.
You should use CardLayout. Then you can switch the visible panel instead of writing clumsy code like you have now.
Related
Which way of implementing an ActionListener is more correct?
Is there any major performance differences?
Implementing an ActionListener to the class:
public class MainFrame implements ActionListener {
JButton exampleButton1 = new JButton();
JButton exampleButton2 = new JButton();
JButton exampleButton3 = new JButton();
public MainFrame(){
exampleButton1.addActionListener(this);
exampleButton2.addActionListener(this);
exampleButton3.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src.equals(exampleButton1)){
//Do something
}else if(src.equals(exampleButton2)){
//Do something
}else if(src.equals(exampleButton3)){
//Do something
}
}
}
Versus adding ActionListeners to each JButton:
public class MainFrame {
JButton exampleButton1 = new JButton();
JButton exampleButton2 = new JButton();
JButton exampleButton3 = new JButton();
public MainFrame(){
exampleButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Do something
}
});
exampleButton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Do something
}
});
exampleButton3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Do something
}
});
}
}
Or perhaps even using Lambdas..?
I would prefer to use an individual Action as the listener for the button. An Action is a slightly more advanced listener that can be used anywhere an ActionListener can be used.
It provides additional functionality such as:
The same Action can be used by multiple components, such as a JButton, JMenuItem
You can disable the Action, which will disable all components that use the Action
It allows to assign mnemonics and accelerators to your components
See the Swing tutorial on How to Use Actions for more information and examples on this concept.
As you can spot, for single ActionListener-approach there are three more branches for each single if test, which button was pressed. There was nothing done yet, so no real action, just testing which button was pressed.
Now, if you want to achieve high quality work there are metrics like branch coverage. Firstly, If you go for the single ActionListener-approach each of your if is creating two branches. So you have to come up with 6 tests to just test if the base idea of your ActionListener is working correctly, so to find out which button was pressed and the correct if part was used. This is some overhead effort.
Secondly, there is the Single Responsibility Paradigm (SRP). It states that each class should be responsibly for one task only. Now, there are three things this single ActionListener is handling.
Thirdly, the reusage of the single ActionListener is very limited and highly depending on the buttons.
Fourthly, I also call this kind of single ActionListener-approach Manual written Object Orientation, because this would be an approach if there was no object orientation and you have to switch or if/else for calling different methods, like exampleButton1Pressed(), exampleButton2Pressed() etc. But, this can be achieved by three dedicated ActionListeners.
So go for dedicated ActionListeners.
I'd like to be able to switch between two possible JPanels in my frame by selecting a certain JMenuItem. What I tried so far:
Action listener in my JMenuBar class:
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(fullList))
gui.switchToFullList();
else if (e.getSource().equals(history))
gui.switchToHistory();
}
In GUI class:
void switchToFullList() {
remove(history);
add(fullList);
}
void switchToHistory() {
remove(fullList);
add(history);
}
where history and fullList are JPanels.
This doesn't seem to modify my frame in any way.
If you want to show one Panel and hide another, they both should ba childreen of your Frame, then you can acces those Panels by: frame.JpanelName.
Example of removing history and adding fullList:
frame.remove(frame.history);
frame.getContentPane().add(frame.fullList);
frame.validate();
frame.repaint();
I just began using JMenu. To ease into it, I decided to use Netbeans form design tool, which has worked great for all components in this app.
Clicking a top-level menu item works great.
For one menu item, I made a submenu with 3 items, each with a mouse click listener.
Here's relevant code for one of the 3 submenus:
private JMenuItem mnuEditDicAddAllScratch;
mnuEditDicAddAllScratch = new JMenuItem();
private void mnuEditDicAddAllScratchMouseClicked(MouseEvent evt) {
new WordsToAdd(); // never happened
}
mnuEditDicAddAllScratch.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
mnuEditDicAddAllScratchMouseClicked(evt);
}
});
mnuEdit.add(mnuEditDicAddAllScratch);
It didn't work. Clicks ignored.
So I tried an Action listener:
private void mnuEditDicAddAllScratchActionPerformed(ActionEvent evt) {
new WordsToAdd(); // WORKED
}
mnuEditDicAddAllScratch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnuEditDicAddAllScratchActionPerformed(evt);
}
});
And it worked.
So a question is, "Why didn't mouse click listener listen?"
Also, "If I should stay away from mouse click events, why or under what circumstances?"
(And, pre-emptive strike: I should stay away from Netbeans form designer.)
You should use the best tool for the job at hand. This means that for JMenuItems and for JButtons, you should use ActionListeners, not MouseListeners (exceptions notwithstanding). For instance if you disable a button, you want the button to not work, right? This works with ActionListeners but not with MouseListeners.
For the best information on this type of stuff, go to the source: Swing Tutorials.
mnuEditDicAddAllScratch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
mnuEditDicAddAllScratchActionPerformed();
}
});
I created a Java app in WindowBuilder for Eclipse. I built a menu and on one of the menu items I added the mouseclicked event.
JMenuItem mitemAbout = new JMenuItem("About");
mitemAbout.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showInternalMessageDialog( form, "Message", "title", JOptionPane.PLAIN_MESSAGE);
}
});
mitemHelp.add(mitemAbout);
I put a breakpoint on the JOptionPane line and when I click on the menu item in debug mode it doesn't even get to it.
Am I completely missing a step here?
Although JMenuItem components offer the addMouseListener method (inherited from java.awt.Component) MouseEvents are only processed for the MenuElements own functional use, i.e. any external MouseEvents will have no effect.
For JMenuItem components, use an ActionListener rather than a MouseListener to listen for user events:
mitemAbout.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
...
}
});
or use:
mitemAbout.setAction(myAction);
First off, I'm new to Java, so please be gentle.
I have a JFrame which has two 'JPanels', one of which is a separate class (i've extended the JPanel). Ideally I'd like to 'dispatch and event' or notify the other JPanel object on the JFrame.
I have an array of JButtons in the custom JPanel class, which i'd like to add an event listener to. Upon clicking the JButton, i'd like to change something on the other JPanel.
I'm really not sure how to do this, so I moved the event handler into the JFrame class, and tried to do the following:
panel.buttonArray[i][j].addActionListener(this);
However, doing that didn't work at all. Annoyingly, Eclipse didn't complain either...
Any tips on how I can achieve this?
This was horribly explained, sorry.
Think of this not in terms of panels but in terms of objects. As long an object, lets say it has a name of object77, has a reference to the other object, call it object42, object77 can call methods on object42.
object77.methodInObject42();
panel77.methodInPanel42();
As for the event handler, then
buttonOnPanelXX.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
panel77.methodInPanel42();
}});
or even better...
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
panel77.methodInPanel42();
}});
}});