When adding a customized JPanel, everything disappears

J

Justin

I am trying to write an application that uses a JSplitPane. I am
working on customizing the JPanel on the left side of the SplitPane.
So far, the only component I've added to the JPanel is a JScrollPane.
When I add the scrollpane, my splitPane nor my Menu appear when I run
the program. I've pasted my code below. If someone could explain to
me how to fix it, and more importantly, why it occurs, I would greatly
appreciate it. Thanks

CODE*********************************
****************************************

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import java.awt.Container;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class Main extends JFrame implements ActionListener{

private String database = "adsadf";
private DataConnection dc;
private LeftPanel leftPanel;
private JPanel rightPanel;

public Main() {

setVisible(true);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);

leftPanel = new LeftPanel();
rightPanel = new JPanel();

JSplitPane splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
splitPane.setSize(getSize());
splitPane.setDividerLocation(150);

Container c = getContentPane();
c.add(splitPane);

createMenuBar();
}

private void createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem item = new JMenuItem("Exit");
item.setActionCommand("exit");
item.addActionListener(this);
menu.add(item);

setJMenuBar(menuBar);
}

public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("exit")){
System.exit(0);
}
}

public static void main(String[] args) {
Main main = new Main();
}

}


//***************************************************
//******CUSTOM JPANEL***********************
//***************************************************

import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class LeftPanel extends JPanel{

JScrollPane scroller;

public LeftPanel() {
super();

scroller = new JScrollPane();
add(scroller);

}

}
 
J

Justin

I am trying to write an application that uses a JSplitPane. I am
working on customizing the JPanel on the left side of the SplitPane.
So far, the only component I've added to the JPanel is a JScrollPane.
When I add the scrollpane, my splitPane nor my Menu appear when I run
the program. I've pasted my code below. If someone could explain to
me how to fix it, and more importantly, why it occurs, I would greatly
appreciate it. Thanks

CODE*********************************
****************************************

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import java.awt.Container;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class Main extends JFrame implements ActionListener{

private String database = "adsadf";
private DataConnection dc;
private LeftPanel leftPanel;
private JPanel rightPanel;

public Main() {

setVisible(true);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);

leftPanel = new LeftPanel();
rightPanel = new JPanel();

JSplitPane splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
splitPane.setSize(getSize());
splitPane.setDividerLocation(150);

Container c = getContentPane();
c.add(splitPane);

createMenuBar();
}

private void createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem item = new JMenuItem("Exit");
item.setActionCommand("exit");
item.addActionListener(this);
menu.add(item);

setJMenuBar(menuBar);
}

public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("exit")){
System.exit(0);
}
}

public static void main(String[] args) {
Main main = new Main();
}

}

//***************************************************
//******CUSTOM JPANEL***********************
//***************************************************

import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class LeftPanel extends JPanel{

JScrollPane scroller;

public LeftPanel() {
super();

scroller = new JScrollPane();
add(scroller);

}



}- Hide quoted text -

- Show quoted text -

Nevermind, been a couple months since I've written anything, and I
forgot the mysterious power of the validate() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top