I have a series of buttons in my code. I have added key listeners to individual buttons to listen to keys, so that when user presses RIGHT, LEFT,UP DOWN, I can transfer focus to the next button.
Note: I know that TAB can be used
now everything works really great! but when the focus is at a disabled button. I am not able to listen to it.
Any suggestions, as to how I can go around the problem?
Please pardon me before hand for amateur coding style!
addEntry.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{calPeriod.setFocusable(true);
calPeriod.grabFocus();
}if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
getTime.setFocusable(true);
getTime.grabFocus();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
genChart.setFocusable(true);
genChart.grabFocus();
}
}
});
Related
I'm trying to handle the event of pressing "enter button" when typing in a SWT Text field. Here is what I've done so far:
textField.addKeyListener(new KeyListener() {
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
if(arg0.keyCode == SWT.CR) {
//do something here....
}
}
});
This actually works when I hit Enter button, but not when I hit the enter button on the numpad.
Someone knows the constant variable for this button?
Thanks
Try using a traverse listener which is intended for handling things like Enter:
textField.addTraverseListener(new TraverseListener()
{
#Override
public void keyTraversed(final TraverseEvent event)
{
if (event.detail == SWT.TRAVERSE_RETURN)
{
...
}
}
});
I would try with:
if (arg0.keyCode==SWT.CR || arg0.keyCode==SWT.KEYPAD_CR)
Im looking for way where an action can be performed while using a tabbed pane whenever new tab is opened.
Something like formwindowopenned
Im looking for way where an action can be performed while using a tabbed pane whenever new tab is opened.
I assume you mean when a user clicks on an existing tab to switch to that tab. If so, then you can add a ChangeListener to the tabbedPane and listen for the stateChanged event.
If you are talking about adding a new tab to the tabbed pane, then you would just manage that in your application logic.
All actions on tabs (this will get you extra actions, not only mouse clicks, for example, if you change tab by code using selected index, this code will be executed):
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
}
};
myJTabbedPanel.addChangeListener(changeListener);
Only mouse clicks on tabs:
/**
* Detects clicks when user click tab inside tabbed pane
*/
private void addMouseEventToPanel(){
this.myJTabbedPanel.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("You clicked on tab number "+this.myJTabbedPanel.getSelectedIndex());
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
I need to be able to process a click on the tabs in a JTabbedPane. I'm not using this to change tabs, and this isn't going to trigger on tab change. What I'm attempting to do is close the tab when it is right clicked. However, I'm not sure how I can access the tab to add a click event on it. Most of the questions related to clicking on JTabbedPanes suggest using a ChangeListener, but that won't work, since the tabs aren't going to be changed on right click.
Is there any way for me to add a click event to a JTabbedPane's tab?
Is there any way for me to add a click event to a JTabbedPane's tab?
Read the section from the Swing tutorial on How to Use TabbedPanes for a working example on how to close a tab with a mouse click.
Keep a link to the tutorial handy for Swing basics.
Sorry for late answer, but I found this very usefull for me and for avoid extra clicks detected by stateChanged (with this you can detect all you want in "click tab"):
myJTabbedPane.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Panel 1 click");
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Finally, if you want to detect right click on tab you can see next tutorial (search getModifiers() in next page):
https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
Normally I don't program in JAVA but for this project I need to. I got everything working except of the key listener. I want the program to react whenever a Key is pressed (doesn't matter wich one).
public class fullscreen extends JPanel implements MouseListener, MouseMotionListener, KeyListener {
public fullscreen() {
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
}
and then the KeyListener methods:
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("Key Pressed!");
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
The MouseListener methods work without any problem. What did I do wrong with the KeyListener?
I am making a tool which writes the data I give to a file. Where I need help is I have four JTextFields in which I want to apply the same mechanism to each one. That mechanism will be written independently from the frame code, and would be about checking the data input into the field and other stuff.
I wrote that mechanism to each field and it worked, and now I am thinking about shortening the code by isolating that mechanism. I know that I must use action listeners and document listeners, but I can't figure out how to make it work with <code>e.getsource()</code> method.
Here is the mechanism that I need to isolate:
txtNa_1 = new JTextField();
//the mechanism will be about setting the field text in the beginning
//to "N/A", when focused sets to "", and when
//focus is lost it preserve the index.
txtNa_1.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent arg0) {
if (txtNa_1.getText().equals("N/A")){
txtNa_1.setText("");
}}
public void focusLost(FocusEvent arg0) {
if (txtNa_1.getText().equals("")){
txtNa_1.setText("N/A");
}}});
txtNa_1.getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
txtNa_23 = txtNa_2.getText();
}
public void insertUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
String txtNa_23 = txtNa_2.getText();
}
public void changedUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
txtNa_23 = txtNa_2.getText();
}});
Simply put, can somebody explain how to cast a <code>getsource</code> along with what I am trying to make? Help appreciated.