Solved

A)Action for button. B)Dynamic allocation

Posted on 1998-08-30
5
234 Views
Last Modified: 2010-03-30
A:
I've written a program. It looks:

1. main class
...
...
public class Test extends JFrame
{

      public Test()
      {
            super();
            addWindowListener(new WindowAdapter()
            {
                  public void windowClosing(WindowEvent e)
                  {
                        System.exit(0);
                  }
            });

            JPanel      panel=new JPanel();
            MyButton      mybt=new MyButton("test");
            panel.add(mybt.createBT());
            setContentPane(panel);
      }

      public      static void main(String[] args)
      {
            Test      frame = new Test();
            frame.pack();
            frame.setSize(100,100);
            frame.setVisible(true);
      }
};

2. another class
...
...
public class MyButton extends JComponent
{
      private JButton bt;
      private String      str;

      public MyButton(String str)
      {
            this.str=str;
      }

      public JButton createBT()
      {
            bt=new JButton(this.str);
            return bt;
      }
};

How can I write codes in the MyButton class so that if one clicks the button, an action (for example a dialog) will be shown?

B:
I am going to write a program, in which a string array will be used. At the begining I don't now how big this array should be, because its size will change by run time. How can I solve this problem?
0
Comment
Question by:chenwei
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 50 total points
ID: 1222984
>How can I write codes in the MyButton class so that if one clicks the button,
>an action (for example a  dialog) will be shown?

Instead of having your button class extend Jcomponent, you might want to
have it extend MouseAdapter. This is a listener class that listens to mouse
actions. Once your myButton class extends Jcomponent. do this:

define the mouseClicked(MouseEvent e) method to be:

public void mouseClicked(MouseEvent e) {
    myDialogbox dialog = new myDialogBox();
}

You also need to register the button as a listener. To do this, add a line after

MyButton mybt=new MyButton("test");

the line you should add is:

mybt.addMouseListener(new MyButton());

This way, the MyButton class will listen to mouse clicks, and when the
mouse is clicked, it will invoke mouseClicked() method and pop up the
dialog box.

Java Programming Question
   Title: "A)Action for button. B)Dynamic allocation"

   From: chenwei
                                                           Date: Sunday, August 30 1998 - 08:15AM PDT
   Status: Waiting for answer
   Points: 50 Points (Easy)


   A:
   I've written a program. It looks:

   1. main class
   ...
   ...
   public class Test extends JFrame
   {

   public Test()
   {
   super();
   addWindowListener(new WindowAdapter()
   {
   public void windowClosing(WindowEvent e)
   {
   System.exit(0);
   }
   });

   JPanel panel=new JPanel();
   MyButton mybt=new MyButton("test");
   panel.add(mybt.createBT());
   setContentPane(panel);
   }

   public static void main(String[] args)
   {
   Test frame = new Test();
   frame.pack();
   frame.setSize(100,100);
   frame.setVisible(true);
   }
   };

   2. another class
   ...
   ...
   public class MyButton extends JComponent
   {
   private JButton bt;
   private String str;

   public MyButton(String str)
   {
   this.str=str;
   }

   public JButton createBT()
   {
   bt=new JButton(this.str);
   return bt;
   }
   };

   How can I write codes in the MyButton class so that if one clicks the button, an action (for example a
   dialog) will be shown?

>   B:    I am going to write a program, in which a string array will be used. At the begining
> I don't now how big this  array should be, because its size will change by run time. How
> can I solve this problem?

Use the Vector class instead of an array. You'll need to import java.awt.Vector;

A sample usage is like:

Vector mylist = new Vector();

String s1 = "hi";
String s2 = " world";

mylist.addElement(s1);
mylist.addElement(s2);

System.out.println(mylist.elementAt(0) + mylist.elementAt(1));


The vector class will grow and decrease in size dynamically as you add or remove
items from it. You don't have to worry about incrementing the size, it's done automatically.
Simply addElement as you need and removeElement when you want to get rid of it.


Best of luck!






0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222985
Ooops, I accidently included a whole portion of the copied code. Please disregard the
portion where I reposted your entire code.
0
 
LVL 4

Expert Comment

by:martinag
ID: 1222986
chenwei, Vector is located in java.util package, not java.awt.

Martin
0
 

Author Comment

by:chenwei
ID: 1222987
To sailwinds's answer:
The anser to question B is qite good. But as martinag said, the Vector is in java.util package.

I have some problem to the answer to question A. Here are once again the whole programs:
1)
// class Test

import com.sun.java.swing.*;
import com.sun.java.swing.border.*;
import com.sun.java.accessibility.*;

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Test extends JFrame
{

      public Test()
      {
            super();

            JPanel      panel=new JPanel();
            MyButton      mybt=new MyButton("test");
//            mybt.addMouseListener(new MyButton());
            panel.add(mybt.createBT());
            setContentPane(panel);
      }

      public      static void main(String[] args)
      {
            Test      frame = new Test();
            frame.pack();
            frame.setSize(100,100);
            frame.setVisible(true);
      }
};

2)
import com.sun.java.swing.*;
import java.lang.*;
import java.awt.event.*;

public class MyButton extends MouseAdapter
{
      private JButton bt;
      private String      str;

      public MyButton()
      {
            ;
      }

      public MyButton(String str)
      {
            this.str=str;
      }

      public JButton createBT()
      {
            bt=new JButton(this.str);
            return bt;
      }

      public void mouseClicked(MouseEvent e)
      {
            System.out.println("you click me");  
      }
            
};As I want to compile the Test class, it shows an erorr message:
Method addMouseListener(MyButton) not found in class MyButton:
  addMouseListener(new MyButton());

Could you just send me the program code?
0
 

Author Comment

by:chenwei
ID: 1222988
I've solved the question B myself. Thanks for the help from all sites.

I am going to have vacation form tomorrow for about 4 weeks. Please don't answer my question any more during my vacation
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question