Appropriate Listener for SWT Text - java

I have a JFace dialog which contains SWT Text and a button . Initially when the dialog is opened the button should be disabled, and when I click on the Text and as long as the caret position of the Text is visible button should be enabled.
These are the listeners i am using :
text.addMouseListener(new MouseListener()
{
#Override
public void mouseDoubleClick(MouseEvent arg0)
{
}
#Override
public void mouseDown(MouseEvent arg0)
{
}
#Override
public void mouseUp(MouseEvent arg0)
{
testButton.setEnabled(true);
}
});
text.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent arg0)
{
testButton.setEnabled(false);
}
#Override
public void focusGained(FocusEvent arg0)
{
}
});
Am I using the appropriate listeners? Please suggest

If I understood you correctly, this should be what you want:
button.setEnabled(false);
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
button.setEnabled(false);
}
});
text.addListener(SWT.FocusIn, new Listener()
{
#Override
public void handleEvent(Event e)
{
button.setEnabled(true);
}
});
Initially, the Button is disabled. It will be enabled once the Text gaines focus. The Button will be disabled again after it was pressed.

Related

Java system trayicon mouselistener

Here is the code. I want to ask why is that mouseClicked for a trayicon works perfectly but mouseEntered won't work at all?
mouseListener = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
showStage(stage);
EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
}
}
#Override
public void mouseEntered(MouseEvent e) {
// why this cant be triggerd
trayIcon.displayMessage(
"警告", "这是一个警告提示!", TrayIcon.MessageType.WARNING);
}
};
trayIcon.addMouseListener(mouseListener);
After
trayIcon.addMouseListener(mouseListener);
add this line:
trayIcon.addMouseMotionListener(mouseListener);

How to assign a method to a button when pressed or released with ActionListener in Java?

I am new to coding GUIs in Java and I am trying to just print a message on the terminal when a button is pressed and another one when it is released.
This is what I have for a regular button pressing.
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Pressed");
}
});
I did this with the help of IntelliJ IDEA. I want to make the button send a message when pressed and a different thing when released.
You can just add a simple MouseAdapter, like this:
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
}
};
leftButton.addMouseListener(ma);
frame.add(button);
This will detect when it is the mouse is pressed on the button or released on the button.
If you want, you can also add a mouseClicked() method, mouseExited(), mouseEntered(), mouseMoved(), and (many) more methods in your MouseAdapter. Check out this JavaDoc
Use custom class and use it
leftButton.getModel().addChangeListener(new BtnCusttomListener());
private class BtnCusttomListener implements ChangeListener {
private boolean pressed = false; // holds the last pressed state of the button
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel Buttonmodel = (ButtonModel) e.getSource();
// if the current state differs from the previous state
if (model.isPressed() != pressed) {
String text = "Button pressed: " + model.isPressed() + "\n";
textArea.append(text);
pressed = model.isPressed();
}
}
}
You can use a MouseListener instead:
leftButton.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
// The mouse button was pressed and released
}
#Override
public void mousePressed(MouseEvent e) {
// The mouse button was pressed
}
#Override
public void mouseReleased(MouseEvent e) {
// The mouse button was released
}
#Override
public void mouseEntered(MouseEvent e) {
// The cursor entered the bounds of the button (i.e. hovering)
}
#Override
public void mouseExited(MouseEvent e) {
// The cursor exited the bounds of the button
}
});

Why is this event doing as much as the overall clicks?

I have two events that support the button and table by click mouse
if i click te button should remove the record from the list and table. It
happens but program display this exception ArrayIndexOutOfBoundsException:
-1
r.getjTable3().addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e)
{
System.out.println(e.getClickCount());
r.getjButton2().addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e)
{
String nazwa = r.getjTable3().getValueAt(r.getjTable3().getSelectedRow(), 3).toString();
System.out.println(r.getjTable3().getSelectedRow());
((DefaultTableModel)r.getjTable3().getModel()).removeRow(r.getjTable3().getSelectedRow());
for (Wydarzenie lista1 : lista)
{
if(nazwa==lista1.name)
{
lista.remove(lista1);
}
}
}
});
}
});

Java button reapeating itself

The button listener is stacking: the first time I click the button, it writes in the combo-box once; the second time it writes twice, and so on.
Do I have to clean the buffer or something like that?
novoP.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
painel.removeAll();
painel2.removeAll();
if(autenticator == false) {
add(painel, BorderLayout.CENTER);
painel.add(nega);
} else {
add(painel, BorderLayout.CENTER);
painel.add(iproduto);
painel.add(buton);
buton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jComboBox1.addItem(iproduto.getText().toString());
}
});
}
painel.repaint();
painel.revalidate();
painel2.repaint();
painel2.revalidate();
}
});

Handling editing events in a JTextField

I have a login form where a user can enter his credentials to login. I have a JLabel that serves to display the text telling the user that the user name cannot be empty. This label is display after a user click the login button when the text field is empty.
I want that the moment the user starts typing in the text field the label with the information should disappear.How do I achieve this behavior?
Here is the code:
public class JTextFiledDemo {
private JFrame frame;
JTextFiledDemo() {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(300, 300);
frame.setLayout(new GridLayout(4, 1));
frame.setLocationRelativeTo(null);
iniGui();
}
private void iniGui() {
JLabel error = new JLabel(
"<html><font color='red'> Username cannot be empty!<></html>");
error.setVisible(false);
JButton login = new JButton("login");
JTextField userName = new JTextField(10);
frame.add(userName);
frame.add(error);
frame.add(login);
frame.pack();
login.addActionListener((ActionEvent) -> {
if (userName.getText().equals("")) {
error.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JTextFiledDemo tf = new JTextFiledDemo();
}
});
}
}
You have to create DocumentListener:
DocumentListener dl = new DocumentListener()
{
#Override
public void insertUpdate(DocumentEvent de)
{
error.setVisible(false);
}
#Override
public void removeUpdate(DocumentEvent de)
{
//
}
#Override
public void changedUpdate(DocumentEvent de)
{
error.setVisible(false);
}
};
then for your text fields:
login.getDocument().addDocumentListener(dl);
For that purposes you need to use DocumentListener on your JTextField, here is tutorial.
As example:
userName.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent de){
event(de);
}
#Override
public void removeUpdate(DocumentEvent de) {
event(de);
}
#Override
public void changedUpdate(DocumentEvent de){
event(de);
}
private void event(DocumentEvent de){
error.setVisible(de.getDocument().getLength() == 0);
// as mentioned by nIcE cOw better to use Document from parameter
frame.revalidate();
frame.repaint();
}
});
error must be final(for java lower than 8 version).
Also at start your field is empty, so may be need to use setVisible(true) on error label.
You can add a keyListener in the input filed
userName.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent arg0) {
}
#Override
public void keyPressed(KeyEvent arg0) {
error.setVisible(false);
}
});

Categories