In JavaFX when I use a TreeView, whenever I click on the main menus (which have submenus) sometimes they "stuck" and don't show me their submenus.
In 10 clicks seven is okay, 3 is not. I observed, that the problem shows up only, if I click to another main menu that I am in. For example:
>File
New File
Open File
Close
>Tools
Settings
So if I click: File, it shows me it's submenus (New, Open, Close). If I click it again, it closes the submenus. BUT!
When I click to another submenu, just like "Settings" and after I click to File (to expand, or collapse it's submenus) I get this error message:
java.lang.NullPointerException
at utiokos_program.FoablakController$1.changed(FoablakController.java:166)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:101)
at javafx.scene.control.MultipleSelectionModelBase$1.invalidated(MultipleSelectionModelBase.java:65)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:195)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:161)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:130)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:67)
at javafx.scene.control.MultipleSelectionModelBase.shiftSelection(MultipleSelectionModelBase.java:253)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1114)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1059)
at com.sun.javafx.scene.control.WeakEventHandler.handle(WeakEventHandler.java:62)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.control.TreeItem.fireEvent(TreeItem.java:713)
at javafx.scene.control.TreeItem.access$300(TreeItem.java:197)
at javafx.scene.control.TreeItem$5.invalidated(TreeItem.java:529)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:81)
at javafx.scene.control.TreeItem.setExpanded(TreeItem.java:509)
at com.sun.javafx.scene.control.behavior.TreeCellBehavior.doSelect(TreeCellBehavior.java:192)
at com.sun.javafx.scene.control.behavior.TreeCellBehavior.mousePressed(TreeCellBehavior.java:122)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:335)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3164)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
After I click it again, and it is okay, it shows me it's subs again! (Since now the File was the last I clicked and not the Settings)
//One possible solution would be to disable the treeview's collapse, and then I use it everytime in expanded mode. Is it possible to disable collapsing in JavaFX Treeview?
//I copy the whole FoablakController.java on request
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.application.Platform;
import javafx.scene.layout.StackPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventDispatchChain;
import javafx.event.EventDispatcher;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
public class FoablakController implements Initializable {
public Windows operation = null;
private final Node fileicon = new ImageView(new Image(getClass().getResourceAsStream("file.png")));
private final Node editicon = new ImageView(new Image(getClass().getResourceAsStream("edit.png")));
private final Node windowsicon = new ImageView(new Image(getClass().getResourceAsStream("windows.png")));
private final Node toolsicon = new ImageView(new Image(getClass().getResourceAsStream("tools.png")));
private final Node helpicon = new ImageView(new Image(getClass().getResourceAsStream("help.png")));
#FXML
public static StackPane menuholder;
#FXML
public static StackPane main_right;
#FXML
public static StackPane help_right;
#FXML
public static StackPane contact_right;
#FXML
public static StackPane settings_right;
#FXML
public static StackPane contrast_right;
#FXML
public static StackPane comments_right;
#FXML
public static StackPane nice_right;
#FXML
public static StackPane faq_right;
#FXML
public TextArea summary;
#FXML
public static TextArea textArea_help_summary;
#FXML
public Button btn_print;
#FXML
public void Quitgomb (ActionEvent event) {
Platform.exit();
}
public void ClearScreen(){
main_right.setVisible(false);
help_right.setVisible(false);
settings_right.setVisible(false);
contrast_right.setVisible(false);
contact_right.setVisible(false);
comments_right.setVisible(false);
nice_right.setVisible(false);
faq_right.setVisible(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
TreeItem<String> treeItemRoot1 = new TreeItem<> ("Menu");
TreeItem<String> nodeItemA = new TreeItem<>("File",fileicon);
TreeItem<String> nodeItemB = new TreeItem<>("Edit",editicon);
TreeItem<String> nodeItemC = new TreeItem<>("Windows",windowsicon);
TreeItem<String> nodeItemD = new TreeItem<>("Tools",toolsicon);
TreeItem<String> nodeItemE = new TreeItem<>("Help",helpicon);
treeItemRoot1.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC,nodeItemD,nodeItemE);
nodeItemA.setExpanded(true);
nodeItemB.setExpanded(true);
nodeItemC.setExpanded(true);
nodeItemD.setExpanded(true);
nodeItemE.setExpanded(true);
TreeItem<String> nodeItemA1 = new TreeItem<>("New File");
TreeItem<String> nodeItemA2 = new TreeItem<>("Open");
TreeItem<String> nodeItemA3 = new TreeItem<>("Quit");
nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3);
TreeItem<String> nodeItemB1 = new TreeItem<>("Edit A");
TreeItem<String> nodeItemB2 = new TreeItem<>("Edit B");
TreeItem<String> nodeItemB3 = new TreeItem<>("Edit C");
TreeItem<String> nodeItemB4 = new TreeItem<>("Edit D");
TreeItem<String> nodeItemB5 = new TreeItem<>("Edit E");
TreeItem<String> nodeItemB6 = new TreeItem<>("Edit F");
nodeItemB.getChildren().addAll(nodeItemB1, nodeItemB2, nodeItemB3, nodeItemB4, nodeItemB5, nodeItemB6);
TreeItem<String> nodeItemC1 = new TreeItem<>("Windows Settings");
TreeItem<String> nodeItemC2 = new TreeItem<>("Windows Brightness");
TreeItem<String> nodeItemC3 = new TreeItem<>("Windows Quit");
nodeItemC.getChildren().addAll(nodeItemC1, nodeItemC2, nodeItemC3);
TreeItem<String> nodeItemD1 = new TreeItem<>("Settings");
TreeItem<String> nodeItemD2 = new TreeItem<>("Print");
nodeItemD.getChildren().addAll(nodeItemD1, nodeItemD2);
TreeItem<String> nodeItemE1 = new TreeItem<>("Help");
TreeItem<String> nodeItemE2 = new TreeItem<>("FAQ");
nodeItemE.getChildren().addAll(nodeItemE1, nodeItemE2);
TreeView<String> treeView = new TreeView<>(treeItemRoot1);
treeView.setShowRoot(false);
menuholder.getChildren().add(treeView);
//
treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
public void changed(ObservableValue observable, Object oldValue,
Object newValue) {
operation = new Windows();
TreeItem<String> selectedItem = (TreeItem<String>) newValue;
String actualmenu;
**actualmenu = selectedItem.getValue();**
if (null != actualmenu) {
switch (actualmenu) {
case "File":
try{
selectedItem.setExpanded(true);
}
catch (Exception ex) {
Logger.getLogger(FoablakController.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case "Edit":
selectedItem.setExpanded(true);
break;
case "Windows":
selectedItem.setExpanded(true);
break;
case "Tools":
selectedItem.setExpanded(true);
break;
case "Help":
selectedItem.setExpanded(true);
break;
case "New File":
try {
menuholder.setDisable(true);
textArea_help_summary.setDisable(true);
operation.first();
} catch (Exception ex) {
Logger.getLogger(FoablakController.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case "Open":
System.out.println("OK");
break;
case "Quit":
System.out.println("OK");
break;
case "Edit A":
System.out.println("OK");
break;
case "Edit B":
ClearScreen();
contrast_right.setVisible(true);
case "Edit C":
break;
case "Edit D":
System.out.println("OK");
break;
case "Edit E":
System.out.println("OK");
break;
}
}
}
});
}
}
Sir;
Solution 1
This is what i think is happening, ChangeListener listens to change events, whey you click an item in the TreeView it is triggered, when you click a sub-item in it, it is also triggered, with that when you try to collapse an item, it is triggered twice, but one for the collapsing which does not return any value(hence your newValue is null so when you try to assign an object to null, it is null, therefore calling a function on a null object results in NPE) and the second for the TreeItem selection, which returns the child TreeItem as a value. so quick fix my change listener is a little different from yours in terms of the type parameters,but its no biggie
ChangeListener<TreeItem<String>>() {
public void changed(ObservableValue<? extends TreeItem<String>> observable,TreeItem<String> oldValue, TreeItem<String> newValue) {
TreeItem<String> selectedItem;
if(newValue != null){
selectedItem = newValue;
}else{
selectedItem = oldValue; // here it s null so i am assigning it
// to the old value
}
// add the other codes
Solution 2
Actually this solutin 2 goes to your question "One possible solution would be to disable the treeview's collapse, and then I use it everytime in expanded mode. Is it possible to disable collapsing in JavaFX Treeview?" the answer is YES.. here
yourTreeItem.addEventHandler(TreeItem.branchCollapsedEvent(),
new EventHandler<TreeModificationEvent<String>>() {
#Override
public void handle(TreeModificationEvent<String> event) {
event.getTreeItem().setExpanded(true);
}
});
hope it helps
Related
I am trying to use jcef browser inside my swing application but getting problems.
First of all i unable to add jcef browser as JPanel component on jFrame. Then i try to add directly on jframe
[code]getContentPane().add(browser.getUIComponent(), BorderLayout.CENTER);[/code]
Now when browser window load inside JFrame, and If i want to switch with other Jpanel then it is not working working in any way
I cant switch the screen after loading CEF browser. Can any one point out what i need to do. Here is my test jframe.
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CardLayoutTst extends JFrame {
static CefBrowser browser = null;
static CefClient client = null;
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTst() {
setTitle("Test med CardLayout");
setSize(400, 300);
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(browser.getUIComponent(), "2");
btn1 = new JButton("Show Card 1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "1");
}
});
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn1);
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
public static void main(String[] args) {
client = CefApp.getInstance().createClient();
browser = client.createBrowser("http://www.google.com", false, false);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CardLayoutTst frame = new CardLayoutTst();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
It is a bit late but I had just the same problem. A friend of mine solved the issue.
You have to create CefSettings, set the windowless_rendering_enabled to false and pass this settings object to the getInstance call:
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = false;
client = CefApp.getInstance(settings).createClient();
I'm trying execute a method when i change to a specific tab.
If is not selected, when i choose it i need to lauch it.
this is what i am actually doing:
class Graph{
#FXML private TabPane Graphics;
public void llenarResponsables() throws SQLException{
IDProceso=cbProceso.getSelectionModel().getSelectedItem().getIdProceso();
IDIndicador = cbIndicador.getSelectionModel().getSelectedItem().getIdIndicador();
List<Responsables> responsables = new ArrayList<Responsables>();
cdFormatoAnalisis oFormatoAnalisis = new cdFormatoAnalisis();
responsables = oFormatoAnalisis.listarResponsables();
ObservableList<Responsables> tvLlenar = FXCollections.observableArrayList(responsables);
tcNombre.setCellValueFactory(new PropertyValueFac`tory<Responsables,String>("Usuario"));
tcNombre.setResizable(false);
tcPuesto.setCellValueFactory(new PropertyValueFactory<Responsables,String>("Categoria"));
tcPuesto.setResizable(false);
tvResponsables.setItems(tvLlenar);
Graphics.setOnMouseClicked();
}
Well I am not sure what you are trying to do in the method mentioned about, but if you want to fire events on change of tabs, you can use the ChangeListener on your TabPane
A working example
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
public class FireEventsOnTabChange extends Application {
#Override
public void start(Stage stage) throws Exception {
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Tab1");
Tab tab2 = new Tab("Tab2");
Tab tab3 = new Tab("Tab3");
tabPane.getTabs().addAll(tab1, tab2, tab3);
Scene scene = new Scene(tabPane, 200, 200);
stage.setScene(scene);
stage.show();
tabPane.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<Tab>() {
#Override
public void changed(ObservableValue<? extends Tab> old,
Tab oldTab, Tab newTab) {
//Check for Tab and call you method here
System.out.println("You have selected "
+ newTab.getText());
}
});
}
public static void main(String[] args) {
launch(args);
}
}
So, I'm developing a program using the Swing library and I obviously have buttons and menu items. Some of these are supposed to do the same stuff, and I thought using the Command Pattern should be the way to do it, e.g. I have a "save" button and a "save" menu item and they have to implement the same saving algorithm.
Command Pattern seems to be ok but I can't get who's the receiver in all that. Isn't a comand supposed to work on an object which implements some sort of "receiver interface", so that you can use different commands on different receivers coupling them aribtrarily? It looks like there's no "receiver" in my implementation of the pattern.
Another doubt i have is should a command be implemented as a singleton, since you could potentially call its functions from differents parts of the same project, and it would be handly to instantiate it only once and make it statically invokable?
Thank you.
" I obviously have buttons and menu items. Some of these are supposed to do the same stuff,"
As #nIcEcOw noted, that's what Actions are for. This Answer Shows exactly this.
As stated in the How to use Actions :
An Action can be used to separate functionality and state from a component. For example, if you have two or more components that perform the same function, consider using an Action object to implement the function. An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.
An There only three Actions. Ont to open, save, and new. Each Action has an ActionCommand, and icon, and and action to perform. Both the JMenuItem and JToolBar button share the same Action and do the same thing. Here is the code you can run.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class ActionTest {
public ActionTest() {
ImageIcon openIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/open.gif"));
ImageIcon saveIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/save.gif"));
ImageIcon newIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/new.gif"));
Action openAction = new AbstractAction("Open", openIcon) {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Open File");
}
};
Action saveAction = new AbstractAction("Save", saveIcon) {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Save File");
}
};
Action newAction = new AbstractAction("New", newIcon) {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("New File");
}
};
JMenuItem openMenuItem = new JMenuItem(openAction);
JMenuItem saveMenuItem = new JMenuItem(saveAction);
JMenuItem newMenuItem = new JMenuItem(newAction);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(newMenuItem);
menuBar.add(fileMenu);
JToolBar toolBar = new JToolBar();
toolBar.add(Box.createHorizontalGlue());
toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
toolBar.add(newAction);
toolBar.add(openAction);
toolBar.add(saveAction);
JFrame frame = new JFrame("Toolbar and Menu Test");
frame.setJMenuBar(menuBar);
frame.add(toolBar, BorderLayout.PAGE_START);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ActionTest();
}
});
}
}
As stated in the quote from the above mentioned tutorial, you can do more than just add an image and an action command to the Action. You can use it to set mnemonics and accelorators. Here is a custom Action class that takes
An action command String
an icon
a description for tooltips
a mnemonic
and a key accelorator.
private class MyAction extends AbstractAction {
String name;
public MyAction(String name, Icon icon) {
super(name, icon);
this.name = name;
}
public MyAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelorator) {
super(name, icon);
putValue(Action.SHORT_DESCRIPTION, desc);
putValue(Action.MNEMONIC_KEY, mnemonic);
putValue(Action.ACCELERATOR_KEY, accelorator);
this.name = name;
}
#Override
public void actionPerformed(ActionEvent e) {
switch (name) {
case "Open":
System.out.println("Open");
break;
case "New":
System.out.println("New");
break;
case "Save":
System.out.println("Save");
break;
}
}
}
Here's an instantiation of this Action
Action newAction = new MyAction("New", newIcon,
"Creates a new file",
new Integer(KeyEvent.VK_N),
KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
And here's the new result. You will see the actionCommand in the menu, along with the key mnemonics and accelerators, tooltips, and you will see the jtoolbar buttons share the same traits. You will also see in the final code, that never once once a component created. All you do is add the Action to the JToolBar and the JMenu and let them work their magic.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionInterfaceDemo extends JFrame {
public ActionInterfaceDemo() {
ImageIcon openIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/open.gif"));
ImageIcon saveIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/save.gif"));
ImageIcon newIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/new.gif"));
Action openAction = new MyAction("Open", openIcon,
"Opens a file",
new Integer(KeyEvent.VK_O),
KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
Action saveAction = new MyAction("Save", saveIcon,
"Saves a file",
new Integer(KeyEvent.VK_S),
KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
Action newAction = new MyAction("New", newIcon,
"Creates a new file",
new Integer(KeyEvent.VK_N),
KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.add(newAction);
fileMenu.add(openAction);
fileMenu.add(saveAction);
JToolBar toolBar = new JToolBar("Alignment");
toolBar.setBorder(BorderFactory.createLineBorder(Color.BLUE));
toolBar.add(Box.createHorizontalGlue());
toolBar.add(newAction);
toolBar.add(openAction);
toolBar.add(saveAction);
add(toolBar, BorderLayout.PAGE_START);
add(new JScrollPane(new TextArea(10, 40)), BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Action Interface Demo");
pack();
setLocationByPlatform(true);
setVisible(true);
}
private class MyAction extends AbstractAction {
String name;
public MyAction(String name, Icon icon) {
super(name, icon);
this.name = name;
}
public MyAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelorator) {
super(name, icon);
putValue(Action.SHORT_DESCRIPTION, desc);
putValue(Action.MNEMONIC_KEY, mnemonic);
putValue(Action.ACCELERATOR_KEY, accelorator);
this.name = name;
}
#Override
public void actionPerformed(ActionEvent e) {
switch (name) {
case "Open":
System.out.println("Open");
break;
case "New":
System.out.println("New");
break;
case "Save":
System.out.println("Save");
break;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ActionInterfaceDemo();
}
});
}
}
UPDATE
The better explain the relationship of Action and Command Patterns
As noted in Command Pattern
The command pattern is a commonly used pattern which encapsulates a method call or action-like code into a single class. The advantages of being able to package a method (or methods) into a class become evident when you have multiple invokers for a single action (for example a button and a menu item may perform the same action).
In Swing and Borland Delphi programming, an Action is a command object. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only the Action object.
So basically Swing uses the concept of the command pattern through the use of Actions
As for OP's question
"Command Pattern seems to be ok but I can't get who's the receiver in all that."
As for the receiver, the wiki uses a text editor as an example and defines the receiver as such
Receiver, Target Object: the object that is about to be copied, pasted, moved, etc. The receiver object owns the method that is called by the command's execute method. The receiver is typically also the target object. For example, if the receiver object is a cursor and the method is called moveUp, then one would expect that the cursor is the target of the moveUp action. On the other hand, if the code is defined by the command object itself, the target object will be a different object entirely.
The main more components of a Command Pattern are stated as follows
Four terms always associated with the command pattern are command, receiver, invoker and client.
Client, Source, Invoker: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user.
So to put it all together:
The MenuItem (client) invokes it
Action (command object) which calls it actionPerformed which in turn
Invokes an method on the receiver.
The wiki article is good read with a Java example
When two or more components are mean to do exactly the same thingy, one should look at Action, which reduces the duplicate code.
Small example for further help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionExample {
private JFrame frame;
private JButton button;
private JMenuItem exitItem;
private Action commonActions;
private class CommonActions extends AbstractAction {
public CommonActions(String title, String desc) {
super(title);
putValue(SHORT_DESCRIPTION, desc);
}
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame,
"Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
}
};
private void displayGUI() {
frame = new JFrame("Action Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
commonActions = new CommonActions("Exit", "To Exit the Application");
JPanel contentPane = new JPanel();
button = new JButton();
button.setAction(commonActions);
contentPane.add(button);
frame.setJMenuBar(getMenuBar());
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar getMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
exitItem = new JMenuItem(commonActions);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
return menuBar;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new ActionExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
ADDED an example with SINGLETON PATTERN (though I am not sure of this approach(about how good this approach is))
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionExample {
private JFrame frame;
private JButton button;
private JMenuItem exitItem;
private void displayGUI() {
frame = new JFrame("Action Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CommonActions.setValues(frame);
JPanel contentPane = new JPanel();
button = new JButton();
button.setAction(CommonActions.getInstance());
contentPane.add(button);
frame.setJMenuBar(getMenuBar());
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar getMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
exitItem = new JMenuItem(CommonActions.getInstance());
fileMenu.add(exitItem);
menuBar.add(fileMenu);
return menuBar;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new ActionExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class CommonActions extends AbstractAction {
private static CommonActions commonActions = null;
private static JFrame frame = null;
static {
try {
commonActions = new CommonActions("Exit", "To Exit the Application");
} catch (Exception e) {
throw new RuntimeException("BINGO, an error");
}
}
private CommonActions(String title, String desc) {
super(title);
putValue(SHORT_DESCRIPTION, desc);
}
public static CommonActions getInstance() {
return commonActions;
}
public static void setValues(JFrame f) {
frame = f;
}
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame,
"Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
}
}
Im writing the content of text field to a text file:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import java.io.FileNotFoundException;
import java.util.Formatter;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Payroll extends JFrame implements ActionListener{
private JButton btn1;
private JButton btn2;
private JButton btnadd;
// initialize the lbl with caption name is employee information.
JLabel lbl = new JLabel("Nilai University Payroll System");
public Payroll(){
super("Nilai University Payroll System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel lblid,lblname,lblrank,lblclass,lbltype,lblpay;
JTextField txtid,txtname,txtrank,txtclass,txtEmployeeClass,txtEmployeeType;
JRadioButton rb1,rb2,rb3,rb4,rb5;
lbl.setBounds(200,50,500,100);
lbl.setHorizontalAlignment(lbl.CENTER );
lblid = new JLabel("Employee ID: ");
lblname = new JLabel("Employee Name: ");
lblrank = new JLabel("Employee Rank: ");
lblclass = new JLabel("Employee Class: ");
lbltype = new JLabel("Class Type: ");
lblpay = new JLabel("Employee Pay Rate: ");
// initialize all the label which are declared in the example above with its caption name
lblid.setBounds(300,140,100,20);
lblname.setBounds(300,180,100,20);
lblrank.setBounds(300,220,100,20);
lblclass.setBounds(300,260,100,20);
lbltype.setBounds(300,300,100,20);
lblpay.setBounds(300,340,100,20);
// add all the label on the frame
add(lblid);
add(lblname);
add(lblrank);
add(lblclass);
add(lbltype);
add(lblpay);
// initialize the text field with size
txtid=new JTextField(15);
txtname=new JTextField(15);
txtclass=new JTextField(15);
txtEmployeeClass=new JTextField(15);
txtEmployeeType=new JTextField(15);
// set a particular position on a screen with set bounds constructor
txtid.setBounds(400,140,100,20);
txtname.setBounds(400,180,100,20);
txtclass.setBounds(400,220,100,20);
txtEmployeeClass.setBounds(400,260,100,20);
txtEmployeeType.setBounds(400,300,100,20);
// add text field on a Frame
add(txtid);
add(txtname);
add(txtclass);
add(txtEmployeeClass);
add(txtEmployeeClass);
// initialize radio button with its caption
rb1 = new JRadioButton("1. Excellent");
rb2 = new JRadioButton("2. Good");
rb3 = new JRadioButton("3. Average");
rb4 = new JRadioButton("4. Fair");
rb5 = new JRadioButton("5. Poor");
// set a particular position on a Frame
rb1.setBounds(400,220,100,20);
rb2.setBounds(500,220,100,20);
rb3.setBounds(600,220,100,20);
rb4.setBounds(700,220,100,20);
rb5.setBounds(800,220,100,20);
// add button on a frame
add(rb1);
add(rb2);
add(rb3);
add(rb4);
add(rb5);
btnadd = new JButton("Add Employee");
btnadd.setToolTipText("Click this button to add employee details to a text file.");
btnadd.setBounds(400,320,150,20);
add(btnadd);
btnadd.addActionListener(this);
btnadd.setActionCommand("Add");
}
private BufferedWriter output;
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("Add"))
{
output.write("ID: "+txtid.getNume()+"\n");
}
}
public void WriteFile(){
try {
output = new BufferedWriter(new FileWriter("E:/EC3307/eclipse/Payroll.txt",true));
output.newLine();
output.close();
}
catch(IOException e)
{
System.out.println("There was a problem:" + e);
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run()
{
Payroll f1=new Payroll();
// set frame size
f1.setSize(1000,600);
// set frame visible true
f1.setVisible(true);
}
});
}
}
The problem is that txtid is a local variable of your Payroll constructor and not a member of the Payroll class. Simply declare txtid as variable of your Payroll class and this will allow to access it in your actionPerformed method.
Declare txtid outside of Payroll() constructor
I want to add components inside JDialog Box in Swing without extending inner class which extends JDialog like the code below,SimpleAboutDialog is an inner class inside my class, based on some condition I m instantiating its class to give a JDialog box with JLabels but I dont want to use object of it
package com.project.swings.layout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class PopUpMenu{
JPopupMenu Pmenu;
JMenuItem menuItem;
public static void main(String[] args) {
PopUpMenu p = new PopUpMenu();
}
public PopUpMenu(){
JFrame frame = new JFrame("Creating a Popup Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pmenu = new JPopupMenu();
menuItem = new JMenuItem("Cut");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Cut")){
SimpleAboutDialog sad=new SimpleAboutDialog(new JFrame());
sad.setSize(200, 200);
sad.setVisible(true);
}
}
});
Pmenu.add(menuItem);
menuItem = new JMenuItem("Copy");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Copy")){
JOptionPane.showInputDialog(null, "Please choose a name", "Example 1",
JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Amanda",
"Colin", "Don", "Fred", "Gordon", "Janet", "Jay",
"Joe", "Judie", "Kerstin", "Lotus", "Maciek", "Mark",
"Mike", "Mulhern", "Oliver", "Peter", "Quaxo", "Rita",
"Sandro", "Tim", "Will" }, "Joe");
}
}
});
Pmenu.add(menuItem);
menuItem = new JMenuItem("Paste");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Paste")){
String string[]={"Sam","Samarth","Mourya"};
JComboBox combo=new JComboBox();
JOptionPane.showInputDialog(new JFrame(),"Combo","Dialog",1,null,string ,"Mourya");
}
}
}
);
Pmenu.add(menuItem);
menuItem = new JMenuItem("Delete");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Undo");
Pmenu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Cut")){
SimpleAboutDialog sad=new SimpleAboutDialog(new JFrame());
sad.setSize(200, 200);
sad.setVisible(true);
}
}
});
frame.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent Me){
if(Me.isPopupTrigger()){
Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
}
}
});
frame.setSize(400,400);
frame.setVisible(true);
}
private class SimpleAboutDialog extends JDialog {
public SimpleAboutDialog(JFrame parent) {
super(parent, "About Dialog", true);
Box b = Box.createVerticalBox();
b.add(Box.createGlue());
b.add(new JLabel("J"));
b.add(new JButton("g"));
b.add(Box.createGlue());
getContentPane().add(b, "Center");
JPanel p2 = new JPanel();
JButton ok = new JButton("Ok");
p2.add(ok);
getContentPane().add(p2, "South");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
setSize(250, 150);
}
}
}
Everything you do could be done outside the SimpleAboutDialog class.
Just like you call f.show(); you would add a reference to the JDialog like so f.setSize(250, 150); or f.getContentPane().add(p2, "South");
JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)
is another way of adding Jcombo Box and Button.