Solved

Modifying a clear all

Posted on 2004-04-11
16
304 Views
Last Modified: 2006-11-17
Hello everyone I have another question

I think all that would have to be remove from this would be the componets and container to make this be generic, but I'm not sure.  Can someone help me out here.


import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ClearAll extends JFrame
{
     public ClearAll()
     {
          super();

          initialize();
     }

     private void initialize()
     {
          JPanel panel = (JPanel) getContentPane();
          panel.setLayout(new FlowLayout());

          panel.add(new JTextField("Hello"));
          panel.add(new JTextField("Hai"));
          panel.add(new JTextField("New"));
          panel.add(new JTextField("Test"));

          panel.add(new JCheckBox("Check box 1"));
          panel.add(new JCheckBox("Check box 2"));
          panel.add(new JCheckBox("Check box 3"));
          panel.add(new JCheckBox("Check box 4"));
          panel.add(new JCheckBox("Check box 5"));

          panel.add(new JTextField("Last one"));

          JButton button = new JButton("Clear all");
          panel.add(button);

          button.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent e)
               {
                    clearAll();
               }
          });

          setSize(300, 300);
          setVisible(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     }

     public void clearAll()
     {
          List list = new ArrayList();
          Component temp;
          Container container = null;
         
          int count = getComponentCount();

          do
          {
               if (list.size() > 0)
               {
                    container = (Container)list.remove(0);
                    count = container.getComponentCount();
               }
               
               for (int i = 0; i < count; i++)
               {
                    if (container != null)
                    {
                         temp = container.getComponent(i);
                    }
                    else
                    {
                         temp = getComponent(i);
                    }

                    if (temp instanceof JTextField)
                    {
                         ((JTextField) temp).setText("");
                    }
                    else if (temp instanceof JCheckBox)
                    {
                         ((JCheckBox) temp).setSelected(false);
                    }
                    else if (temp instanceof Container)
                    {
                         list.add(temp);
                         continue;
                    }                    
               }
          }
          while (list.size() > 0);
     }

     public static void main(String[] args)
     {
          new ClearAll();
     }
}
0
Comment
Question by:Drop_of_Rain
  • 8
  • 6
  • 2
16 Comments
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
What is it that you wanna do ? I didnt get your question ?
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I want this to be the clearall class I can call from the  newmenuItem from the File menu of the program. I think if the example container the #1 code below that it would work for the program except it could have to be modified for AbstractButton as well.
Code included code #2

We changed the buttons to AbstractButton because I was told the code would work for buttons and checkboxs.

#1
JPanel panel = (JPanel) getContentPane();
          panel.setLayout(new FlowLayout());

          panel.add(new JTextField("Hello"));
          panel.add(new JTextField("Hai"));
          panel.add(new JTextField("New"));
          panel.add(new JTextField("Test"));

          panel.add(new JCheckBox("Check box 1"));
          panel.add(new JCheckBox("Check box 2"));
          panel.add(new JCheckBox("Check box 3"));
          panel.add(new JCheckBox("Check box 4"));
          panel.add(new JCheckBox("Check box 5"));

          panel.add(new JTextField("Last one"));

          JButton button = new JButton("Clear all");
          panel.add(button);

          button.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent e)
               {
                    clearAll();
               }
          });

          setSize(300, 300);
          setVisible(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     }






#2
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * @author Javatm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Kempos extends JPanel {
     AbstractButton check;
     JLabel label;
     JPanel gridPanel;
     ArrayList myArrayList;
     /**
      *Constructor for the MyClass4 object
      */
     public Kempos() {

          myArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, 3));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("           Kempos");
          label.setForeground(Color.blue);
          setBackground(Color.white);
          looprow : for (int row = 0; row < 20; row++) {
               for (int col = 0; col < 3; col++) {
                    int n = (20 * col + row + 1);
                    if (n > 50) {
                         break looprow;
                    }
                    check = new JButton("" + n);
                    //check = new JCheckBox("" + n);
                    myArrayList.add(check);
                    gridPanel.add(check);
                    check.addMouseListener(new ColorChanger(check));
                   
               }
          }
     }

         class ColorChanger extends MouseAdapter {

         AbstractButton check;
          public ColorChanger(JButton check) {
          this.check = check;
          }

          public void mouseEntered(MouseEvent e) {
          // To change the color of the panel . . .
          check.setBackground(Color.green);  
          check.setForeground(Color.black);
          }

          public void mouseClicked(MouseEvent e) {
          check.setBackground(null);
          check.setForeground(Color.black);
          }
     }

     public static void main(String args[]) {

     JFrame x = new JFrame();
     x.getContentPane().add(new Kempos());
     x.setSize(400,300);
     x.setVisible(true);  
     }
     }
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
As I checked your codes everything looks fine ? it clears every checkmarks and text on
the textfields and checkboxses.

