Adding more than one JPanel to my JFrame(North,South)
-
I want to add two Jpanel on my tabbed pane ,one should be in south with buttons,and other should be in north where I have Text fields and Labels. My problem is that the buttons on container Jpanel is not showing in south. Here is the code:
public class Mainframe extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel container = new JPanel();
JFrame frame = new JFrame("TEST");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
/**
* @param args
*/
private Mainframe()
{Container contentPane = getContentPane(); container.setLayout(new FlowLayout()); BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File("FINAL.jpg")); } catch (IOException e1) { e1.printStackTrace(); } JLabel lbl = new JLabel(new ImageIcon(myPicture)); container.add(lbl); container.add(button1); container.add(button2); container.add(button3); container.add(button4); container.add(button5); jp1.add(container,BorderLayout.SOUTH); frame.add(tabbedPane); tabbedPane.addTab("First Tab",jp1); frame.setPreferredSize(new Dimension(1500, 1210)); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setResizable(true); frame.setVisible(true);
}
} -
I want to add two Jpanel on my tabbed pane ,one should be in south with buttons,and other should be in north where I have Text fields and Labels. My problem is that the buttons on container Jpanel is not showing in south. Here is the code:
public class Mainframe extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel container = new JPanel();
JFrame frame = new JFrame("TEST");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
/**
* @param args
*/
private Mainframe()
{Container contentPane = getContentPane(); container.setLayout(new FlowLayout()); BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File("FINAL.jpg")); } catch (IOException e1) { e1.printStackTrace(); } JLabel lbl = new JLabel(new ImageIcon(myPicture)); container.add(lbl); container.add(button1); container.add(button2); container.add(button3); container.add(button4); container.add(button5); jp1.add(container,BorderLayout.SOUTH); frame.add(tabbedPane); tabbedPane.addTab("First Tab",jp1); frame.setPreferredSize(new Dimension(1500, 1210)); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setResizable(true); frame.setVisible(true);
}
} -
It looks as though you have too many containers. Try simplifying your setup by adding one element at a time to see where it's going wrong. Start with the tab, then add a panel to the tab, then a single item to the panel etc.
Veni, vidi, abiit domum
Here is how I see it can be simplified ,and still the problem exists.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;public class Mainframe extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel Firstpanel = new JPanel();
static JFrame frame = new JFrame("Create Company");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
/**
* @param args
*/
private Mainframe()
{BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File("FINAL.jpg")); } catch (IOException e1) { e1.printStackTrace(); } JLabel lbl = new JLabel(new ImageIcon(myPicture)); frame.add(tabbedPane); jp1.add(Firstpanel,BorderLayout.SOUTH); tabbedPane.addTab("New Employer",jp1); Firstpanel.add(button1); Firstpanel.add(button2); Firstpanel.add(button3); } public static void main(String\[\] args) { frame.setPreferredSize(new Dimension(1500, 1210)); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setResizable(true); frame.setVisible(true); new Mainframe(); // TODO Auto-generated method stub }
}
-
Here is how I see it can be simplified ,and still the problem exists.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;public class Mainframe extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel Firstpanel = new JPanel();
static JFrame frame = new JFrame("Create Company");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
/**
* @param args
*/
private Mainframe()
{BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File("FINAL.jpg")); } catch (IOException e1) { e1.printStackTrace(); } JLabel lbl = new JLabel(new ImageIcon(myPicture)); frame.add(tabbedPane); jp1.add(Firstpanel,BorderLayout.SOUTH); tabbedPane.addTab("New Employer",jp1); Firstpanel.add(button1); Firstpanel.add(button2); Firstpanel.add(button3); } public static void main(String\[\] args) { frame.setPreferredSize(new Dimension(1500, 1210)); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setResizable(true); frame.setVisible(true); new Mainframe(); // TODO Auto-generated method stub }
}
I didn't see any layout set against the JPanel jP1. see this http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html[^]
Regards Shubhashish
-
Here is how I see it can be simplified ,and still the problem exists.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;public class Mainframe extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel Firstpanel = new JPanel();
static JFrame frame = new JFrame("Create Company");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
/**
* @param args
*/
private Mainframe()
{BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File("FINAL.jpg")); } catch (IOException e1) { e1.printStackTrace(); } JLabel lbl = new JLabel(new ImageIcon(myPicture)); frame.add(tabbedPane); jp1.add(Firstpanel,BorderLayout.SOUTH); tabbedPane.addTab("New Employer",jp1); Firstpanel.add(button1); Firstpanel.add(button2); Firstpanel.add(button3); } public static void main(String\[\] args) { frame.setPreferredSize(new Dimension(1500, 1210)); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setResizable(true); frame.setVisible(true); new Mainframe(); // TODO Auto-generated method stub }
}
You need to tell the panel to use a border layout like:
jp1.setLayout(new BorderLayout()); jp1.add(Firstpanel,BorderLayout.SOUTH); tabbedPane.addTab("New Employer",jp1); Firstpanel.add(button1); Firstpanel.add(button2); Firstpanel.add(button3); jp1.add(Secondpanel, BorderLayout.NORTH); Secondpanel.add(button4); Secondpanel.add(button5);
Veni, vidi, abiit domum
-
You need to tell the panel to use a border layout like:
jp1.setLayout(new BorderLayout()); jp1.add(Firstpanel,BorderLayout.SOUTH); tabbedPane.addTab("New Employer",jp1); Firstpanel.add(button1); Firstpanel.add(button2); Firstpanel.add(button3); jp1.add(Secondpanel, BorderLayout.NORTH); Secondpanel.add(button4); Secondpanel.add(button5);
Veni, vidi, abiit domum
-
I didn't see any layout set against the JPanel jP1. see this http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html[^]
Regards Shubhashish