Solved

A)Action for button. B)Dynamic allocation

Posted on 1998-08-30
5
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now