However if you want to clear all components from the other class then you need to instantiet them in your
clear all class and thats the time you could clear them.

I created a whole new set of clearing all :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ClearAll extends JFrame
{
     private JPanel panel;
     private JButton b1, b2;

     public ClearAll()
     {
          setTitle("Example . . .");
      
          panel = new JPanel();
          panel.setLayout(new FlowLayout());

          panel.add(new JTextField("Hello"));
          panel.add(new JTextField("Hai"));
          panel.add(new JTextField("New"));
          panel.add(new JTextField("Test"));

          panel.add(new JCheckBox("Check box 1"));
          panel.add(new JCheckBox("Check box 2"));
          panel.add(new JCheckBox("Check box 3"));
          panel.add(new JCheckBox("Check box 4"));
          panel.add(new JCheckBox("Check box 5"));

          panel.add(new JTextField("Last one"));

          JPanel p1 = new JPanel();
          p1.setBorder(BorderFactory.createLineBorder(Color.gray));

          b1 = new JButton("Back");
          b2 = new JButton("Clear");
          p1.add(b1); p1.add(b2);

          b1.setEnabled(false);

          this.getContentPane().add(panel, BorderLayout.CENTER);
          this.getContentPane().add(p1, BorderLayout.SOUTH);

          setSize(300, 300);
          setVisible(true);

          b1.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent e)
               {
                    backAll();
               }
          });

          b2.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent e)
               {
                    clearAll();
               }
          });
     }

     private void backAll()
     {
     b1.setEnabled(false);
     panel.setVisible(true);
     }

     private void clearAll()
     {
     b1.setEnabled(true);
     panel.setVisible(false);
     }

     public static void main(String[] args)
     {
         ClearAll x = new ClearAll();
         x.setDefaultCloseOperation(EXIT_ON_CLOSE);
     }
}

If you have any questions just let me know ?

Friend : Javatm
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I know it works mmuruganandam gave me this as an example.  Give me an example with the code above #2


0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 400 total points
Comment Utility
public class ClearAll
{
     public ClearAll()
     {
     }

     public void clearAll(Container container)
     {
          List list = new ArrayList();
          Component temp;
         
          int count = 0;

          do
          {
               if (list.size() > 0)
               {
                    container = (Container)list.remove(0);
                    count = container.getComponentCount();
               }
               
               for (int i = 0; i < count; i++)
               {
                    if (container != null)
                    {
                         temp = container.getComponent(i);
                    }
                    else
                    {
                         temp = getComponent(i);
                    }

                    if (temp instanceof JTextField)
                    {
                         ((JTextField) temp).setText("");
                    }
                    else if (temp instanceof JCheckBox)
                    {
                         ((JCheckBox) temp).setSelected(false);
                    }
                    else if (temp instanceof Container)
                    {
                         list.add(temp);
                         continue;
                    }                    
               }
          }
          while (list.size() > 0);
     }
}


This is how you can make your ClearAll a generic one.

In a frame based application, you need to call,

new ClearAll().clearAll(this);

This way, you can reuse this code in different places.  JFrame is the top most container.  So you need to pass "this" to the clearAll method.

That way, all the sub-containers and its components would be iterated.



Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Thanks for the support again,
Christopher
0
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
Its my pleasure
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Could you help me with this one as well?

Arraylist needs to be changed
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Javatm
Comment Utility
It gave me a hardtime experimenting but here it goes I created a different format
which you can just rearrange afterwards.

