1

When a user clicks a JButton in my Java-Swing application, a string is returned from a method and the user then needs to be able to read the string (somehow). The JButton is within a JPanel. My first thought was to create an 'alert' dialogue (thinking this would be easy), I tried to follow this example that looked easy: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExample.htm

I have not yet been able to confirm if this works because I do not know how to import the libraries into eclipse. For example import org.eclipse.swt.SWT; gives the error "... cannot be resolved".

So one possible solution is how to import in Eclipse. Another possible solution is to dynamically change the text within the JPanel somehow.

1
  • Have you thought of adding a JLabel to your panel and then setting the text of the JLabel with the return value from your method like myLabel.setText("abcdefg") Commented Nov 17, 2011 at 18:22

3 Answers 3

1

As Ben mentioned in his comment. I would set a jLabel with a blank text to start. Then, when you click your button that triggers the method, simply tack on:

label.setText(value);

Alternatively you could use another pane to popup and display the message.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're talking about Swing, an easy solution is to pop a message box with the string:

button.addActionListener(new ActionListener {
    public void actionPerformed(ActionEvent e) {
        String message = methodThatReturnsYourString();
        JOptionPane.showMessageDialog(null, message);
    }
}

Comments

0

In this link you will find the information u require in order to add the library and import it:

http://www.eclipsepluginsite.com/swt.html

as well I would not mix SWT with swing and I would stick to swing you can set the label on the event of your button

button.addActionListener(new ActionListener {
    public void actionPerformed(ActionEvent e) {
        String message = "Test String";
        labelMessage.setText(message );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.