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.
Printable View
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.
it should work. Try setVisible(false) and setVisible(true) else.
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.
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.
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:
here is the result: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
}
}
}
WILLEM DRAGSTRA WEB SITE
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)
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?