0

I have an app and when you run it you take a panel in order to add 3 values and after you need press the OK button in order to continue.

I put a Click() method but when i push OK nothing happen.

Also to mention when i am deluging is working but when i export it as executable jar is not.

JFrame frame = new JFrame();
JLabel mlabel = new JLabel("Please provide xxxx",JLabel.CENTER);
JLabel uLabel = new JLabel("User ID:",JLabel.LEFT);
JPanel buttonField = new JPanel(new GridLayout (1,3));
JPanel userArea =  new JPanel(new GridLayout (0,3));


frame.setLayout(new GridLayout (0,1));
buttonField.setLayout(new FlowLayout());
JButton confirm =new JButton("OK");
confirm.addMouseListener((MouseListener) new mouseClick());
buttonField.add(confirm);

App.insertText = new JTextField(20);
frame.add(mlabel);
userArea.add(uLabel);
userArea.add(insertText);
frame.add(buttonField);
frame.setSize(300,600);
App.credGet = false;
frame.setVisible(true); 

and the Click :

public void mouseClicked(MouseEvent e) {
    App.un = App.insertText.getText();
    App.project = ((JTextComponent) App.insertProject).getText();
    //App.pw = char[] App.insertPass.getPassword();
    char[] input = App.insertPass.getPassword();
    App.pw = "";
    for (int i1 = 0; i1 < input.length; i1++){
        App.pw = App.pw + input[i1];
    }
} 

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
ChristosV
  • 35
  • 1
  • 10
  • 4
    1. Use the proper listener for the right need, and here never use a MouseListener in this way, and instead use an ActionListener when listening for JButton presses. 2. You have unusual field references suggesting possible over-use of static fields -- but I really can't tell based on what you've posted so far. 3. For better help, create and post a valid [mcve]. – Hovercraft Full Of Eels Feb 13 '16 at 22:40

3 Answers3

2

Line

confirm.addMouseListener((MouseListener) new mouseClick());

I suppose mouseClick is a class you posted in the example below. Why do you cast it to MouseListener? Does it not implement the MouseListener?

Anyway, you'll be better off replacing it with an ActionListener (anonymous class will work fine for you here), e.g.

confirm.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        ...         
    }
});

Read Pros and cons of using ActionListener vs. MouseListener for capturing clicks on a JButton for more info

Community
  • 1
  • 1
radoh
  • 4,554
  • 5
  • 30
  • 45
1

You should do something like this:

JButton button = new JButton("ButtonName");
//add button to frame
frame.add(button);    
//Add action listener to button
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("You clicked the button");
        }
    });         
Onez
  • 91
  • 10
1

You could either use an ActionListener using something like this:

anyBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //Your Code Here
    }
});

Or you could use a Lambda as shown here:

anyBtn.addActionListener(e ->
{
    //Your Code Here
});

You should not be using a MouseListener in that way ever. That is not what it is intended for.

Jonah
  • 1,013
  • 15
  • 25