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
Solved

A)Action for button. B)Dynamic allocation

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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