Multiple swt buttons, same listener [duplicate] - java

This question already has an answer here:
Creating multiple identical text verify listeners in eclipse-rcp/swt
(1 answer)
Closed 8 years ago.
I have an application which I want to develop in SWT, and I was wondering if there is a possibility to use a single Selection Listener for multiple buttons. For example I have a menu bar which contains an "open" menu item, and I also have a toolbar where I have an open button, can I use the same listener for both? I would be glad to see just a simple example if there is a possibility, and an explanation if there is no possibility to do what's above.

In general it's possible ... your listener will have to check the event's source attribute to determine where the event was fired from.
If you're attaching the listener to different object's, you probably want to implement a generic Listener instead of an object specific listener.

Related

how to set a listener for the selected text in TextView in java? [duplicate]

This question already has answers here:
How to set up a listener on the selected text in TextView
(3 answers)
Closed 1 year ago.
i want a Help just which way i follow to get a listener for the selected text in TextView,
what i want is when the user select a specified text, a button of options will appear.
i hope you help me
Have you tried ActionListener? It will be called when user interacts or performs any action on that particular component. You can get the selected content by component.getSelectedText() or similar method. Do not write separate listeners for mouse and keyboard operations as it might interfere with one another and cause problems.

Own Tabs Container of Buttons: how to initialize all of them doing the same?

I just created my own Container, its structure looks like this:
Container
|- TabsContainer
|-Button1
|-Button2
|-Button3
...
It should be like this:
and be positioned at the bottom of the screen, like this:
in every Form I create.
When I add this custom Container to the 4 of my Forms, I still want all of the buttons to do the exact same thing. How could I do this? And In what function? before?
I already tried onPostShow()of my login Screen by getting all of them with their unique component root and adding an actionListener, but it did not work.
Furthermore the tabs are no "blank containers" but still depicting the buttons, but they should not. See here:
How can I solve these two issues?
To get this to work globally, you need to add the actionListenerdirectly to the buttons.
After creating your Container and adding the Buttons, right click on them one after the other and select Event -> Action Event. This will generate onComponentAction method where you can write your code for that button.
You should get something similar to this:
#Override
protected void onContainer_Button1Action(Component c, ActionEvent event) {
//Write all you want this button to do here
}
I'm sure you're trying to avoid code repetition, as mentioned in your previous question. Using universal container is more secure and less prone to errors than the earlier method.
Codename One also supports the EmbeddedContainer functionality in the old GUI builder. So you can create a Container instance for the 6 tabs (in the add new GUI element menu pick "Container") and add them to the tabs as an EmbeddedContainer UI. Note that this isn't supported by the new GUI builder which is based on a more traditional GUI builder approach and it might make navigation behavior in the app somewhat "odd".
In the new GUI builder you can just write generic code to map to this as the form would just map directly to a single class.

How to write a code for default close button in JFrame? [duplicate]

This question already has answers here:
setDefaultCloseOperation to show a JFrame instead
(3 answers)
Closed 8 years ago.
I want to write a code to default close button in JFrame. I just want to ask whether "to save the content or not?" using another JDialog or JOptionPane when click on close button.
Where do I have to write the code? As default close button is not in design of JFrame, and no such event found for JFrame window too, how can I add event listener to that button?
Check out Closing an Application for a couple of solutions:
using a WindowListener and overriding the windowClosing(...) event to display your option pane.
using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.

How do I reset/clear JRadioButton in Java [duplicate]

This question already has an answer here:
Clearing a group of radio buttons in Java
(1 answer)
Closed 9 years ago.
How do you reset/clear a JRadiobutton? clearing a TextField is as simple as surenameField.setText(""); is there any similar way with the RadioButton?
You mean
radioButton.setSelected(false);
?
If you want to clear the text of a JRadioButton named radio, you would call radio.setText(""). If you want to de-select the button, you call radio.setSelected(false). Note that this does not actually fire events, so if you have a listener registered to respond to de-select events, this won't notify that listener.
For more information, you can read the official documentation for JRadioButton here
JRadioButton API
Could you specify what you mean by "reset"?
The following things are possible:
Remove the button from the program: you can do this by calling the remove method from its container ([container].remove([radioButton], eg. myPanel.remove(myRadioButton).
Remove the text from the button: you can do this by doing what you set: myRadioButton.setText("")

Instanceof, Enum Or Multiple Listeners for JButtons

On my swing GUI I have lines of data and a number of buttons, the user selects a number of items and then then selects a button.
Each button applies a different rule to the data and so different functions need to be called for every button, I'm using an MVC design pattern and my question is such, How should I handle the different needs of every button?
Create a class 'MyButton' which extends JButton then give this some sort of Enum, I can then create 1 action listener and then check which button has been pressed in the ActionListener by inspecting the Enum.
Similar to above but with a different class for each button then using instanceof to determine which has been pressed.
Implement a separate ActionListener for each button
Other?
Which is the best method to use if any? Any advice would be greatly received!
Implement a separate listener for each button.
First because it's the usual solution. Second, between there's no reason to extend JButton just to do something else when it's clicked. That's the role of the ActionListener. Swing components are designed to be used as is, and you should generally not extend them.
It's MVC: you separate the logic (in Actions) and the view (the button).
There is no need to use an enum or to subclass JButton. What you can do to keep things clean when you have dozens of buttons, is a factory class to create Action instances.
If I get your question correctly, you mean to say, you have a data in line items and every line items have a button, which when pressed invokes a rule pertaining to the line item.
If so, then
If you take the 2nd approach, you need to code inside your action listener every time a new line item added in future.
Third approach will also have same implication as above
First approach sounds quite good. You can have a Factory which may have a hashmap keyed with the enum variables and the respective rule. Inside the action listener get the rule from the factory and invoke it.
This way you get a proper separation of concerns and your action listener will act as a controller, having no knowledge of rules and data items.

Categories

Resources