🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8504

Re: How to change multiple objects within a JPanel

#8504
From: Torsten Welches
Date: Thu, 15 Mar 2001 20:31
48 lines
2271 bytes
Tom,

use the instanceof operator and apply a cast, like this:

for (int i=0;i<components;i++) {
  Component c = object.getComponent(i);
  if (c instanceof JTextField) {
    ((JTextField)c).setText("");
    ...
  } else if (c instanceof ...) {
    ...
  } ...
}

Torsten

Tom Harrop schrieb in Nachricht <3ab10685$1_2@dnews>...
>I currently have a JPanel object that contains multiple text boxes, labels,
>a checkbox and a button.
>At a certain point I want to reset all of the items in the JPanel.  I want
>the text boxes to be blank, the checkboxes unchecked and so forth.  I have
>been trying to use the component method to iterate through the JPanel
>objects, but I can reset the text boxes because .setText() is not
>available.. Anyone have any suggestions on how to do this instead of going
>through all of the object items manually.
>
>Here is the code snippet I tried to use
>
>void clearFields(JPanel object)
> {
>  int components=object.getComponentCount();
>  int val=0;
>  JTextField testTextField = new JTextField();
>  JCheckBox testCheckBox = new JCheckBox();
>    for (int i=0;i<components;i++)
>  {
>    if(object.getComponent(i).getClass() == testTextField.getClass())
>    object.getComponent(i).setText("");  /*This is part that doesn't
>work???? */
>
>    if(object.getComponent(i).getClass() == testCheckBox.getClass())
>    object.getComponent(i).setEnabled(false);
>
>
>  }
> }
>
>
Technical Details
Message-ID: <3ab1276a_1@dnews>
Path: arc1-ca.pugleaf.net!pubnews.netcom.net.uk!diablo.netcom.net.uk!netcom.net.uk!fr.clara.net!heighliner.fr.clara.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!195.14.254.156
References: <3ab10685$1_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,919520a7a625add8
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-17 07:55:12 PST
From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Newsgroups: borland.public.jbuilder.java.language
References: <3ab10685$1_2@dnews>
Subject: Re: How to change multiple objects within a JPanel
Date: Thu, 15 Mar 2001 21:31:02 +0100
Lines: 50
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
NNTP-Posting-Host: 195.14.254.156
Message-ID: <3ab1276a_1@dnews>
X-Trace: 15 Mar 2001 12:34:50 -0800, 195.14.254.156
Path: arc1-ca.pugleaf.net!pubnews.netcom.net.uk!diablo.netcom.net.uk!netcom.net.uk!fr.clara.net!heighliner.fr.clara.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!195.14.254.156