I'm trying to separate function from state in my GUI application by the use of Action objects. I've been successful in using these to create menu items and buttons that have the same functionality.
My problem is this: I want to have the same Action for both the 'exit' item in my menu and the close button of the frame.
Currently I've been able to solve it by adding the following WindowListener to the frame:
private class MainWindowListener extends WindowAdapter {
#Override
public void windowClosing(WindowEvent e) {
new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit"));
}
}
Isn't there a simpler more straightforward way to do this?
Forwarding events is handy, but you can also use dispatchEvent(), as shown here.
Addendum: More examples that use Action are shown below.
LinePanel which connects buttons and keys.
ScrollAction which leverages existing Swing actions.
KeyPadPanel which illustrates forwarding actions.
GraphPanel which shows graph editor actions in a tool bar.
Related
In my app I display a popup using JPopupMenu. I want to run some code when this popup closes (either directly, programmatically or when escape key is pressed). For windows I can attach a WindowListener but JPopupMenu doesn't have any corresponding feature, and SwingUtilities.windowForComponent returns the root window of the app. How do I implement this?
How about adding a PopupMenuListener to it? Something like:
jpopMenu.addPopupMenuListener(new PopupMenuListener
{
public void popupMenuCanceled(PopupMenuEvent popupMenuEvent)
{
//here the code you want to be executed at close
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent){}
public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {}
}
This should be automatically executed when you cancel/close the popMenu. I didn't add code to the other two methods, but feel free to play with them if needed.
So I was trying to build a custom UI among which there was back arrow (not the one that can be used from Form's toolbar in Codenameone) like this:
Now the issue is when I want to implement the actionListener of the back button, I don't seem to find the method to pop itself.
Yeah there is native support for back button with toolbar something similar to this :
form.getToolbar().setBackCommand(title, evt -> menuCard.getComponentForm().showBack());
But i want to do something like this :
backButton.addActionListener {
form.dispose()
print("the form should dispose")
}
where I want to hook the logic to dispose / dismiss the form to an actionListener of a Button I can't do that as dispose() on Formis package private .
Then I did a hack
package com.codename1.ui;
import com.codename1.ui.layouts.Layout
class CustomForm constructor(contentPaneLayout: Layout) : Form(contentPaneLayout) {
public override fun dispose() {
super.dispose()
}
}
But calling dispose() on this new Form does not dismiss the form either.
What is the right way to dismiss/dispose/pop a form from its's screen to show the previous form?
I'm programming a web browser with multiple classes. One class is "navbar" which holds most of the buttons such as search, back, forward, etc.
The navbar class has action listeners which, when the search button is pressed, become active, and lead to the production of a URL, which needs to be passed to the JEditorPane, but the editor pane is in a different class, "editor".
It does not make sense for an editor to be instantiated inside navbar, so how can i pass the variable from the navbar class to the editor class?
Is it okay to use statics in this situation?
Why not simply give the nav bar a reference to the editor? This could be done in the constructor, if the editor is instantiated first, or via getter/setter pair (actually, only the setter is really needed...). If the nav bar has such a reference, it can call the appropriate methods from within the listener implementation.
class Editor { ... }
class NavBar implements ActionListener
{
public NavBar(Editor editor)
{
myEditor = editor;
}
public void actionPerformed (ActionEvent e)
{
// call methods of myEditor
}
private Editor myEditor;
}
Editor theEditor = new Editor();
NavBar theNavBar = new NavBar(theEditor);
You can use some kind of intermediate interface between these, typically a service like layer. Which is basically what the observer pattern is as well. The editor could list events the navbar produces, but that also means later on you could also catch these events in other components
I want to set components invisible in the netbeans design view and then show then from coding when some event occurs. Is it possible?
When you create NewJFrameForm using Netbeans, In Design view you can drag and drop all the components available in palette.
In order to set initially invisible / hidden at starting you have to do it manually.
Click on source above, Now you can see generated source of that frame you designed.
You will see constructor as:
public NewJFrame() {
initComponents();
}
Generated itself.
now you have to put your own code to update like in my case i will set invisible my compnents like:
jPanel1.setVisible(false);
OR
specific components:
jButton1.setVisible(false);
jToggleButton1.setVisible(false);
jLabel1.setVisible(false);
if prefer this like:
public NewJFrame() {
initComponents();
mySettings();
}
public void mySettings(){
//Hide or set initial Values of components
}
Note:
all your generated code is in
initComponents();
you can not edit it in source, have to do it in design view
Does JavaFX contain an equivalent to java.awt.EventQueue? For my application I need to intercept all GUI related events such as mouse and keyboard input, and so far I haven't been able to find a way to do it without attaching listeners to every GUI element.
This isn't quite the same as the question in your title, but to intercept all events, you can add an EventFilter to the Scene:
scene.addEventFilter(Event.ANY, new EventHandler<Event>() {
#Override
public void handle(Event event) {
System.out.println(event);
}
});
If you need to veto the event, call event.consume()