thebuizelclub thebuizelclub - 7 months ago 16
Java Question

Why Wont the Java Component JPanel wont disapear?

I was trying to set up something with the JMenu where you can switch between "pages" (JPanels) but I ran into an issue where one would not disappear after trying a few methods online. Can someone tell me what is the error I am making?

code:

public class Window {

public static boolean NewTerrainCamPos = false;

public static String textVal;
public static String textVal2;
public static String resiveTex;
public static String resiveTex2;

public static final int Width = 1000;
public static final int Height = 720;
public static final int FPS_CAP = 120;

private static long lastFrameTime;
private static float delta;

public void createDisplay(){

ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);

try {
Canvas openglSurface = new Canvas();
JFrame frame = new JFrame();

JPanel game = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);

//..............Menu Bar...............
JMenuBar menuBar = new JMenuBar();
JMenu terrain = new JMenu("Terrain");
JMenu Home = new JMenu("Home");
menuBar.add(Home);
menuBar.add(terrain);
JMenuItem newTerrain = new JMenuItem("add Terrain");
JMenuItem editTerrain = new JMenuItem("Edit Terrain");
JMenuItem editTexture = new JMenuItem("Edit Texture");

terrain.add(newTerrain);
terrain.add(editTerrain);
terrain.add(editTexture);
frame.setJMenuBar(menuBar);
//......................................................

newTerrain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
NewTerrainCamPos = true;
JFrame frame2 = new JFrame();
frame2.setVisible(true);
frame2.setSize(300, 300);
//...............................
GridLayout experimentLayout = new GridLayout(3,2);
frame2.setLayout(experimentLayout);
//.....................................
JLabel xCord = new JLabel("XCoords: ");
JLabel zCord = new JLabel("ZCoords: ");
JTextField text = new JTextField();
JTextField text2 = new JTextField();

resiveTex2 = text2.getText();

text.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {
resiveTex = text.getText();
}

@Override
public void removeUpdate(DocumentEvent de) {
resiveTex = text.getText();
}

@Override
public void changedUpdate(DocumentEvent de) {
//plain text components don't fire these events
}
});



text2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {
resiveTex2 = text2.getText();
}

@Override
public void removeUpdate(DocumentEvent de) {
resiveTex2 = text2.getText();
}

@Override
public void changedUpdate(DocumentEvent de) {
//plain text components don't fire these events
}
});

JButton createTerrain = new JButton("CreateTerrain");

createTerrain.addActionListener(new ActionListener(){
TIDF terrainFileID;
public void actionPerformed(ActionEvent a){
NewTerrainCamPos = false;
textVal = text.getText();
textVal2 = text2.getText();
TIDF load = new TIDF();
load.terrainIDFile();
}
});

frame2.add(xCord);
frame2.add(text);
frame2.add(zCord);
frame2.add(text2);
frame2.add(createTerrain);
}
});

editTerrain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame frame3 = new JFrame();
frame3.setVisible(true);
frame3.setSize(300, 300);
//......................................
GridLayout experimentLayout = new GridLayout(3,2);
frame3.setLayout(experimentLayout);
//......................................
JButton select = new JButton("Select");
String terrainLocList[] =
{
"Item 1",
"Item 2",
"Item 3",
"Item 4"
};

JList list = new JList(terrainLocList);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setVisibleRowCount(-1);
frame3.add(list);
frame3.add(select);
}
});

editTexture.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {

frame.remove(MainPage());

frame.add(TextureEditor());

frame.revalidate();
}
});

//.........................................

frame.add(MainPage());
frame.add(game);
frame.setSize(1200, 1200);
frame.setVisible(true);
game.add(openglSurface);
game.setBounds(0, 266, 1000, 720);
openglSurface.setSize(1000, 720);
Display.setParent(openglSurface);

Display.setDisplayMode(new DisplayMode(Width, Height));
Display.create(new PixelFormat(), attribs);
frame.setTitle("Game editor 0.3");
} catch (LWJGLException e) {
e.printStackTrace();
}

GL11.glViewport(0, 0, Width, Height);
lastFrameTime = getCurrentTime();
}

public Component MainPage(){
JPanel Main = new JPanel();
Main.setBounds(400, 0, 500, 200);
Label Welcome = new Label("Welcome to: Game Editor Version 0.3");
Welcome.setFont(new Font("Monotype Corsiva",10,22));
Main.add(Welcome);
return Main;
}

public Component TextureEditor(){
JPanel TextureLayoutLook = new JPanel();
TextureLayoutLook.setBounds(60, 0, 200, 200);
Label editorVersion = new Label("Terrain Texture Editor: 0.1");
TextureLayoutLook.add(editorVersion);
return TextureLayoutLook;
}

public static boolean Returnboolean(){
return NewTerrainCamPos;
}

public static String getTex1() {
return textVal;
}

public static String getTex2(){
return textVal2;
}

public static String getTexupdate(){
return resiveTex;
}

public static String getTexupdate2(){
return resiveTex2;
}

public static void updateDisplay(){
Display.sync(FPS_CAP);
Display.update();
long currentFrameTime = getCurrentTime();
delta = (currentFrameTime - lastFrameTime)/1000f;
lastFrameTime = currentFrameTime;
}

public static float getFrameTimeSeconds(){
return delta;
}

public static void closeDisplay(){
Display.destroy();
}

private static long getCurrentTime(){
return Sys.getTime()*1000/Sys.getTimerResolution();
}

}

Answer
frame.add(MainPage());

This line builds a brand new "main page" JPanel, and adds it the the JFrame.

frame.remove(MainPage());

This line of code creates a brand new "main page" JPanel, which has never been added to the JFrame, and attempts to remove it.

This new panel can't be removed, because it was never added. You need to retain a reference to the original panel, and remove that.

JPanel main_page = MainPage();
frame.add(main_page);

//...

frame.remove(main_page);

Note: This main_page could be re-added at a future time without needing to recreate it. Just call frame.add(main_page); again. But really, you want to use the card layout manager.