If you click any buttons it will give a color to it, if you click clear all button it
will give back its original color :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ChangeColor1 extends JPanel {

     private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
     private JButton clear;

     public ChangeColor1() {

     clear = new JButton("Clear All");

     b1 = new JColoredButton("1", Color.yellow, Color.black);
     b2 = new JColoredButton("2", Color.yellow, Color.black);
     b3 = new JColoredButton("3", Color.yellow, Color.black);
     b4 = new JColoredButton("4", Color.yellow, Color.black);
     b5 = new JColoredButton("5", Color.yellow, Color.black);
     b6 = new JColoredButton("6", Color.yellow, Color.black);
     b7 = new JColoredButton("7", Color.yellow, Color.black);
     b8 = new JColoredButton("8", Color.yellow, Color.black);
     b9 = new JColoredButton("9", Color.yellow, Color.black);

     setLayout(new BorderLayout());

     JPanel p1 = new JPanel(new GridLayout(3,3));
     JPanel p2 = new JPanel();

     p1.add(b1); p1.add(b2); p1.add(b3);
     p1.add(b4); p1.add(b5); p1.add(b6);
     p1.add(b7); p1.add(b8); p1.add(b9);

     p2.add(clear);

     add(p1, BorderLayout.CENTER);
     add(p2, BorderLayout.SOUTH);

     clear.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent h)
     {
     b1.setBackground(new Color(210, 210, 210));
     b2.setBackground(new Color(210, 210, 210));
     b3.setBackground(new Color(210, 210, 210));
     b4.setBackground(new Color(210, 210, 210));
     b5.setBackground(new Color(210, 210, 210));
     b6.setBackground(new Color(210, 210, 210));
     b7.setBackground(new Color(210, 210, 210));
     b8.setBackground(new Color(210, 210, 210));
     b9.setBackground(new Color(210, 210, 210));
     }
     });
     }

     public static void main(String args[]) {

     JFrame x = new JFrame();
     x.getContentPane().add(new ChangeColor1());
     x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     x.setTitle("Example . . .");
     x.setSize(400, 300);
     x.setVisible(true);
     }
}

class JColoredButton extends JButton implements ActionListener {

     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;
     private boolean state = true;
     
     public JColoredButton(String label, Color background, Color foreground) {

     super(label);
     this.background = background;
     this.foreground = foreground;
     defaultBackground = getBackground();
     deafultForeground = getForeground();
     addActionListener(this);
     }

     public void actionPerformed(ActionEvent e) {
     if (state) {
     setBackground(background);
     setForeground(foreground);
     } else {
     setBackground(defaultBackground);
     setForeground(deafultForeground);
     }
     state = !state;
     }
}

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Joops, I wasted 2 hrs and I didnt even know that it was already answered. You said that I need
to make an example of problem number 2. I created one that should provided an example
but as I see it you've already accepted an answer.
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Javatm if you just click the same button again it turns back to its original color.

I you look at this comment above this took care of it.


Accepted Answer from mmuruganandam
Date: 04/11/2004 09:37PM PDT
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Sorry about that I though you had finished for the evening and the answer did come in.
You did learn something from what you did and look at the answer to learn what it can offer you. It is a great answer, powerfull with a small amount of code.

You didn't answer me if you got my last email from yahoo


Joops, I wasted 2 hrs and I didnt even know that it was already answered. You said that I need to make an example of problem number 2. I created one that should provided an example but as I see it you've already accepted an answer.

I also thought that it would be for clearing everything not just buttons, sorry for the lack of clarity. I will work harder on this.
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Sorry I did not recieve your e-mail and another thing the accepted answer from mmuruganandam
did it work for problem number 2 ? did it cleared anything becasue as what I thought was to
clear the current colors of the buttons.

Another thing is that I already mentioned on my above comments that you could call the other
class from your clear class method see :

Comment from Javatm
Date: 04/12/2004 09:48AM PHT



0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Anyways just never mind it as long as your problem is already resolved
I'm already happy . . .

Friend : Javatm
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Sorry I did not recieve your e-mail
I'll resend it.

and another thing the accepted answer from mmuruganandam
did it work for problem number 2 ? Yes it clear everything in the JFrame

did it cleared anything becasue as what I thought was to
clear the current colors of the buttons.

Where did you get that idea from?

That was sample code that mmuruganandam and he modified it.
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
That was sample code that mmuruganandam and he modified it.


I use sample code all the time. I don't care where the code comes from I just need the answer.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

11 Experts available now in Live!

Get 1:1 Help Now