Results 1 to 7 of 7
  1. #1
    Kjevo is offline Member
    Join Date
    Jul 2015
    Posts
    2
    Rep Power
    0

    Unhappy Not adding a jpanel to a frame makes other jpanel not show up

    Java Code:
    package mapEditor;
    
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    
    public class TwoD2 extends JFrame{
    	private static final long serialVersionUID = 1L;
    	
    	JPanel optionsPanel, whichGroundPanel, backgroundPanel;
    	MapPiecesPanel mapPiecesPanel;
    	JScrollPane mapPiecesScrollPane;
    	JRadioButton bgButton, mgButton, fgButton;
    	ButtonGroup gButtonGroup;
    	public JProgressBar progressBar;	
    	JScrollPane mapScrollPane;
    	public static JMenuBar menuBar;
    	public static JMenu menu;
    	public static JMenu subMenu;
    	public static JMenuItem menuItem;	
    	
    	
    	MapPanel mapPanel;
    	public static TwoD2 twoD2;
    	both.InitDatabase initDatabase;
    	both.LoadImages loadImages;
    	public static both.MySql mySql;
    	
    	
    	public static void main (String args[]){
    		twoD2 = new TwoD2();
    		twoD2.initialize();
    	}
    	
    	private void initialize() {
    
                    //START BUGFIX
    		[B][U]backgroundPanel = new JPanel();
    		backgroundPanel.setBackground(Color.WHITE);
    		backgroundPanel.setBounds(0, 0, 870, 680);
    		twoD2.add(backgroundPanel);	
                   //END BUGFIX
    
    		twoD2.setJMenuBar(returnJMenuBar());
    		twoD2.setVisible(true);
    		twoD2.setSize(870, 680);
    		twoD2.setLayout(null);
    		twoD2.setResizable(true);
    
    		mySql = new both.MySql();
    		initDatabase = new both.InitDatabase();
    		loadImages = new both.LoadImages();
    		
    		doMapScrollPane();		
    		doMapPiecesScrollPane();
    		twoD2.add(mapPiecesScrollPane);
    		twoD2.add(mapScrollPane);
    		twoD2.revalidate();
    		twoD2.repaint();
    	}
    
    	TwoD2(){	
    		whichGroundPanel  = new JPanel();
    		whichGroundPanel.setLayout(null);
    		bgButton = new JRadioButton("Bg: ");
    		bgButton.setBounds(0, 0, 60, 20);
    		mgButton = new JRadioButton("Mg: ");
    		mgButton.setBounds(0, 20, 60, 20);
    		fgButton = new JRadioButton("Fg: ");
    		fgButton.setBounds(0, 40, 60, 20);
    		
    		gButtonGroup = new ButtonGroup();
    		gButtonGroup.add(bgButton);
    		gButtonGroup.add(mgButton);
    		gButtonGroup.add(fgButton);
    		
    		whichGroundPanel.add(bgButton);
    		whichGroundPanel.add(mgButton);
    		whichGroundPanel.add(fgButton);
    		whichGroundPanel.setBounds(0, 5, 50, 60);
    
    	    progressBar = new JProgressBar();
    	    progressBar.setValue(100);
    	    progressBar.setStringPainted(true);
    	    progressBar.setBorder(BorderFactory.createTitledBorder("Busy..."));		
    		progressBar.setBounds(735, 5, 120, 60);
    	    
    		optionsPanel = new JPanel();
    		optionsPanel.setLayout(null);
    		optionsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.black));
    		optionsPanel.add(whichGroundPanel);
    		optionsPanel.add(progressBar);
    		optionsPanel.setBounds(0, 545, 870, 70);
    		
    		add(optionsPanel);
    	}
    	
    	private void doMapScrollPane() {
    		mapPanel = new MapPanel();
    		mapPanel.addMouseListener(new MapPanelMouseListener());
    		mapPanel.addMouseMotionListener(new MapPanelMouseListener());
    		
    		mapPanel.setPreferredSize(new Dimension(both.Variabelen.size[0]*50, both.Variabelen.size[1]*50));
    		
    		mapScrollPane = new JScrollPane(mapPanel);
    		mapScrollPane.setWheelScrollingEnabled(false);
    		mapScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		mapScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		mapScrollPane.getVerticalScrollBar().setUnitIncrement(50);
    		mapScrollPane.getHorizontalScrollBar().setUnitIncrement(50);
    		mapScrollPane.setBounds(0, 0, 800, 545);	
    	}
    	
    	private void doMapPiecesScrollPane(){
    		mapPiecesPanel = new MapPiecesPanel();
    		
    		mapPiecesScrollPane = new JScrollPane(mapPiecesPanel);
    		mapPiecesScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		mapPiecesScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		mapPiecesScrollPane.getVerticalScrollBar().setUnitIncrement(50);
    		mapPiecesScrollPane.setPreferredSize(new Dimension(50, 670));
    		mapPiecesScrollPane.setBounds(800, 0, 70, 545);	
    	}
    	
    	private JMenuBar returnJMenuBar(){
    		menuBar = new JMenuBar();
    		
    		menu = new JMenu("Opties");
    		menuBar.add(menu);
    		
    		menuItem = new JMenuItem("Save");
    		menu.add(menuItem);
    		
    		return menuBar;
    	}
    }
    VS

    Java Code:
    package mapEditor;
    
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    
    public class TwoD2 extends JFrame{
    	private static final long serialVersionUID = 1L;
    	
    	JPanel optionsPanel, whichGroundPanel, backgroundPanel;
    	MapPiecesPanel mapPiecesPanel;
    	JScrollPane mapPiecesScrollPane;
    	JRadioButton bgButton, mgButton, fgButton;
    	ButtonGroup gButtonGroup;
    	public JProgressBar progressBar;	
    	JScrollPane mapScrollPane;
    	public static JMenuBar menuBar;
    	public static JMenu menu;
    	public static JMenu subMenu;
    	public static JMenuItem menuItem;	
    	
    	
    	MapPanel mapPanel;
    	public static TwoD2 twoD2;
    	both.InitDatabase initDatabase;
    	both.LoadImages loadImages;
    	public static both.MySql mySql;
    	
    	
    	public static void main (String args[]){
    		twoD2 = new TwoD2();
    		twoD2.initialize();
    	}
    	
    	private void initialize() {
    		twoD2.setJMenuBar(returnJMenuBar());
    		twoD2.setVisible(true);
    		twoD2.setSize(870, 680);
    		twoD2.setLayout(null);
    		twoD2.setResizable(true);
    
    		mySql = new both.MySql();
    		initDatabase = new both.InitDatabase();
    		loadImages = new both.LoadImages();
    		
    		doMapScrollPane();		
    		doMapPiecesScrollPane();
    		twoD2.add(mapPiecesScrollPane);
    		twoD2.add(mapScrollPane);
    		twoD2.revalidate();
    		twoD2.repaint();
    	}
    
    	TwoD2(){	
    		whichGroundPanel  = new JPanel();
    		whichGroundPanel.setLayout(null);
    		bgButton = new JRadioButton("Bg: ");
    		bgButton.setBounds(0, 0, 60, 20);
    		mgButton = new JRadioButton("Mg: ");
    		mgButton.setBounds(0, 20, 60, 20);
    		fgButton = new JRadioButton("Fg: ");
    		fgButton.setBounds(0, 40, 60, 20);
    		
    		gButtonGroup = new ButtonGroup();
    		gButtonGroup.add(bgButton);
    		gButtonGroup.add(mgButton);
    		gButtonGroup.add(fgButton);
    		
    		whichGroundPanel.add(bgButton);
    		whichGroundPanel.add(mgButton);
    		whichGroundPanel.add(fgButton);
    		whichGroundPanel.setBounds(0, 5, 50, 60);
    
    	    progressBar = new JProgressBar();
    	    progressBar.setValue(100);
    	    progressBar.setStringPainted(true);
    	    progressBar.setBorder(BorderFactory.createTitledBorder("Busy..."));		
    		progressBar.setBounds(735, 5, 120, 60);
    	    
    		optionsPanel = new JPanel();
    		optionsPanel.setLayout(null);
    		optionsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.black));
    		optionsPanel.add(whichGroundPanel);
    		optionsPanel.add(progressBar);
    		optionsPanel.setBounds(0, 545, 870, 70);
    		
    		add(optionsPanel);
    	}
    	
    	private void doMapScrollPane() {
    		mapPanel = new MapPanel();
    		mapPanel.addMouseListener(new MapPanelMouseListener());
    		mapPanel.addMouseMotionListener(new MapPanelMouseListener());
    		
    		mapPanel.setPreferredSize(new Dimension(both.Variabelen.size[0]*50, both.Variabelen.size[1]*50));
    		
    		mapScrollPane = new JScrollPane(mapPanel);
    		mapScrollPane.setWheelScrollingEnabled(false);
    		mapScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		mapScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		mapScrollPane.getVerticalScrollBar().setUnitIncrement(50);
    		mapScrollPane.getHorizontalScrollBar().setUnitIncrement(50);
    		mapScrollPane.setBounds(0, 0, 800, 545);	
    	}
    	
    	private void doMapPiecesScrollPane(){
    		mapPiecesPanel = new MapPiecesPanel();
    		
    		mapPiecesScrollPane = new JScrollPane(mapPiecesPanel);
    		mapPiecesScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		mapPiecesScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		mapPiecesScrollPane.getVerticalScrollBar().setUnitIncrement(50);
    		mapPiecesScrollPane.setPreferredSize(new Dimension(50, 670));
    		mapPiecesScrollPane.setBounds(800, 0, 70, 545);	
    	}
    	
    	private JMenuBar returnJMenuBar(){
    		menuBar = new JMenuBar();
    		
    		menu = new JMenu("Opties");
    		menuBar.add(menu);
    		
    		menuItem = new JMenuItem("Save");
    		menu.add(menuItem);
    		
    		return menuBar;
    	}
    }
    with the part between comments being the bugfix that works, but why does adding another jpanel to the frame make the 2nd jpanel how up?
    The JPanel not showing up being optionsPanel.
    Willing to post SSCCE if it is needed. Probably missing something/doing something wrong.
    Will be here for any questions and or information needed.
    Thanks already!
    Last edited by Kjevo; 07-07-2015 at 08:48 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    What bold underlined part?

    Stop using setBounds(...). Learn to use layout managers: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    Also don't use setPreferredSize(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    Moved from New to Java to AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Kjevo is offline Member
    Join Date
    Jul 2015
    Posts
    2
    Rep Power
    0

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    Het DB! Thanks for the interest in my problem! I changed it since bold/underline doesn't seem to work in code tags.
    It's between comments now. Adding that part makes it so that the other(optionsPanel) becomes visible.
    And i am not using a layout manager because i want more control, If i were to use one which one would you recommend?
    Thanks!
    Last edited by Kjevo; 07-07-2015 at 08:50 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    Quote Originally Posted by Kjevo View Post
    And i am not using a layout manager because i want more control,
    Famous last words...

    Quote Originally Posted by Kjevo View Post
    If i were to use one which one would you recommend?
    Take your pick. I've already given you a tutorial link, and the first subtopic is a visual guide.

    Always remember that you can nest JPanels, each with a different layout, to achieve the desired result.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Not adding a jpanel to a frame makes other jpanel not show up

    Quote Originally Posted by Kjevo View Post
    And i am not using a layout manager because i want more control
    That would imply that layout managers take away control, but they only change it to a type of control that is far easier to manage and far more reliable.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Adding JPanel to JPanel not showing
    By rru96 in forum AWT / Swing
    Replies: 5
    Last Post: 08-11-2013, 04:45 AM
  2. Scrolling in JPanel makes the content erased
    By Mathi_r in forum Java Applets
    Replies: 3
    Last Post: 07-28-2011, 02:01 PM
  3. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  4. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM

Tags for this Thread

Posting Permissions

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