Results 1 to 7 of 7
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default add remove swing components

    What do you do you when, in a swing context, like to add and remove a JPanel, when pushing a button?
    tried add. remove. but did not work.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    it should work. Try setVisible(false) and setVisible(true) else.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you post a small, simple program that demonstrates what you are trying to do?
    A JFrame with some panels and a button. Nothing else.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees (again) with Norm. Why don't you show us in code, or if you can't do that with a screen capture, just exactly what it is you are trying to do. As a first guess though, I'd suggest you look into the CardLayout, a layout that allows you to swap JPanels (or any JComponent) as desired.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default

    this actually did it
    setVisible(false) and setVisible(true).

    fieldPanel.setPreferredSize(new Dimension(40, 40));
    fieldPanel.setLayout(new GridLayout(4,2));
    //fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.Y_AXIS));
    fieldPanel.setAlignmentX(Component.LEFT_ALIGNMENT) ;
    fieldPanel.add( new JLabel(" md1 "), BorderLayout.LINE_END);
    fieldPanel.add(md0field, BorderLayout.CENTER);
    fieldPanel.add( new JLabel(" md2 "), BorderLayout.LINE_END);
    fieldPanel.add(md1field, BorderLayout.CENTER);
    fieldPanel.add( new JLabel(" md3 "), BorderLayout.LINE_END);
    fieldPanel.add(md2field, BorderLayout.CENTER);
    fieldPanel.add( new JLabel(" delay"), BorderLayout.LINE_END);
    fieldPanel.add(delayfield, BorderLayout.CENTER);
    testPanel.add(fieldPanel);

    So it concerns the fieldPanel
    wrote at the constructor:
    buildPanel();
    drawPanel();
    fieldPanel.setVisible(false);

    At some button:

    Java Code:
    if (source ==  midipedalTest)  {
                   
                    if (midipedalTest.isSelected()) {
                       infoPane.setText(pedaltext);
                       fieldPanel.setVisible(true);
                       ed.md.setPedaldata0(Integer.parseInt(md0field.getText()));
                       ed.md.setPedaldata1(Integer.parseInt(md1field.getText()));
                       ed.md.setPedaldata2(Integer.parseInt(md2field.getText()));
                       ed.setDelay(Integer.parseInt(delayfield.getText()));
                       pedtest  = new PedaltestThread();
                       pedtest.start();
                    }
                    else {
                        stoptest=true;
                        fieldPanel.setVisible(false);
                        synchronized (ed.md.getPedlock()){ // while in play thread waitloop sleeps						
                              ed.md.getPedlock().notify();      // notify the pedal test of fboController
                        }
                    }
            
               }
    here is the result:
    WILLEM DRAGSTRA WEB SITE

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default

    by the way supamagier I could not see your
    games on my macbook because of this:

    java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :620)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:124)
    at sun.applet.AppletClassLoader.findClass(AppletClass Loader.java:177)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.applet.AppletClassLoader.loadClass(AppletClass Loader.java:119)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
    at sun.applet.AppletClassLoader.loadCode(AppletClassL oader.java:605)
    at sun.applet.AppletPanel.createApplet(AppletPanel.ja va:723)
    at sun.plugin.AppletViewer.createApplet(AppletViewer. java:1861)
    at jep.AppletFramePanel.createApplet(AppletFramePanel .java:189)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java: 652)
    at sun.applet.AppletPanel.run(AppletPanel.java:326)
    at jep.AppletFramePanel.run(AppletFramePanel.java:176 )
    at java.lang.Thread.run(Thread.java:613)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The version numbers in class files change. Older versions of java do NOT recognize the newer numbers and complain.
    What version of java do you have?
    What version of java created the program you tried to execute?

Similar Threads

  1. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 10:23 PM
  2. Swing Components Placing
    By ne2000 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 08:00 AM
  3. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 10:06 AM
  4. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 12:59 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •