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();
}
});
Related
b.button1 = new JButton("Deal");
b.button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
b.button2 = new JButton("Hit");
panel.add(b.button2);
panel.validate();
b.button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
});
b.button3 = new JButton("Stay");
panel.add(b.button3);
panel.validate();
b.button3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
}
});
So I want The Buttons Hit and Stay to be added as soon as the Deal button has been pressed. I searched for a solution and found the panel.validate() method. I used it but now if I press the Deal button it only adds the Hit button.
You could add the buttons before and make them "hidden". If you press the button, you can "show" them to include them.
I have a JButton that will not allow me to perform the same action on any subsequent click on it after the first in the same Swing GUI instance.
JButton Run = new JButton("Run");
Run.setLocation(290, 70);
Run.setSize(120, 30);
buttonPanel.add(Run);
Run.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Run.isEnabled()) {
errorLabel.setText("");
Result result = JUnitCore.runClasses(Run.class);
errorMessageDisplay(result);
}
}
});
totalGUI.setOpaque(true);
return totalGUI;
}
So far I thought about and tried removing the JPanel and painting all of the buttons back on, and disabling/renabling buttons.
The errorMessageDisplay method is as follows:
public void errorMessageDisplay(Result resultPass) {
if (resultPass.getFailureCount() > 0) {
errorLabel.setForeground(Color.red);
errorLabel.setVisible(true);
errorLabel.setText(" Failed");
}
else {
errorLabel.setForeground(Color.green);
errorLabel.setText(" Passed");
errorLabel.setVisible(true);
}
}
At first glance, the JUnitCore.runClasses(Run.class); call is suspicous. Also, it would be good to know what does the errorMessageDisplay() do. I believe, the problem is with one of these methods.
You can verify this with the following experimental code. Just be careful not to push it into production.
JButton run = new JButton("Run");
run.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Run.isEnabled()) {
errorLabel.setText("");
System.out.println("Run action peformed.");
}
}
Update Since the errorMessageDisplay() looks okay, it's probably a Threading problem with JUniCore. Thus I'd try the following code:
final ExecutorService executor = Executors.newFixedThreadPool(5); // this runs stuff in background
JButton run = new JButton("Run");
// ..
run.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Run.isEnabled()) {
executor.execute(new Runnable() { // This is how we run stuff in background. You can use lambdas instead of Runnables.
public void run() {
final Result result = JUnitCore.runClasses(Run.class); // Run.class is different from the current JButton run.
SwingUtilities.invokeLater(new Runnable() { // Now we go back to the GUI thread
public void run() {
errorMessageDisplay(result);
}
});
}
});
}
});
I'm making a game where the players are taking turns and they would have a set of buttons each that they can click when it is their turn. Below is a sample code that follows the logic of what I am saying. But what happens is when I clicked the "btn1", it prints three 1s and I can still click the second button.
//this loop is in the main
for(int i=0; i<3;i++){
if(player==1){
player1();
}
else if (player==2){
player2();
}
}
public void player1(){
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n1");
player=2;
}});
}
public void player2(){
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n2");
player=1;
}});
}
I can see what could be the problem but I don't know what to do.
Replace the loop
for(int i=0; i<3;i++){
if(player==1){
player1();
}
else if (player==2){
player2();
}
}
with just
player1();
player2();
Instead of adding 3 times the same listener to the button add it just once
If all you want to do is enable and disable buttons, why not do:
public void player1(){
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n1");
player=2;
btn1.setEnabled(false);
btn2.setEnabled(true);
}});
}
public void player2(){
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n2");
player=1;
btn1.setEnabled(true);
btn2.setEnabled(false);
}});
}
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);
}
});
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.