0

I need to create a gui in java which will take a string from a user and then user will click on submit button.On clicking the submit button,the user will do some processing on the string and then give output on screen(gui).

I have written the following code till now but when I run this code,it doesn't give any output.

public class userinterface extends javax.swing.JFrame {
 
 public userinterface() {
 initComponents();
 }
 
 private javax.swing.JButton jButton1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JPanel jPanel1;
 private javax.swing.JPanel jPanel2;
 private javax.swing.JPanel jPanel3;
 private javax.swing.JPanel jPanel5;
 private javax.swing.JTextField jTextField1;
 // End of variables declaration 
 
 public void show() {
 String str = jTextField1.getText();
 
 jButton1.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 //Execute when button is pressed
 System.out.println("You clicked the button,str");
 }
 });
 }
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new userinterface().setVisible(true);
 userinterface obj = new userinterface();
 obj.show();
 }
 });
 }
}

Please tell me where am I doing wrong ? How can I make the output display on gui screen?

Thanks.

Matt Ke
3,78912 gold badges35 silver badges53 bronze badges
asked Apr 21, 2012 at 17:07
2
  • 2
    Does the class even compile? You seem to call this method: initComponents() without the class having the method. Have you gone through the Oracle Swing tutorials? The basic Java tutorials? Commented Apr 21, 2012 at 17:15
  • yes,class compiles(actually I have removed some auto-generated code).The problem must be in these two functions. Commented Apr 21, 2012 at 17:18

2 Answers 2

4

Your problem is you've overridden the essential method show() without realizing it, and this prevents the JFrame from displaying. Change the name of this method to something different:

public void myShow() {
 // String str = jTextField1.getText(); // not useful
 jButton1.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 // Execute when button is pressed
 System.out.println("You clicked the button,str");
 }
 });
}
public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 // new userinterface().setVisible(true);
 userinterface obj = new userinterface();
 // obj.show();
 obj.myShow();
 obj.setVisible(true);
 }
 });
}

This is another reason that we all should strive not to extend classes unless absolutely necessary, since doing so can occasionally cause very difficult to debug errors.

I was able to debug this by placing println's throughout your code and seeing that show() was being called even when it wasn't explicitly being called by me.

answered Apr 21, 2012 at 17:31
Sign up to request clarification or add additional context in comments.

1 Comment

Any decent IDE will show you an icon in the sidebar indicating you overrode something of the super-class, which should ring a bell. But I do agree with your remark about not having to extend the Swing component classes unless you have a very good reason
0
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("this is GUI boi");
frame.setSize(500, 500);
panel.add(label);
frame.add(panel);
frame.setVisible(true);
  1. you have to instantiate all the gui components
  2. you have to call the setVisible() method of the JFrame
  3. you have to set a width and height of the JFrame
  4. you have add the components to a parent component(like a label to a panel and that panel to a frame)

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.