3
package Input;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class ProgramEditor2 {
    JFrame frame;
    JPanel contactListPanel;
    ArrayList<JButton> program;

    public ProgramEditor2() {
        initialize();
    }

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            LineEditor.main((JButton) arg0.getSource(),program,contactListPanel);
        }
    };

    public void initialize() {
        frame = new JFrame();
        frame.setSize(471, 298);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);
        frame.getContentPane().setLayout(new BorderLayout());

        program = new ArrayList<JButton>();

        // Set layout for contactListPane
        contactListPanel = new JPanel();
        contactListPanel.setLayout(new GridLayout(15, 1)); // 15 rows, 1 column
        contactListPanel.setMinimumSize(new Dimension(471, 298));
        contactListPanel.setPreferredSize(new Dimension(471, 298));
        contactListPanel.setMaximumSize(new Dimension(471, 298));
        for (int i = 0; i < 1; i++) {
            JButton button = new JButton();
            button.addActionListener(al);
            contactListPanel.add(button);
        }
        JScrollPane scrollPane = new JScrollPane(contactListPanel);
        scrollPane
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    }

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

I am trying to add a JLabel above the scrollpanel, and two JButtons below it.I believe the issue is the fact that I am setting the frame layout to BorderLayout at line 57. How can I got about fixing this problem?

Luffy
  • 1,317
  • 1
  • 19
  • 41

1 Answers1

3

Start by having a closer look at How to Use BorderLayout

I am trying to add a JLabel above the scrollpanel

frame.add(new JLabel("Hello"), BorderLayout.NORTH);

and two JButtons below it

Use a "compound layout" approach by using a different container with a different layout manager, which meets the requirements for the inner components, then add this container to the parent container, for example

JButton btn1 = ...;
JButton btn2 = ...;
JPanel buttons = new JPanel();
buttons.add(btn1);
buttons.add(btn2);

frame.add(buttons, BorderLayout.SOUTH);

Side notes:

  • Instead of frame.setSize(471, 298);, I'd recommend using JFrame#pack, but I'd only do this AFTER you've established the base UI.
  • frame.getContentPane().setLayout(null); is a bit pointless
  • frame.setVisible(true); should be called last, after you have established the UI, otherwise some components may not appear
  • Avoid using etMinimumSize, setPreferredSize, setMaximumSize, they can adversely affect the layouts if you're not careful. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more discussions. If you want to change the way a JScrollPane works, then use the Scrollable interface instead
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366