I've read several topics showing how to create a KeyBinding, however, none of them fully worked for me. My JFrame has a JMenuBar and for the items of the menu NetBeans is correctly generating code such as:
mniExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
mniExit.setText(bundle.getString("Menu.File.Exit")); // NOI18N
mniExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mniExitActionPerformed(evt);
}
});
mnuFile.add(mniExit);
However, only this binding is not visible when the menu is hidden. I've tried something like:
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mniExit.getAccelerator(), "exit");
getRootPane().getActionMap().put("exit", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
mniExit.doClick();
}
});
But it simply does not work. What am I doing wrong?
Thanks in advance!
You state that
However, only this binding is not visible when the menu is hidden. I've tried something like:
I'm guessing here, but I'm not sure that a button or menu can be clicked if its not visible. To simplify, I would create an ExitAction class, a class that extends from AbstractAction, that is assigned as an Action to any JMenuItems, JButtons, or Key Bindings that need it. If they all share the same ExitAction object, then the Action (and the corresponding menu items and buttons) can be disabled all at once if need be.
If this does not help, again create and post a minimal example program where you create the smallest program that runs, compiles, requires no outside dependencies (images, database) and that demonstrates your problem for us.
Related
I have this strange problem when I add buttons to the toolbar. I added action listener to one button added before the frame is shown and it works fine:
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
toolbar.add(new JButton("new button"));
}
});
I also added a piece of code that is supposed to add a new button after some plugins are loaded, and for some reason that piece of code does not work.
#Override
public void handle() {
System.out.println("Button added");
MainFrame frame = Application.getInstance().getMainFrame();
frame.getToolbar().add(new JButton("Plugin button"));
frame.getToolbar().revalidate();
frame.getToolbar().repaint();
System.out.println(frame.getToolbar().getComponents().length); // It is definitely being added, just not shown
}
The button is definitely being added, just not shown.
I would really appreciate any help since this thing is blocking me from progressing any further.
I found out what the problem was. The problem was that I instantiated MainFrame twice, first by calling Application constructor in main and then when calling Application.getInstance(), so all components added to the MainFrame were removed.
I have the following situatation:
I have a Java Swing application.
In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:
JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);
Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
System.exit(0);
}
});
In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)
What can I do to do this thing?
Tnx
Andrea
You don't really need to close/open window junky approach at all. Just use Card Layout:
set Frame's content pane's layout to card layout.
getContentPane().setLayout(new CardLayout());
Put your different Form's content code inside different panel and
add them to the content pane with their corresponding name, for example:
getContetnPane().add(logInFormPanel, "logIn Form");
Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name). For example:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "logIn Form");
}
});
Check out a CardLayout demo from my another answer.
1> I have a JButton in Jframe.
2> The click of JButton opens new instance of another JFrame.
The problem is when a Key is pressed very fast on the above Jbutton .Two instances of the same JFrame opens up.
I have to open these frames. I knows there are other options also not using the Jframes as I read.
I managed to solve this problem for Doulbl click of Mouce by setMultiClickThreshHold('time in miliseconds'). But it worked only for mouse.
Tried some other stuffs which I got in google, But none worked.
Is there any other way to solve this issue?
For full control of how often/quickly again an Action is triggered, implement it to disable itself in its actionPerformed. Something like:
singlePerform = new AbstractAction("DoSomthing") {
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doSomething();
}
};
JButton button = new JButton(singlePerform);
When it's safe for doSomething to be triggered again, simply re-enable the Action:
singlePerform.setEnabled(true);
I am pretty sure this is very easy and that I am only missing one line or two but I just cannot make this work despite searching for solutions over the internet. I am fairly new to java and my problem is in a desktop application.
I have a pretty simple desktop application with one text area, one menu bar with one menu and 3 menu item. I want to edit the text of the text area when I click on the Statistic menu item in a JFrame.
Here is the part of the code where I create the menu bar, menu and menu items (as well as their events):
//menu
mnuRevision.setText("Revision");
mnuitmStats.setText("Statistique");
mnuitmStats.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mnumnuitmStatsMouseClicked(evt);
}
});
mnuRevision.add(mnuitmStats);
mnuitmOrthographe.setText("Grammaire et orthographe");
mnuitmOrthographe.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mnuitmOrthographeMouseClicked(evt);
}
});
mnuRevision.add(mnuitmOrthographe);
mnuitmAnalyse.setActionCommand("Analyse");
mnuitmAnalyse.setText("Analyse");
mnuRevision.add(mnuitmAnalyse);
jMenuBar1.add(mnuRevision);
setJMenuBar(jMenuBar1);
Here is the Mousclicked function:
private void mnumnuitmStatsMouseClicked(java.awt.event.MouseEvent evt){
this.txtTexte.setText("asdf");
this.repaint();
}
What I want to do is when I click on mnuitemStats, the txtTexte will get the text "asdf" written in it. Somehow, this is not working. It looks like the program is not even getting into the function. I looked on some tutorials and they pretty much have the same code as me except for the objects names since most tutorials uses JButton instead of JMenuItem.
I can provide my whole code if it is needed, but I thought the rest would be irrelevant since it does not touch the menu bar or the textarea. I am using Eclipse Java EE IDE.
I usually write something like
mnuitemStats.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{// your logic here;
}});
Assuming mnuitmStats is a JMenuItem, it should be. A little more code would be helpful but given that assumption you should use ActionListener not a MouseListener for this.
Something like:
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//do something
}
}
and
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
fileMenu.add(newMenuItem);
Can we use a JInternalFame with a button in the main frame? The frame contains a JDesktopPane, of course. The button should open up the JInternalFrame How?
I don't know a way to put a JButton directly on a JDesktopPane, but you can use menu items to create and select a JInternalFrame. In this example, each menu item uses an Action defined in the JInternalFrame to select the corresponding frame.
class MyFrame extends JInternalFrame {
private Action action;
MyFrame(JDesktopPane desktop, String name, int offset) {
…
action = new AbstractAction(name) {
#Override
public void actionPerformed(ActionEvent ae) {
try {
MyFrame.this.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
};
}
public Action getAction() { return action; }
}
Addendum: as #camickr suggests, it is technically possible to put a JButton directly on a JDesktopPane, but it might prove difficult to use in practice.
I don't really understand the question so I will just make some observations.
a) a JInternalFrme is like a frame in that you can add any component to it that you want
b) A JButton works the same whether it is added to an internal frame or a frame
I suggest you start by reading the Swing tutorial for working examples. You might start with the sections on "How to Use Internal Frames" and "How to Use Buttons".
If you still have problems then post your SSCCE that shows what you have tried.