I have some difficulties with setting a shortcut to my dynamically added buttons. Buttons are on dynamically added panel. I want to add keyboard shortcuts to these buttons, like so:
ctrl+1 -> button1
ctrl+2 -> button2
etc.
Buttons are added by an algorithm, so I don't know how many of them there will be(min 0, max 5). I would like to call these buttons with Global (working in whole panel) keyboard shortcuts. I am wondering how to achieve this, when the text on my button is made dynamically like this:
getNewAmountQueryButton(label)
which makes this:
private JButton getNewAmountQueryButton(final Decimal label) {
JButton temp = new JButton(label.toString());
...
I tried adding keylistener but it works only when I have a focus on a button:
button.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(final KeyEvent e) {
#Override
public void run() {
if ((e.getKeyCode() == getKeyCode()) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
model.setAmount(model.getAmount().add(label));
field().selectAll();
refreshDisplay();
}
}
});
});
Do you know how can I do this? I wanted to add listener to top-level panel, but as I use model.setAmount(model.getAmount().add(label));to refresh some textField it is impossible due to lack of this label parameter.
Any idea?
Thanks in advance!
It is recommend that you use Key Bindings.
Bascially, they don't suffer from the same focus issues as KeyListeners
Use Action to encapsulate each button's behvior. You can specify the desired MNEMONIC_KEY, as shown here, and you can add a binding to the ACCELERATOR_KEY, as shown here.
Related
I just began using JMenu. To ease into it, I decided to use Netbeans form design tool, which has worked great for all components in this app.
Clicking a top-level menu item works great.
For one menu item, I made a submenu with 3 items, each with a mouse click listener.
Here's relevant code for one of the 3 submenus:
private JMenuItem mnuEditDicAddAllScratch;
mnuEditDicAddAllScratch = new JMenuItem();
private void mnuEditDicAddAllScratchMouseClicked(MouseEvent evt) {
new WordsToAdd(); // never happened
}
mnuEditDicAddAllScratch.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
mnuEditDicAddAllScratchMouseClicked(evt);
}
});
mnuEdit.add(mnuEditDicAddAllScratch);
It didn't work. Clicks ignored.
So I tried an Action listener:
private void mnuEditDicAddAllScratchActionPerformed(ActionEvent evt) {
new WordsToAdd(); // WORKED
}
mnuEditDicAddAllScratch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnuEditDicAddAllScratchActionPerformed(evt);
}
});
And it worked.
So a question is, "Why didn't mouse click listener listen?"
Also, "If I should stay away from mouse click events, why or under what circumstances?"
(And, pre-emptive strike: I should stay away from Netbeans form designer.)
You should use the best tool for the job at hand. This means that for JMenuItems and for JButtons, you should use ActionListeners, not MouseListeners (exceptions notwithstanding). For instance if you disable a button, you want the button to not work, right? This works with ActionListeners but not with MouseListeners.
For the best information on this type of stuff, go to the source: Swing Tutorials.
mnuEditDicAddAllScratch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
mnuEditDicAddAllScratchActionPerformed();
}
});
Ok hello, What I want is when my button is pressed I want read the text from a URL and display it in the GUI,
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//???????????????????
}
I am very confused on this. I saw it in a Open Source project and I can't get it to work :/ All I really want is it to open up to a raw github file (in the GUI) to display the contents of the github.
This is the github link
I want the text to display as if it where actually in the application.
Thanks for any help
What I would do, is go ahead and make a new JFrame, named something different than your previous one, but set it's visibility to false. Within that new JFrame, "ex", add a layout and then add you github info into the layout.
ex:
JFrame ex = new JFrame();
ex.setSize(50,50);
ex.setVisible(false);
ex.setResizable(falsE);
ex.setDefaultCloseOperation(JFrame.Dispose);
//Use "dispose" to exit the window but not terminate your program
and then, when you have a JButton clicked, or whatever you're using, use an actionevent.
ex:
jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("JButton1 Button pressed");
ex.setVisible(true);
}
});
I am working with jsliders, I have made a jslider, and couple of text boxes, what I want to do is when lets say the user slides the slider to 3 it makes visible text boxes. I have a code which is working at the moment and also does not interpret making visible text boxes but enabling or disabling them.
do {
textField.setEnabled(true);
}
while (slider.setValue(3));
I want to hide text boxes and when the user slides the slider to 3 it enables text boxes. Thanks,
You need to add a change listener like
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
and then change the visibility of the text field on that function:
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
if(jSlider1.getValue()<50)
jLabel1.setVisible(false);
else
jLabel1.setVisible(true);
}
This is in the constructor of a JPanel but it does not print anything when I press "h". If more code is needed, I can provide it. Thank you!
String hide = "hide";
this.getInputMap().put(KeyStroke.getKeyStroke('h'), hide);
this.getActionMap().put(hide, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("HIDDEN");
if (isHidden){
slide.setVisible(true);
}else{
slide.setVisible(false);
}
}
});
this.getInputMap()....
You are trying to add the bindings to the default InputMap, which is the InputMap when the component has focus. By default a JPanel does not have focus.
You should try using one of the other InputMaps by using the getInputMap(int) method. Or you will need to make the panel focusable and give it focus.
Read the Swing tutorial on How to Use Key Bindings for more information on the proper variables to use to specify the desired InputMap.
I'm trying to use an editable JComboBox such that upon a user typing into the editor, possible results are displayed in the list part of the combo box.
Unfortunately, I've found that upon using either addItem(item) or getModel().addItem(item), the input typed by the user is overwritten by the first value I added. I've considered storing the editor value, adding items, and then using setSelectedItem() to fix this, but I wan't to preserve the state of any selected text/ caret position, and believe this should be something more trivial, but can't for the life of me figure it out.
JComboBox box = new JComboBox();
box.setModel(new MutableComboBoxModel());
box.setEditable(true);
box.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
// Actual results are retrieved from server via HTTP
box.addItem("Demo");
// Here, the editor window the user was typing in is replaced with the value "Demo".. how to fix this?
}
});
use AutoComplete JComboBox / JTextField,
for listening in JTextComponent is there DocumentListener,
never use KeyListener for Swing JComponents, this listener is designated for AWT Components, for Swing JComponents is there KeyBindings
You need to implement your own MutableComboBoxModel since DefaultComboBoxModel is responsible for the "add item then auto select it" behavior.