javaswingjframejbuttonborder-layout

Why are my JButton and JLists not appearing? (BorderLayout


I'm trying to learn swing and work through the task we have been given. I can't see why the code doesn't display the JButtons in the SOUTH section as there is no issue when displaying the textfields, combobox and labels in the CENTER section.

I used the same format to add components to my CENTER section as I did in the SOUTH and EAST but only the center displays anything.

public class ProductListGUI{
    JMenu menu;
    JMenuItem about,importData,inventory,export;

    ProductListGUI(){
        JFrame f = new JFrame("Assignment 2");

        JPanel p1 = new JPanel();
        JList<String> list = new JList<>();
        list.setBounds(600,0,200,600);
        JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JPanel p3 = new JPanel();

        p1.setLayout(null);
        p1.setBounds(0,0,600,500);
        p1.setBackground(new Color(230,230,230));
        scrollPane.setLayout(null);
        scrollPane.setBounds(600,0,200,500);
        p3.setLayout(null);
        p3.setBounds(0,500,800,100);
        p3.setBackground(new Color(230,230,230));

        JLabel l1,l2,l3,l4;
        JTextField t1,t2,t3;
        l1=new JLabel("ProductID");
        l1.setBounds(10,100,200,30); 
        t1=new JTextField();  
        t1.setBounds(100,100,200,30);
        l2=new JLabel("Name");
        l2.setBounds(10,150,200,30); 
        t2=new JTextField();
        t2.setBounds(100,150,200,30);
        l3=new JLabel("Quantity");
        l3.setBounds(10,250,200,30); 
        t3=new JTextField();
        t3.setBounds(100,250,200,30);
        p1.setBorder(BorderFactory.createTitledBorder("Product Details"));

        JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
        checkBox.setBounds(10,300,250,50);

        l4 = new JLabel("Item Type");
        l4.setBounds(10,200,200,30);
        String[] itemType = {"Select type","Homeware","Hobby","Garden"};
        JComboBox dropdown = new JComboBox(itemType);
        dropdown.setBounds(100,200,120,20);

        p1.add(t1);p1.add(l1);p1.add(t2);p1.add(l2);p1.add(t3);p1.add(l3);p1.add(l4);p1.add(dropdown);p1.add(checkBox);


        JButton b1 = new JButton("New Item");
        b1.setBounds(200,550,80,20);
        JButton b2 = new JButton("Save");
        b2.setBounds(300,550,80,20);
        JButton b3 = new JButton("Delete Selected");
        b3.setBounds(600,550,80,20);
        b3.setEnabled(false);

        p3.add(b1);p3.add(b2);p3.add(b3);


        JMenuBar mb = new JMenuBar();
        menu = new JMenu("Actions");

        about = new JMenuItem("About");
        importData = new JMenuItem("Import Data");
        inventory = new JMenuItem("Inventory");
        export = new JMenuItem("Export to CSV");

        menu.add(about);menu.add(importData);menu.add(inventory);menu.add(export);
        mb.add(menu);
        
        f.getContentPane().add(p1,BorderLayout.CENTER);
        f.getContentPane().add(scrollPane,BorderLayout.EAST);
        f.getContentPane().add(p3,BorderLayout.SOUTH);
        f.setJMenuBar(mb);
        f.setSize(800,600);
        f.setLayout(new BorderLayout());
        f.setVisible(true);
    }

Solution

  • These code changes should help at least a little bit.

    Things that had to change:

    import javax.swing.*;
    import java.awt.*;
    
    public class ProductListGUI {
    
        JMenu menu;
        JMenuItem about,importData,inventory,export;
    
        ProductListGUI(){
            JFrame f = new JFrame("Assignment 2");
    
            JPanel panel1 = new JPanel();
            JList<String> list = new JList<>();
            list.setBounds(600,0,200,600);
            JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            JPanel panel3 = new JPanel();
    
            panel1.setBounds(0,0,600,500);
            panel1.setBackground(new Color(230,230,230));
            scrollPane.setBounds(600,0,200,500);
            panel3.setBounds(0,0,800,100);
            panel3.setBackground(new Color(230,230,230));
    
            JLabel l1,l2,l3,l4;
            JTextField t1,t2,t3;
            l1=new JLabel("ProductID");
            l1.setBounds(10,100,200,30);
            t1=new JTextField();
            t1.setBounds(100,100,200,30);
            l2=new JLabel("Name");
            l2.setBounds(10,150,200,30);
            t2=new JTextField();
            t2.setBounds(100,150,200,30);
            l3=new JLabel("Quantity");
            l3.setBounds(10,250,200,30);
            t3=new JTextField();
            t3.setBounds(100,250,200,30);
            panel1.setBorder(BorderFactory.createTitledBorder("Product Details"));
    
            JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
            checkBox.setBounds(10,300,250,50);
    
            l4 = new JLabel("Item Type");
            l4.setBounds(10,200,200,30);
            String[] itemType = {"Select type","Homeware","Hobby","Garden"};
            JComboBox dropdown = new JComboBox(itemType);
            dropdown.setBounds(100,200,120,20);
    
            panel1.add(t1);
            panel1.add(l1);
            panel1.add(t2);
            panel1.add(l2);
            panel1.add(t3);
            panel1.add(l3);
            panel1.add(l4);
            panel1.add(dropdown);
            panel1.add(checkBox);
    
    
            JButton b1 = new JButton("New Item");
            JButton b2 = new JButton("Save");
            JButton b3 = new JButton("Delete Selected");
            b3.setEnabled(false);
    
            panel3.add(b1);
            panel3.add(b2);
            panel3.add(b3);
    
    
            JMenuBar mb = new JMenuBar();
            menu = new JMenu("Actions");
    
            about = new JMenuItem("About");
            importData = new JMenuItem("Import Data");
            inventory = new JMenuItem("Inventory");
            export = new JMenuItem("Export to CSV");
    
            menu.add(about);
            menu.add(importData);
            menu.add(inventory);
            menu.add(export);
            mb.add(menu);
    
            f.setLayout(new BorderLayout());
            f.getContentPane().add(panel1,BorderLayout.CENTER);
            f.getContentPane().add(scrollPane,BorderLayout.EAST);
            f.getContentPane().add(panel3,BorderLayout.SOUTH);
            f.setJMenuBar(mb);
            f.setSize(800,600);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            ProductListGUI gui = new ProductListGUI();
        }
    }
    

    Here is a very simple BorderLayout example that might help in the future, or not.

    import javax.swing.*;
    import java.awt.*;
    
    public class TestGui {
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame("Test Frame");
    
            frame.setLayout(new BorderLayout());
            frame.setSize(800,600);
            frame.getContentPane().add(createJPanel("CENTER", Color.RED), BorderLayout.CENTER);
            frame.getContentPane().add(createJPanel("NORTH", Color.CYAN), BorderLayout.NORTH);
            frame.getContentPane().add(createJPanel("EAST", Color.LIGHT_GRAY), BorderLayout.EAST);
            frame.getContentPane().add(createJPanel("SOUTH", Color.GREEN), BorderLayout.SOUTH);
            frame.getContentPane().add(createJPanel("WEST", Color.YELLOW), BorderLayout.WEST);
    
            frame.setVisible(true);
        }
    
        private static JPanel createJPanel(String title, Color color) {
    
            JPanel jPanel = new JPanel();
            jPanel.setLayout(new BorderLayout());
            jPanel.add(new JLabel(title), BorderLayout.CENTER);
            jPanel.setBackground(color);
    
            return jPanel;
        }
    }