Solved

Modifying a clear all

Posted on 2004-04-11
16
339 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
[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
  • 8
  • 6
  • 2
16 Comments
 
LVL 15

Expert Comment

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

Author Comment

by:Drop_of_Rain
ID: 10803020
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
ID: 10803128
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Drop_of_Rain
ID: 10803159
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
ID: 10803591
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
ID: 10803612
Thanks for the support again,
Christopher
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10803622
Its my pleasure
0
 

Author Comment

by:Drop_of_Rain
ID: 10803644
Could you help me with this one as well?

Arraylist needs to be changed
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10803690
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
ID: 10803696
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
ID: 10803706
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
ID: 10803733
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
ID: 10803753
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
ID: 10803756
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
ID: 10803767
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
ID: 10803804
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

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
Maven Project: Hibernate Dependencies Conflict 10 96
Delete Android all or certain applications data programmatically 9 153
Problem to Alipay 10 95
maximize the sum of fractions 33 76
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 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