How should i implement an MVC controller with multiple JButtons on the view?
for example: i have a start button, stop button and many others as well.
I tried to do this for the start button and it works fine but then how do i implement it for a stop button trigger?
Controller Code:
public MVCAuctionController(Auction a, MVCAuctionView v) {
auction = a;
view = v;
view.addProcessBidsListener(new ProcessBidsController());
view.addStopProcessListener(new StopBidsController());
}
class ProcessBidsController implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
view.disableProcessButton();
Thread thread = new Thread (auction);
thread.start();
}
}
addProcessBidsListener - is associated with the START/Process button,When i click on the button - the thread starts running and fills the JTextArea with data.
Now my Stop button should stop the thread. For this if i do something like this it doesnt actually stop the Thread:
class ProcessStartController implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == view.start){
view.disableStartButton();
new Thread (rest).start();
//thread.start();
System.out.println("inside action performed of start button");
view.kitchen.append("Orders to kitchen");
}
else if (e.getSource() == view.stop)
{
new Thread (rest).interrupt();
}
}
}
Use an Action for each button, they are self contained controllers
See How to use Actions for more details
You can then set up some kind of relationship between the Actions and the main controller should you need it, via some kind listener for example
Related
I have a Question on performing other buttons action with single button click. Some example code for three buttons:
JButton a = new JButton("a");
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Action of a is Here
}
});
JButton b = new JButton("b");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Action of b is Here
}
});
Those should come together, like:
JButton c = new JButton("c");
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Action of c is Here
// Action of a
// Action of b
}
});
In the above example i have three buttons a,b,c with its own action; but as you can see, C also has to run the actions of A and B. What are good ways to address this?
The other answers are all correct, but there is one important aspect missing here: be careful about dong "too many things" on the AWT event dispatcher thread.
Meaning: when a button is clicked, an event gets created, and the UI framework uses that special thread to trigger the registered listeners. If one of the listeners now decides to do a intensive computation ... the UI event threads stays busy doing "that". And while doing "that thing"; this thread isn't available to dispatch any other UI event.
So, this is "not only" about creating methodA(), methodB(), methodC() and invoking them in your third action listener. It is also about understanding if combining multiple calls becomes subject to "I should better run those things in a separate thread; to not block the event dispatcher thread".
In that sense: the other answers tell you where to go from here; but be really careful about the "amount of activity" that your "joined actions" button is about to create!
1) Methods
Use methods for each action and call those in the ActionListener.actionPerformed
public void methodA(){}
public void methodB(){
methodA();
}
2) Action instance
You could create your own classes of ActionListener to perform the actions
First action :
class ActionA implements ActionListener{
public void actionPerformed(ActionEvent e) {
...
}
}
An improved action
class ActionB extends ActionA{
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e); //Will call the first action
...
}
}
This is limited since you can't have multiple extends but is also a nice solution
3) Click
Last but I don't like it, use AbstractButton.doClick to dynamicly click on other buttons.
4) Add multiple action
Just notice that the methods is not a setActionListener but a addActionListener meaning that it will accept multiple ActionListener.
So define create two instances
ActionListener listenerA = new ActionLisener ..
ActionListener listenerB = new ActionLisener ..
buttonA.addActionListener(listenerA);
buttonB.addActionListener(listenerB);
buttonC.addActionListener(listenerA);
buttonC.addActionListener(listenerB);
With a small test, I notice that the actions are execute in the order B -> A (might not be a generality).
As said in comment, this should be us knowing the risk, this will . If an action failed because of an exception, should the next one be executed ? By default it won't because the process will not hide exceptions.
I would restrict this solution to GUI management like reseting fields, disabling, ... that could be use in different buttons.
Whatever you want to do on Button click a, you can put in a method and call it from wherever you want.
public void methodForA(){
// do here what you want
}
You can call this now in the methods you want it to call from. In your case from button click A and button click C
JButton a = new JButton("a");
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
methodForA();
}
});
// and also in your c-Button
JButton c = new JButton("c");
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Action of c is Here
methodForA();
}
});
Create 3 methods for each button indepently from the actionListeners action Perform method and call them from the actionPerfomed methods:
private void btnAClicked(){};
private void btnBClicked(){};
private void btnCClicked(){};
JButton c = new JButton("c");
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCClicked();
btnAClicked();
btnBClicked();
}
});
I have extended JTextField. There are multiple ActionListeners attached to my class, but I need one in particular to always fire first. Is there a way to ensure that a particular ActionEvent always first first?
Note that I do have a reference to my ActionListener. I am assuming I need to override a method, but I am not sure which one that is.
Is there a way to ensure that a particular ActionEvent always first first?
The Java spec does not guarantee the order in which events are fired.
However, I believe the default implementation is that the last ActionListener added to a component is first first.
Edit:
I use the mouse listener
Why are you using a MouseListener? What happens when the user tabs to/from the field? Don't assume mouse usage. A FocusListener will handle either keyboard or mouse activity in this case.
The problem comes into play when the user presses enter on the text field.
What happens if the user doesn't press enter and they just tab to the next field? That's what I would do.
This needs to happen before other action listners do their thing.
What other ActionListeners? A tgext field would only have a single listener.
Anyway do maybe answer your question you can effectively change the order of execution by using SwingUtilities.invokeLater(...) to place your code on the end of the Event Dispatch Thread so it executes after all other events:
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// add your code here
}
});
}
Again, any solution that depends on the order of events is not a good solution. The order of event could be different on different platforms.
Try to override fireActionPerformed from JTextField. Add custom ActionListener in new class and call him before call super.fireActionPerformed()
Ps: sorry for bad english.
Edit:
public class CustomTextField extends JTextField {
private List<ActionListener> listeners;
public synchronized void addPriorityActionListener(ActionListener l) {
if(l == null) {
return;
}
if(listeners == null) {
listeners = new ArrayList<>();
}
listeners.add(l);
}
public synchronized void removePriorityActionListener(ActionListener l) {
if(l == null || listeners == null) {
return;
}
listeners.remove(l);
}
protected void firePriorityActionPerformed() {
if(listeners == null) {
return;
}
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getText());
for(ActionListener listener: listeners) {
listener.actionPerformed(event);
}
}
#Override
protected void fireActionPerformed() {
firePriorityActionPerformed();
super.fireActionPerformed();
}
}
I'm trying to figure out how to make it so that my navigation menu, when clicked, will open appropriate panels within my GWT page.
Here's a part of my code:
#Override
public void onModuleLoad()
{
MainNavBar nb = new MainNavbar();
NavItem i = new NavItem("1", "TestNavItem");
nb.add(i);
i = new NavItem("2", "TestNavItem2");
nb.add(i);
}
So when I run the project, I see that I have a menu on the test site:
So my question is, how can I have an event handler such that when either one of those are clicked, the panel to the right will be changed to something else?
Thanks!
create an actionListener class,
public class listen implements actionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == ObjectName){
// Your code goes here....
}
}
}
then create an object of this class e.g
listen listener = new listen();
YourObjectName.addActionListener(listener);
Don't forget to make the imports, hope this helps...
I am having trouble with the design of my code. I have 3 buttons not in a button group. I want to - based on the selected button - perform an action. Now the action requires a modification of an object in the class. This means i cannot use an inner class because this does not have access to the outer. If i could add an event listener to a button group this would be much easier but as i see it i will need an event handler for each radio button, is this correct? If not how else can i do it? Thanks
A quick example
public class Test(){
RadioButton 1 = new RadoButton();
RadioButton 2 = new RadoButton();
RadioButton 3 = new RadoButton();
Object myObject = new Object();
public void clickEvent(){
if(1.isSelected()){
myObject.doOne();
}else if(2.isSelected()){
myObject.doTwo();
}.....
}
}
You can set the same listener to all your buttons.
Pseudo code:
radioButton1 = new RadioButton();
radioButton2 = new RadioButton();
radioButton3 = new RadioButton();
listener = new ActionListener() {
...
}
radioButton1.addActionListener(listener);
radioButton2.addActionListener(listener);
radioButton3.addActionListener(listener);
This is to illustrate how you can use an inner class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
public class TestInnerClass {
JRadioButton radioOne = new JRadioButton();
JRadioButton radioTwo = new JRadioButton();
JRadioButton radioThree = new JRadioButton();
Object myObject = new Object();
public TestInnerClass() {
ActionListener myInnerClass = new MyActionListener();
radioOne.addActionListener(myInnerClass);
radioTwo.addActionListener(myInnerClass);
radioThree.addActionListener(myInnerClass);
}
private class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
if(radioOne.isSelected()) myObject.toString();
else if(radioTwo.isSelected()) myObject.notify();
else if(radioThree.isSelected()) myObject.getClass().getName();
}
}
}
Note how the inner class is not static, as stated in gontard's comment, so does have visibility to myObject. And it's safest to keep it private.
It's often good to have one listener handle all events, as in this case. There are, however, other cases where you want your event handling more specific to each component. For example, in these cases, radioThree could trigger the event, and since these buttons are not in a group it might be possible that radioOne is still in the selected state. This single handler will fire and act only on the first radio. While one way to fix this would be to add checks for the source, as in:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == radioOne && radioOne.isSelected())
myObject.toString();
if(event.getSource() == radioTwo && radioTwo.isSelected())
myObject.notify();
if(event.getSource() == radioThree && radioThree.isSelected())
myObject.getClass().getName();
}
Another way would be to use one listener for each component. That's where the anonymous class comes in very handy:
radioOne.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myObject.doOne();
}
});
One of my favorite patterns, especially if the work is non-trivial, is to first create a method to do the work, then call it from the listener. I also wrap the call in SwingUtilities.invokeLater() to get the work off the Swing Event Thread.
public class Test {
JRadioButton radioOne = new JRadioButton();
Object myObject = new Object();
private void handleRadioOne() {
myObject.toString();
// etc, etc.
}
public Test() {
radioOne.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
handleRadioOne();
}
});
}
});
}
}
This provides two nice features:
It encapsulates your action work into a method allowing for programmatic access if later desired
It ensures that the method's work stays off the Swing Event Thread, so your GUI won't hang up during intensive processing
I've implemented right mouse click for open menu listener on my main Jframe, it works fine except one problem. One out of 5 (give or take) clicks it not responding, this can be very annoying for the user. Here is my code:
contentPane = new JPanel();
contentPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
//Do Stuff
}
}
});
Can you please help me
You won't get clicks from sub-components of contentPane.
I think your problem is that you have added things to your panel. When the user clicks at regions occupied by a sub-component, that sub-component get's the click event.
Quick fix: I would recommend you to add the same mouse listener to all sub-components.
You are not "clicking"
A click is when the mouse is pressed and release really quickly. If you are not careful you might get events for (for instance) "pressed, moved, released" instead of "clicked".
Quick fix: use mouseReleased event instead.
Use this Code instead:
private MouseAdapter listener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (downer) {
downer = false;
if (new Rectangle(e.getComponent().getLocationOnScreen(), e.getComponent().getSize())
.contains(e.getLocationOnScreen())) {
downer = false;
// CODE
new Thread(new Runnable(){
public void run(){
//Your Listener code
}
}).start();
/// COde
}
}
}
boolean downer = false;
public void mousePressed(java.awt.event.MouseEvent e) {
downer = true;
}
};
This code only reacts if you press on the component and release on the component AND starts a new Thread for the custom task. This should work allways, because the AWT Thread isnt blocked with long calculations.