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 / December 2005

Tip: Looking for answers? Try searching our database.

why contentPane.remove(jLabel1 ); is not working?

Thread view: 
sree_198114@rediffmail.com - 03 Dec 2005 10:21 GMT
hai all

             i have a problem in my code.i have added the code.when
the button is clicked the label should disappear.but it is not
disappearing.can any one help me.

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

public class card extends JFrame
{
    // Variables declaration
    private JLabel jLabel1;
    private JButton jButton1;
    private JPanel contentPane;

    public card()
    {
        super();
        initializeComponent();
        this.setVisible(true);
    }

    private void initializeComponent()
    {
        jLabel1 = new JLabel();
        jButton1 = new JButton();
        contentPane = (JPanel)this.getContentPane();
          jLabel1.setIcon(new ImageIcon("E:\\cards\\c10.gif"));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                jButton1_actionPerformed(e);
            }

        });
        contentPane.setLayout(null);
        contentPane.setBackground(new Color(23, 122, 17));
        addComponent(contentPane, jLabel1, 62,100,71,99);
        addComponent(contentPane, jButton1, 81,247,83,28);
        this.setTitle("card - extends JFrame");
        this.setLocation(new Point(0, 0));
        this.setSize(new Dimension(473, 509));
    }

    private void addComponent(Container container,Component c,int x,int
y,int width,int height)
    {
        c.setBounds(x,y,width,height);
        container.add(c);
    }
      private void jButton1_actionPerformed(ActionEvent e)
    {
        System.out.println("\njButton1_actionPerformed(ActionEvent e)
called.");
        contentPane.remove(jLabel1 );

    }

    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        try
        {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

        }
        catch (Exception ex)
        {
            System.out.println("Failed loading L&F: ");
            System.out.println(ex);
        }
        new card();
    }

}
sibling - 03 Dec 2005 14:04 GMT
repaint the contentPane may solve this problem.
Roedy Green - 03 Dec 2005 14:33 GMT
>              i have a problem in my code.i have added the code.when
>the button is clicked the label should disappear.but it is not
>disappearing.can any one help me.

This is C-like code. Normally you use a layout and revalidate after
removing a component. See http://mindprod.com/jgloss/layout.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 03 Dec 2005 19:20 GMT
>               i have a problem in my code.i have added the code.when
> the button is clicked the label should disappear.but it is not
> disappearing.can any one help me.

It does disappear, for me, if you switch to another window and then
switch back.

> import java.awt.*;
>  import java.awt.event.*;
>  import javax.swing.*;
>
>  public class card extends JFrame
>  {

It's generally a bad idea to extend a class you do not need to.

>      // Variables declaration
>      private JLabel jLabel1;
[quoted text clipped - 13 lines]
>          jButton1 = new JButton();
>          contentPane = (JPanel)this.getContentPane();

You should avoid casting if possible. It is easy enough to create a
panel and then set it as the frame's content pane. For my money, code
can be written more easily that way.

>            jLabel1.setIcon(new ImageIcon("E:\\cards\\c10.gif"));
>          jButton1.setText("jButton1");
[quoted text clipped - 5 lines]
>
>          });

You might as well have put the action within the action listener. The
worse the useless variable names point to work of a really bad GUI builder.

>          contentPane.setLayout(null);
>          contentPane.setBackground(new Color(23, 122, 17));
>          addComponent(contentPane, jLabel1, 62,100,71,99);
>          addComponent(contentPane, jButton1, 81,247,83,28);

Layout managers would make this sort of data redundant, and also make
the layout work on different GUI configurations.

>          this.setTitle("card - extends JFrame");
>          this.setLocation(new Point(0, 0));
[quoted text clipped - 12 lines]
> called.");
>          contentPane.remove(jLabel1 );

For most uses setVisible works better.

If you add or remove a component on a visible container, you should
paint and validate it. Usually with revalidate and repaint.

>      }
>
[quoted text clipped - 13 lines]
>              System.out.println(ex);
>          }

Was that necessary to demonstrate the problem? When tracking a problem,
and particularly when posting a newsgroup, it generally helps to remove
all non-essential code.

>          new card();

You need the usual boilerplate to make sure all Swing code is run on the
Event Dispatch Thread (EDT).

        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    ... use swing here ...
                }
        });

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Simon OUALID - 09 Dec 2005 23:53 GMT
>  public class card extends JFrame

Class names should begin with a cap.

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html


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.