How can i unload a JPanel?

Hi, i have a JFrame with a JPanel.. i would like to know how to unload it after it's used, thanks....

From the JFrame JavaDoc:
void      remove(Component comp)
          Removes the specified component from the container.I am not sure if you need to get the content pane, and call remove( myJPanel ) on the content pane, or just simply call remove( myJPanel ) from the JFrame.
But, you can test that.
� {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • How can I hide a JPanel?

    I have some JPanels and using the mouse motion listeners you can scribble on them. I want to be able to switch between these panels but the problem is the drawings get wiped off when I use repaint() and frame.add(panel).
    Any ideas how else I could try doing this?

    Ok thanks everyone. I've taken the MyPanel class from here http://java.sun.com/docs/books/tutorial/uiswing/painting/step3.html
    class MyPanel extends JPanel {
        private int squareX = 50;
        private int squareY = 50;
        private int squareW = 20;
        private int squareH = 20;
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    moveSquare(e.getX(),e.getY());
            addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent e) {
                    moveSquare(e.getX(),e.getY());
        private void moveSquare(int x, int y) {
            int OFFSET = 1;
            if ((squareX!=x) || (squareY!=y)) {
                repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
                squareX=x;
                squareY=y;
                repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);      
            g.drawString("This is my custom Panel!",10,20);
            g.setColor(Color.RED);
            g.fillRect(squareX,squareY,squareW,squareH);
            g.setColor(Color.BLACK);
            g.drawRect(squareX,squareY,squareW,squareH);
    }Now if I use this class to make two panels, how can I hide one and bring the other one up and vice versa, using two buttons? setVisible() still doesn't work properly.
    Here's the whole code.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseMotionAdapter;
    public class PanelTest extends JFrame {
        private MyPanel panelOne;
        private MyPanel panelTwo;
        private BorderLayout layoutManager;
        private PanelTest() {
            layoutManager = new BorderLayout();
            this.setLayout(layoutManager);
            panelOne = new MyPanel();
            panelTwo = new MyPanel();
            panelOne.add(new JLabel("This is Panel One"));
            panelTwo.add(new JLabel("This is Panel Two"));
            setCurrentPanel(panelOne);
            JButton switchButton = new JButton("Switch");
            switchButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(getCurrentPanel() == panelOne) {
                        setCurrentPanel(panelTwo);
                    } else {
                        setCurrentPanel(panelOne);
            this.getContentPane().add(switchButton, BorderLayout.SOUTH);
            this.pack();
            this.setVisible(true);
        private void setCurrentPanel(MyPanel panel) {
            this.getContentPane().add(panel, BorderLayout.CENTER);
            panel.revalidate();
            panel.repaint();
        private JPanel getCurrentPanel() {
            return (JPanel)layoutManager.getLayoutComponent(BorderLayout.CENTER);
        public static void main(String[] args) {
            new PanelTest();
    class MyPanel extends JPanel {
        private int squareX = 50;
        private int squareY = 50;
        private int squareW = 20;
        private int squareH = 20;
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    moveSquare(e.getX(),e.getY());
            addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent e) {
                    moveSquare(e.getX(),e.getY());
        private void moveSquare(int x, int y) {
            int OFFSET = 1;
            if ((squareX!=x) || (squareY!=y)) {
                repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
                squareX=x;
                squareY=y;
                repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);      
            g.setColor(Color.RED);
            g.fillRect(squareX,squareY,squareW,squareH);
            g.setColor(Color.BLACK);
            g.drawRect(squareX,squareY,squareW,squareH);
    }Edited by: atom.bomb on Mar 30, 2010 11:25 AM

  • How can I close a JPanel

    Hello,
    I have a GUI that uses a JPanel to acquire user information and returns it to the main GUI class, after I get that information I want to close automatically that JPanel with out closing the whole application, how can I do that?
    I have been looking for a close method but I can not find one, any suggestions will be greatly appreciated.
    Thanks.

    Never mind,
    I guess I did not try the dispose() method yet, thanks anyway!!!!!

  • How can I unload v4.0 and re-install v3.6 so that RealPlayer will work.

    RealPlayerSPplus worked well with Firefox 3.6 but does not work with v4.o. The people at RealPlayer say that the video download feature of RealPlayer is incompatible with v4.0. I would like to go back to Firefox v3.6. How can I do this. (The people at RealPlayer say that their product does work with Internet Explorer and Chrome.

    Sharma,
    Here's step by step what I did after your last post:
    1) I re-installed Flash from my account.
    2) I Deleted the other account, along with all files associated with it.
    3) I created a new account with administrator privilege.
    4) I logged off my account and logged onto the new account.
    5) I started WINDOWS explorer and navigated to windows/system32/Macromed/Flash
    6) I ran FlashUtil10c (See Screen Shot One)
    7) Clicked "Install Now", program ran and I got a box that indicated it was 100% complete.
    8) As I watched, FlashUtil10c, Flash10c.ocx and uninstall_activeX all three disappeared from the folder. (See Screen Shot 2)
    9) Logged 0ff the newly created account and logged back into my account.
    10) Tried to access a game that required flash and got the screen in Screen Shot 3.
    11) Re-installed Flash from my account.
    I'm right back where I started. This has got to be a common thing for people to do. Where am I going wrong? The installer I'm using is "Install_flash_player_10_active_x" that I downloaded from a site I got with my free license and about which Adobe declared: "You may not share the above link, share information with others, or publish the above link on websites, blogs, or by any other means that can be publicly accessed. "
    I appreciate your help.
    Wayne

  • How can I make my JPanels appear overlapped on each other?

    Hi there
    In my program i have say 3 JPanels inside a one big JPanel (container), and I want those 3 JPanels inside, appear in an overlapped fashion. Obviously the layout of the container jpanel does not allow me to that. Is there a work around to that?
    I am aware of the JLayeredPane, but as far as i know i cannot add a JPanel into it. Or can I?
    Any help appreciated.

    You can add JPanels to a container in an overlapped fashion, when you switch off the layout manger of the container by:
    bigPanel.setLayout(null);then you have to layout the container yourself by calling the
    setBounds(x,y,width,height) method on the 3 panels.
    But I don't know how to control the z-order of the panels.
    I think using JLayeredPane is the better solution.
    JLayerdPane definitely works with JPanels.

  • How can I Print a JPanel including all added Components?

    Hello dear saviours,
    I have a JPanel object which is responsible for displaying a Graph.
    I need to Print the Components of this JPanel as close to what they look like to the user as possible.
    I thought about casting the JPanel into an Image of some kind, but couldn't find anything but how to add am Image to a JPanel (God damned search engines :-).
    But wait, this gets more interesting!
    I need to have control over the scale of the Printed matterial.
    I want the option to choose between a single page and dividing the drawing the JPanel displays into Multiple-Pages.
    In both cases I need full details of all the data (Nodes, Arcs, Text describing the various Nodes and Arcs names or type, etc.).Keeping the sizes of all these components is also important (that means, I don't want the nodes to be tinny-winny and the Fonts to be twice as large as the nodes and so on...).
    Attached is the code of the PrintUtillity class I created as an API for printing the JPanel data:
    package ild;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    /** A simple utility class that lets you very simply print
    * an arbitrary component. Just pass the component to the
    * PrintUtilities.printComponent. The component you want to
    * print doesn't need a print method and doesn't have to
    * implement any interface or do anything special at all.
    * <P>
    * If you are going to be printing many times, it is marginally more
    * efficient to first do the following:
    * <PRE>
    * PrintUtilities printHelper = new PrintUtilities(theComponent);
    * </PRE>
    * then later do printHelper.print(). But this is a very tiny
    * difference, so in most cases just do the simpler
    * PrintUtilities.printComponent(componentToBePrinted).
    * 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
    * May be freely used or adapted.
    public class PrintUtilities implements Printable {
    private Component componentToBePrinted;
    public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
    public static void printComponent(Component c) {
    new PrintUtilities(c).print();
    public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
    try {
    printJob.print();
    } catch(PrinterException pe) {
    System.out.println("Error printing: " + pe);
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    return(NO_SUCH_PAGE);
    } else {
    Graphics2D g2d = (Graphics2D)g;
    // double scale = 2;
    // g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    double scaleFactor = java.lang.Math.min((double)this.componentToBePrinted.getSize().width/(double)(pageFormat.getImageableWidth()),
    (double)this.componentToBePrinted.getSize().height/(double)(pageFormat.getImageableHeight()));
    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g2d.scale(1.0/scaleFactor,1.0/scaleFactor);
    // disableDoubleBuffering(componentToBePrinted);
    componentToBePrinted.paint(g2d);
    enableDoubleBuffering(componentToBePrinted);
    return(PAGE_EXISTS);
    /** The speed and quality of printing suffers dramatically if
    * any of the containers have double buffering turned on.
    * So this turns if off globally.
    * @see enableDoubleBuffering
    public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
    /** Re-enables double buffering globally. */
    public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);

    That's a nice utility, but my JPanel gets truncated when printed. Is there a way to print a JPanel in full.

  • How can i unload a sequence from another sequence

    Hello,
    i have a sequence which is running in a loop. At a specified point the sequence executes an ActiveX-call an runs "Test UUTs" with a sequence-name that is passed with a variable.
    When "TestUUT" is running then first the SequenceFileLoad of the new sequence is executed and after that the Mainsequence. Thats all good up to here.
    But the next time when i call the execute "Test UUTs" then the file is already in loaded and no SequenceFileLoad is running.
    So my question is: What do i have to call/run that the loaded sequence is leaving the memory with SequenceFileUnload and the next time when i run "TestUUTs" is completely new loaded?
    Thx for your help
    Attached a screenshot of how i execute the "TestUUTs"
    Attachments:
    Clipboard01.jpg ‏42 KB

    The thing is, i only want to execute the SequenceFileUnload when i have a new sequence name in the variable of "Execute Test UUTs".
    If the sequence is loaded and executed again, then i dont want to run SequenceFileLoad again.
    Thats why i want to execute an "unload"-thing somehow.
    It looks like this:
    string seqname= sequence1
    call execute "Test UUTs" (seqname)
    SequenceFileLoad(sequence1)
    execute(seqname)
    call execute "Test UUTs" (seqname)
    //no SequenceFileLoad because its already loaded
    execute(seqname)
    call execute "Test UUTs" (seqname)
    //no SequenceFileLoad because its already loaded
    execute(seqname)
    string seqname= sequence2
    SequenceFileUNLoad(sequence1)
    SequenceFileLoad(sequence2)
    execute(seqname)
    call execute "Test UUTs" (seqname)
    //no SequenceFileLoad because its already loaded
    execute(seqname)

  • How can you unload your iphone contacts to a computer

    When I purchased my phone from ATT, they copied my contacts from my existing phone memory card to the new iphone.  When looking in the iphone, the names aren't first and last, they're just one name so instead of 'Bob Jones' it came as  'Jones;Bob/H  - it was Bob Jones Home phone number.' .  No way to sort by 1st name because there isn't anyfirst name. 
    I would like to unload the contacts from the iphone into my pc, edit them and then load them back into the iphone.  Suggestions??
    Ron

    Previous to starting this, I synced to icloud. And I  believe that itunes backed up all the info to windows contacts, so they were in 2 places and the phone.
    From windows contacts, I deleted all the contacts and created several 'good' entries in windows contacts.
    I went to the phone and went to settings>icloud>contacts and turnd the contacts OFF and I have a choice of  1)keep on my phone, 2)delete from my iphone, or cancel.  So no choice to MERGE; nothing referring to ADVANCED.   Am I in the right place to do what you're suggesting??  I don't think I am.
    Ron

  • How can I unload data to ASCII fixed-format or delimited files,

    Hello,
    SQL*Loader loads data from ASCII fixed-format or delimited files. Is there such utility to unload data from Oracle database ?
    Agnieszka

    No. The Export utility will dump data out but not into an ascii file.
    Do you need the whole database or just some table or result of a query set?
    Check out the ULT_FILE package - this will allow you to write some SQL code which will do the job you want.
    Cheers,
    R

  • I just want to know how can i unload may balance.? Im in japan store but my friend gave me itunes card from canada. I still have 145 yen. How can i unload that balance i want to change to canada store

    A

    Contact iTunes Customer Service and request assistance
    Use this Link  >  Apple  Support  iTunes Store  Contact
    Note:  iTunes Gift Cards are only Valid in Country of Issue

  • HT1925 i have delete whatsapp by mistake and now the new version is not compatible with my iphone, how can i take it back again ?

    Hello i need help, i unloaded the new version of whatsapp and now is not working because my phone is not compatible, i delete it, and now i can not unloaded it again ! how can i unload the old version again ? thanks

    If you did not make a backup of the old version of the app before you deleted it, there's no way you can get it back now. The current version should still work on your iPhone 3GS, though. You just need iOS 4.3 or later, so if your iPhone still has an older version, update to the latest iOS.
    If, however, you really have an iPhone 3G, not a 3GS, then unless again you make a backup copy of the old version of the app, there's nothing you can do until you're ready to get a newer iPhone since iOS 4.2.1 is the final version your iPhone 3G will run.
    This question, by the way, would have better been posted in the Using iPhone forum rather than here in the QuickTime forum.
    Regards.

  • How can I display JTextFields correctly on a JPanel using GridBagLayout?

    I had some inputfields on a JPanel using the boxLayout. All was ok. Then I decided to change the panellayout to GridBagLayout. The JLabel fields are displayed correctly but the JTextField aren't. They are at the JPanel but have a size of 0??? So we cannot see what we type in these fields... Even when I put some text in the field before putting it on the panel.
    How can I display JTextFields correctly on a JPanel using GridBagLayout?
    here is a shortcut of my code:
    private Dimension sFieldSize10 = new Dimension(80, 20);
    // Create and instantiate Selection Fields
    private JLabel lSearchAbrText = new JLabel();
    private JTextField searchAbrText = new JTextField();
    // Set properties for SelectionFields
    lSearchAbrNumber.setText("ABR Number (0-9999999):");
    searchAbrNumber.setText("");
    searchAbrNumber.createToolTip();
    searchAbrNumber.setToolTipText("enter the AbrNumber.");
    searchAbrNumber.setPreferredSize(sFieldSize10);
    searchAbrNumber.setMaximumSize(sFieldSize10);
    public void createViewSubsetPanel() {
    pSubset = new JPanel();
    // Set layout
    pSubset.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    // Add Fields
    gbc.gridy = 0;
    gbc.gridx = GridBagConstraints.RELATIVE;
    pSubset.add(lSearchAbrNumber, gbc);
    // also tried inserting this statement
    // searchAbrNumber.setText("0000000");
    // without success
    pSubset.add(searchAbrNumber,gbc);
    pSubset.add(lSearchAbrText, gbc);
    pSubset.add(searchAbrText, gbc);
    gbc.gridy = 1;
    pSubset.add(lSearchClassCode, gbc);
    pSubset.add(searchClassCode, gbc);
    pSubset.add(butSearch, gbc);
    }

    import java.awt.*;
    import java.awt.event.*;
    import javax .swing.*;
    public class GridBagDemo {
      public static void main(String[] args) {
        JLabel
          labelOne   = new JLabel("Label One"),
          labelTwo   = new JLabel("Label Two"),
          labelThree = new JLabel("Label Three"),
          labelFour  = new JLabel("Label Four");
        JLabel[] labels = {
          labelOne, labelTwo, labelThree, labelFour
        JTextField
          tfOne   = new JTextField(),
          tfTwo   = new JTextField(),
          tfThree = new JTextField(),
          tfFour  = new JTextField();
        JTextField[] fields = {
          tfOne, tfTwo, tfThree, tfFour
        Dimension
          labelSize = new Dimension(125,20),
          fieldSize = new Dimension(150,20);
        for(int i = 0; i < labels.length; i++) {
          labels.setPreferredSize(labelSize);
    labels[i].setHorizontalAlignment(JLabel.RIGHT);
    fields[i].setPreferredSize(fieldSize);
    GridBagLayout gridbag = new GridBagLayout();
    JPanel panel = new JPanel(gridbag);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.insets = new Insets(5,5,5,5);
    panel.add(labelOne, gbc);
    panel.add(tfOne, gbc);
    gbc.gridwidth = gbc.RELATIVE;
    panel.add(labelTwo, gbc);
    gbc.gridwidth = gbc.REMAINDER;
    panel.add(tfTwo, gbc);
    gbc.gridwidth = 1;
    panel.add(labelThree, gbc);
    panel.add(tfThree, gbc);
    gbc.gridwidth = gbc.RELATIVE;
    panel.add(labelFour, gbc);
    gbc.gridwidth = gbc.REMAINDER;
    panel.add(tfFour, gbc);
    final JButton
    smallerButton = new JButton("smaller"),
    biggerButton = new JButton("wider");
    final JFrame f = new JFrame();
    ActionListener l = new ActionListener() {
    final int DELTA_X = 25;
    int oldWidth, newWidth;
    public void actionPerformed(ActionEvent e) {
    JButton button = (JButton)e.getSource();
    oldWidth = f.getSize().width;
    if(button == smallerButton)
    newWidth = oldWidth - DELTA_X;
    if(button == biggerButton)
    newWidth = oldWidth + DELTA_X;
    f.setSize(new Dimension(newWidth, f.getSize().height));
    f.validate();
    smallerButton.addActionListener(l);
    biggerButton.addActionListener(l);
    JPanel southPanel = new JPanel(gridbag);
    gbc.gridwidth = gbc.RELATIVE;
    southPanel.add(smallerButton, gbc);
    gbc.gridwidth = gbc.REMAINDER;
    southPanel.add(biggerButton, gbc);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(panel);
    f.getContentPane().add(southPanel, "South");
    f.pack();
    f.setLocation(200,200);
    f.setVisible(true);

  • How can i save the data in a jpanel form to a .txt document

    how can i save the data in a jpanel form to a text file using parse function,this should happen when i click a save button .
    please help me..

    each time when i fill the form and click save button ,all the data in the form should be written to a text file and each time when i repeat it should append to the old data in the text file.the elements in the form should be seperated by pipe delimeter.
    thanks for your patience..

  • How can i put one scene into JPanel of the swing?

    how can i put one scene into JPanel of the swing?

    980571 wrote:
    ty for the information but i have a problen with JFXpanel when i try to into a panel it doesnt display insideSorry to hear that. If you want assistance with the code, feel free to post the relevant parts of it. Make sure to use \ tags when you do.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How can i transform MFC HWND into jpanel or jframe instance

    Hi!
    I want to open a MFC window, then with the HWND , i want to open java subwindow created inside the MFC window with the HWND, i wonder if there is a way. how can i transform the HWND into jpanel or jframe instance.

    Look at the article in CodeProject and read the example in it. The other questions you can send to my email because I cannot give you concrete response in this forum.

Maybe you are looking for

  • OS 8.6 vs. 9.0

    Is OS 8.6 considered a "classic environment?" I need to install the classic environment on an ibook running "Tiger" so that I can open an old geneology file. That is the closest copy I have to the 9.1. The geneology program used to run on a lime gree

  • Production NTSC or Flastscreen Plasma/LCD monitors?

    I work in network cable television, where I use a broadcast NTSC monitor at work for color correction and any visual effects work. But my bosses view all final output on a large (around 42") flatscreen TV. I forgot the make/model. Now at home, I woul

  • Language Settings in Unattend.txt Win8.1 OSD not working properly

    Hi fellow suffers :) Although I am using the following unattend.txt within my OSD TS Win8.1 is still asking to confirm the "Region and Language" manually at the end of the setup (even if I am choosing english)... what am I doing wrong ?! I am not usi

  • Buying used MacBook Air @craigslist

    Hi there, I plan on buying an MBA from craigslist and would like to know if you folks have any advice on buying a used MBA. I'm making a check list on what to inspect when I see the MBA. Obviously, google is gold, but there is so much content there t

  • The requested feature DSC-Service is not found on the target machine.

    Hi, I'm trying to create a DSC Pull Server with a 2008 R2 SP1 Virtual Machine, While many of the forums and tutorials have helped, particularly https ://www .google.com/url?url=https://davewyatt.wordpress.com/2014/06/07/how-to-install-a-dsc-pull-serv