I have multiple JTextFields and I want to see which one is selected within the program. At the moment it does not seem as though clicking on the JTextField calls an ActionEvent (is that how you phrase it?).
public void actionPerformed(ActionEvent ae){
if(e.getSource().equals(JTextField.class)){
current = (JTextField) e.getSource();
System.out.println(current);
}
}
A ActionListener will generally be triggered when the user "actions" the field, for most platforms/look and feels, this is triggered by the user pressing the Enter key.
I think what you're after is a FocusListener
Have a look at How to Write a Focus Listener for more details
If you just want to find out which component is currently focused, you could use the KeyboardFocusManager
Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
Use can use addMouseListener too,
jtextField.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
...
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
Did you add the actionListener to the JTextFiled?
JTextField tf= new JTextfield();
tf.addActionListener(//class name goes there, if the actionListener is in a different class otherwise just say "this");
Related
I have a submit button (and an action listener) which checks if the number inserted into a Futoshiki puzzle is legal (checking a 2D array for duplications etc)
In another method I have the actual grid with an action listener that gets the numbers and inserts them into the 2D array.
JButton acction listener
JButton isRight = new JButton("Check My Answer");
isRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (!(puzzle.isLegal())) {
JOptionPane.showMessageDialog(FutoshikiFrame.this,
puzzle.getProblems(),
"You made a mistake!",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(FutoshikiFrame.this,
"YOU WIN!",
"YES THATS FINE",
JOptionPane.INFORMATION_MESSAGE);
}
puzzle.printProblems.clear();
}
});
Grid action listener
public void keyReleased(KeyEvent e) {
String getInsertedValue = Emptysquare.getText();
int getInsertedIntValue = Integer.parseInt(getInsertedValue);
setSquareValue(r, c, getInsertedIntValue);
System.out.print(getSquareValue(r, c));
}
Is there a way I can access the keyReleased action listener from the JButton so it basically "submits" the contents of the grid and then checks if its legal?
Sure, just either keep a reference to that action listener and call the keyReleased method with a null value, or refactor out the content of keyReleased to an own method and call this method from both listeners.
Although 2 other methods were given, here is an additional way you could go about it, but I'm not sure which would be best to use.
public class YourClass {
JButton isRight;
public YourClass() {
this.isRight = new JButton();
this.isRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
YourClass.this.isRight.getKeyListeners()[0].keyReleased(null);
//Other action related code
}
});
isRight.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
//Key related code
}
//Other required key listener methods
});
}
}
addItem = new JButton("Add");
gc.gridwidth = GridBagConstraints.RELATIVE;
gc.weightx = 1.0;
panel.add(addItem, gc);
Am I able to make it into something like that:
addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
Instead of this I would want that button, but I have no idea what does that addItem do, since it does not let me to add a name there.
Is there a way how I can do this without modifying the 4 rows of code given at the beginning of the question?
what you can do is to add ActionListener to the button click or if you want you can add MouseListener , actually it depends on what you want to do
addItem.addActionListener(new ActtionListener() {...});
Code using an inner class instead of EventHandler.
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something
}
});
you can get more information on this in java official website.
Refer: http://docs.oracle.com/javase/7/docs/api/java/beans/EventHandler.html
if you add just the mouse listener you will not get the 'press' event if using keyboard. So, if your requirement is strictly bound to mouse press then use below snippet :)
addItem.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
// do something
}
#Override
public void mousePressed(MouseEvent e) {
// do something
}
#Override
public void mouseExited(MouseEvent e) {
// do something
}
#Override
public void mouseEntered(MouseEvent e) {
// do something
}
#Override
public void mouseClicked(MouseEvent e) {
// do something
}
});
hello is there a way mouse even that can Hold the mouse and release cause I can't find it on google.
so for example this image..
When the jTextBox is **** when he click the button, he see the words oops...
then after he release the click of mouse the jTextBox will back to **** again
I know this code already but the mouseevent is I only don't know
Yes. You will want to implement the MouseListener interface with a new class and add this new Listener against your button with the following;
button.addMouseListener(new YourMouseListener());
An example custom MouseListener might look like this.
class YourMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
// Insert code to show password
}
#Override
public void mouseReleased(MouseEvent e) {
// Insert code to hide password again
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I hope this helps.
You'll need a Robot object. This can do things as follows:
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
The Mousebutton is pressed until you do this:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
This should do what you want.
In my program, the user writes something in a JTextField then clicks a 'generate' button, which triggers the characters in the JTextField to be drawn to a JPanel.
I would then like to clear all the text in the JTextField when the user clicks the JTextField again. I tried to achieve this by adding a FocusListener and an ActionListener to the JTextField, however my attempts did not work. Moreover, my implementation of the FocusListener gave an Unreachable Statement compiler error.
Is this possible to do in Java and if so how can I do this?
The code below is my ActionListener implementation.
dfaText = new JTextField(6);
dfaText.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
generateLabel.setText("NOOOOO!!!");
dfaText.setText("");
isDfaDrawn = false;
canDraw = false;
repaint();
}
});
Add a mouse listener:
field.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
field.setText("");
}
});
Bear in mind this could get frustrating if the user legitimately clicks elsewhere and returns to the field. You may wish to maintain some state, e.g. only clear the field if the button has been clicked in the interim.
You can do this:
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField.setText("");
}
});
Just an addition to others' code.
public void textfieldMouseClicked(java.awt.event.MouseEvent evt) {
uname_tf.setText(null);
} //This will set the JTextfield blank on mouse click//
public void textfieldFocusLost(java.awt.event.FocusEvent evt) {
uname_tf.setText("Username");
} //This will bring back the default text when not in focus//
Hope it helps, Cheers!!!
If you want it just one Click on it to delete the text you can do like this :
textField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
textFieldMousePressed(evt);
}
});
private void textFieldMousePressed(java.awt.event.MouseEvent evt) {
textField.setText("");
}
Add 1 line to you buttonMethod e.g:
txtfield.clear();
or set your txtfield to an empty string
for(int k=0;k< dtm.getRowCount();k++) //dtm is object of default table model
{
if(String.valueOf(dtm.getValueAt(k,1)).equalsIgnoreCase("Today") && check==0 )
{
cnt++;
JLabel jp=new JLabel();
panel.add(jp);
panel.setLayout(null);
if(cnt<=12)
{
jp.setBounds(j,500,100,100);
j=j+115;
jp.addMouseListener(this);
}
else
{
j=j-115;
jp.setBounds(j,400,100,100);
}
String b="<html><body text=#FDFA0B>"+String.valueOf(dtm.getValueAt(k,0))+"'s Birthday";
jp.setText(b);
jp.setFont(new java.awt.Font("comicbd",Font.ITALIC+Font.BOLD, 14));
}
}
It will not work mouselister only apply for last placed Label...
I want to apply mouse listener for all label how can I do that ..
please help me ....
Without SSCCE I can tell you that you're adding listener on 3 conditions:
String.valueOf(dtm.getValueAt(k,1)).equalsIgnoreCase("Today")
check == 0
and if(cnt<=12)
Other JLabels (that don't pass these conditions) haven't assigned your listener.
Make sure that you're clicking correct labels.
Or move jp.addMouseListener(this); just after JLabel creation (if you want to add listener to all your JLabels).
You certainly can add the same MouseListener to multiple components - here's an example in it's simplest form:
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {System.out.println("Released!");}
#Override
public void mousePressed(MouseEvent e) {System.out.println("Pressed!");}
#Override
public void mouseExited(MouseEvent e) {System.out.println("Exited!");}
#Override
public void mouseEntered(MouseEvent e) {System.out.println("Entered!");}
#Override
public void mouseClicked(MouseEvent e) {System.out.println("Clicked!");}
};
JLabel j1 = new JLabel("Label1");
j1.addMouseListener(ml);
JLabel j2 = new JLabel("Label2");
j2.addMouseListener(ml);
BUT according to your code, you're messing with a JTable - and JTable's act differently than you're thinking. The labels you're trying to edit are actually part of a TableCellEditor. The JTable uses the single TableCellEditor (read: single JLabel) to display every cell in the JTable. This is why you're only seeing the Listener applied to the last cell (because that's the only the last cell has a full component any more - the rest are just ghosts of where the component was applied before).
The good news is you can add a MouseListener to the JTable, and obtain information from there:
final JTable table = new JTable();
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseExited(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
};
table.addMouseListener(ml);
One Option is to add another inner class:
class MListener extends MouseAdapter{
public void mouseReleased(MouseEvent e) {}
//other mouse evetns
}
then rather then:
jp.addmousListener(this);
do:
jp.addMouseListener(new MListener());