?
Solved

A)Action for button. B)Dynamic allocation

Posted on 1998-08-30
5
Medium Priority
?
251 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 150 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
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

649 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