Solved

A)Action for button. B)Dynamic allocation

Posted on 1998-08-30
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 262
What browser will run Java? 7 166
Coding for the first time 9 95
Setup Eclipse for Andriod development 2 15
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 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