0

I have a simple Swing program (TestButton.java) as under

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class TestButton extends JFrame implements ActionListener{

 private final int SIZE = 200; 
 private Container con = getContentPane(); 
 private JButton button = new JButton("Button");
 private TextField text = new TextField(20);
 private int numClicks = 0;

     public TestButton() 
     { 
        super("Button"); 
        setSize(SIZE, SIZE); 
        con.setLayout(new FlowLayout());
        con.add(button); 
        con.add(text); 
        //con.setBackground(Color.BLACK); 
        //button.setBackground(Color.ORANGE); 
        button.setForeground(Color.BLACK); 
        button.addActionListener (this);
     }

     public void actionPerformed(ActionEvent e) {
                numClicks++;
                text.setText("Button Clicked " + numClicks + " times");
        }

     public static void main(String[] args) 
     { 
        TestButton frame = new TestButton(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
     }
}

It basically creates a button and on clicking on that prints the numbers of click on the textbox. Cool!

enter image description here

Now I have created a package "MyPackage". Inside the "MyPackage" folder I have the MyButton java file as under

package MyPackage;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class MyButton extends JFrame implements ActionListener{

     private final int SIZE = 200; 
     private Container con = getContentPane(); 
     private JButton button = new JButton("Button");
     private TextField text = new TextField(20);
     private int numClicks = 0;

       public void show()
       {
            setSize(SIZE, SIZE); 
            con.setLayout(new FlowLayout());
            con.add(button); 
            con.add(text);          
            button.setForeground(Color.BLACK); 
            button.addActionListener (this);
       }

       public void actionPerformed(ActionEvent e) {
                numClicks++;
                text.setText("Button Clicked " + numClicks + " times");
        }
}

It complied correctly. And then I have changed my TestButton.java program as under

import java.io.*;
import javax.swing.*;
import MyPackage.*;

public class TestButton extends JFrame{     

     public TestButton() 
     { 
        super("Button"); 
        MyButton objButton=new MyButton();
        objButton.show();       
     }  

     public static void main(String[] args) 
     { 
        TestButton frame = new TestButton(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
     }
}

Basically I want to execute the Button Creation/Action stuffs from a separate class file and the main program will just invoke it. The current code yields the below output

enter image description here

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
  • so what happens if anything when you click the button – GregH Aug 03 '15 at 16:51
  • *"want to execute the Button Creation/Action stuffs from a separate class file"* I'd suggest to first achieve it from a different class in the **same package.** And going further to make a [mcve], demote it to default access and paste it into the end of the source with `main(String[])` method.. – Andrew Thompson Aug 03 '15 at 16:52
  • @peggy *"so what happens.."* The problem is summed up in the difference between the first screenshot with the expected result and the second (unexpected) result that does not include a button or text field. – Andrew Thompson Aug 03 '15 at 16:54
  • ah sorry about that mate, didn't see the first screenshot – GregH Aug 03 '15 at 16:55
  • As an aside. Rather than add components dynamically (which is a PITA for various reasons), either disable the components before they are needed, or use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Aug 03 '15 at 16:56
  • @peggy *"sorry about that"* Meh.. (shrugs) I don't see how an apology is necessary. :) – Andrew Thompson Aug 03 '15 at 16:57

1 Answers1

0
MyButton extends JFrame

This is the biggest mistake here. Later when You' re doing:

 MyButton objButton=new MyButton();

You are just invoking another JFrame but isn' t even making it visible.

Suggested solution:

Extend JButton and configure layout from the frame level.

Asakura
  • 127
  • 3
  • 14
  • 1
    *"Suggested solution:** Extend JButton ** .."* I have yet to see in this Q&A, justification for extending *any* component.. – Andrew Thompson Aug 03 '15 at 17:33
  • In this particular case author wrote "I want to execute the Button Creation/Action stuffs from a separate class file", so based on what he wants I suggested a solution. In general, You' re propably right :) – Asakura Aug 03 '15 at 18:07
  • 1
    *"..so based on what he wants I suggested a solution."* And how do you justify extending button? Even based on the words in your comment, it makes no sense. – Andrew Thompson Aug 03 '15 at 18:10
  • I understood him as he **wants to have** seperate object (class) which extends `JFrame`, `JButton` or any other component. Thus I did not judge if it' s a good or bad idea. If this isn' t what he wants, then yes, this is not the best solution. If this is what he wants and there is a better solution, I would be grateful if You tell me about it. – Asakura Aug 03 '15 at 18:29
  • 1
    *"I understood him as he **wants to have** seperate object (class) which extends.."* That's an invalid presumption, like saying that any person who posts code with bugs, **wants** bugs. Extending components is just the (less than optimal) way he is currently doing things. To quote the OP *"I **want** to execute the Button Creation/Action stuffs from a separate class"* - no mention of extending classes. -1 for your lack of comprehension skills. – Andrew Thompson Aug 03 '15 at 18:39