Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2005

Tip: Looking for answers? Try searching our database.

why is remove(myPanel2) not working?

Thread view: 
Geoff Cox - 29 Jul 2005 14:39 GMT
Hello,

For some reason the

 contentArea.remove(myPanel3);

works but the

 contentArea.remove(myPanel2);

does not - any ideas please?

(happy to post the whole code if this helps)

Cheers

Geoff

 if (textIndex == 0)
 {

 contentArea.remove(myPanel2);

 contentArea.remove(myPanel3);
 myPanel3 = new JPanel();
 myPanel3.setBackground(Color.white);
 
 JLabel endMessage = new JLabel("Thank you - all questions
answered");

 myPanel3.add(endMessage);
 contentArea.add(myPanel3, BorderLayout.SOUTH);
 setContentPane(contentArea);
 contentArea.setVisible(true);

}
Andrew Thompson - 29 Jul 2005 14:56 GMT
> For some reason the
>
>   contentArea.remove(myPanel3);
>
> works

[1]

>...but the
>
>   contentArea.remove(myPanel2);

[1]  What do you mean 'it works'?  *Specifically* what
do you mean?  Describe it in detail, ..
<http://www.physci.org/codes/javafaq.jsp#specific>
..or..

> does not - any ideas please?
>
> (happy to post the whole code if this helps)

..provide that code.  Since the word
descriptions are not that clear.

E.G.  you state that removing the myPanel3 'works', yet
your code immediately re-adds it, so I am not certain
how *you* can be sure it is *ever* removed in the first
place.

And look, for my personal preferences, the SSCCE[1] provided
with the brief explanation and question is almost always
necessary.  *Especially* when the poster[2] has no idea what is
going wrong.  The trick is that if the poster does not know
what is going wrong, they often (usually) post the wrong
'bit' of code.

[1] <http://www.physci.org/codes/sscce.jsp>
[2] you, in this instance, but could be anyone.

[ Note: Follow-Ups to this response set to c.l.j.help only. ]

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Beats A Hard Kick In The Face

Geoff Cox - 29 Jul 2005 15:56 GMT
>E.G.  you state that removing the myPanel3 'works', yet
>your code immediately re-adds it, so I am not certain
>how *you* can be sure it is *ever* removed in the first
>place.

Andrew,

Actually the code shows that myPanel2 is to be removed and myPanel3 to
be removed and replaced ....

have added the whole code below.

Cheers

Geoff

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class SocialGroup extends JFrame implements ChangeListener,
ActionListener {

private JSlider scale = new JSlider(-20, +20, 0);
private JLabel position = new JLabel("Set Position");
private JButton nxtButton = new JButton("Next");
private ImageIcon picIcon;
private JButton picButton;
private JPanel myPanel;
private JPanel myPanel2;
private JPanel myPanel3;
private static SocialGroup egg;
private Container contentArea;
private BorderLayout myLayout;
private JLabel sliderTextLhs;
private JLabel sliderTextRhs;
private static int textIndex = 0;
private static String[] textLhs =
{
"I would often be in this situation",
"I would often talk to a group of friends",
"I would not worry if I stuttered here",
"If I stuttered here I would know why",
"I would stutter mildy here",
"If I were to stutter here, my friends would not be able to tell",
"If I were to stutter here I would feel bad",
"If I were to stutter here, my group of friends would be
understanding"
};

private static String[] textRhs =
{
"I would hardly ever be in this situation",
"I would hardly ever talk to a group of friends",
"I would worry if I stuttered here",
"If I were to stutter here I would not know why",
"I would stutter severely here",
"If I were to stutter here, my group of friends would be able to
tell",
"If I were to stutter here I would not feel bad",
"If I were to stutter here, my group of friends would not be
understanding"
};
private int qnumber = 1;

public SocialGroup() {

 super("Slider");
 setSize(800, 600);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setVisible(true);

 contentArea = getContentPane();
 myLayout = new BorderLayout();
 contentArea.setLayout(myLayout);

 scale.addChangeListener(this);

 nxtButton.addActionListener(this);

 myPanel = new JPanel();
 myPanel.setBackground(Color.blue);
 picIcon = new ImageIcon("pic3.jpg");
 picButton = new JButton(picIcon);
 myPanel.add(picButton);

 myPanel2 = new JPanel();
 myPanel2.setBackground(Color.white);

 JLabel slidertextLhs = new JLabel(textLhs[textIndex]);
 JLabel slidertextRhs = new JLabel(textRhs[textIndex]);

 if (++textIndex == textLhs.length)
 textIndex = 0;
 
 myPanel2.add(slidertextLhs);
 myPanel2.add(scale);
 myPanel2.add(slidertextRhs);

 myPanel3 = new JPanel();
 myPanel3.setBackground(Color.white);

 myPanel3.add(nxtButton);

 contentArea.add(myPanel, BorderLayout.NORTH);
 contentArea.add(myPanel2, BorderLayout.CENTER);
 contentArea.add(myPanel3, BorderLayout.SOUTH);

 setContentPane(contentArea);

}

public void stateChanged(ChangeEvent event) {
 JSlider src = (JSlider) event.getSource();
 if (!src.getValueIsAdjusting())
  position.setText("Position is " + scale.getValue());
}

public void actionPerformed(ActionEvent event) {
 Object source = event.getSource();
 if (source == nxtButton) {
  tofile();
 }

 contentArea.remove(myPanel2);

 myPanel2 = new JPanel();
 myPanel2.setBackground(Color.white);
 sliderTextLhs = new JLabel(textLhs[textIndex]);
 scale = new JSlider(-20,+20,0);
 sliderTextRhs = new JLabel(textRhs[textIndex]);
 if (++textIndex == textLhs.length)
 textIndex = 0;
 myPanel2.add(sliderTextLhs);
 myPanel2.add(scale);
 myPanel2.add(sliderTextRhs);
 contentArea.add(myPanel2, BorderLayout.CENTER);
 setContentPane(contentArea);
 contentArea.setVisible(true);

}

public void tofile() {
 try {
  FileWriter outfile = new FileWriter("data.txt", true);
  PrintWriter pw = new PrintWriter(outfile);
  pw.print("question "+qnumber+": ");
  ++qnumber;
  pw.println(scale.getValue());
  pw.close();
 } catch (IOException e) {
  System.out.println(e);
 }

  if (textIndex == 0)

 {
 contentArea.remove(myPanel2);

 contentArea.remove(myPanel3);
 myPanel3 = new JPanel();
 myPanel3.setBackground(Color.white);
 
 JLabel endMessage = new JLabel("Thank you - all questions
answered");

 myPanel3.add(endMessage);
 contentArea.add(myPanel3, BorderLayout.SOUTH);

 setContentPane(contentArea);
 contentArea.setVisible(true);

}

//  System.out.println("tofile incr "+textIndex);

}

public static void main(String[] args) {
 egg = new SocialGroup();
}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.