Question

I am creating a program which has three jpanels: one container, and inside the container is two jpanels, one that is going to hold buttons and one which will hold the content. Ive got them both showing so far and its looking good, the only problem i I was hoping to add some space or a border between or the two (or around the button menu if possible) however since both internal panels are set to null layouts and the external layout is set to a border layout I cannot seem to add a border between the two internal ones. Here is my code so far:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class internal_test extends JFrame {

int height = 480;
int width = 640;

public internal_test() {
    initUI();
}

private void initUI() {

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.setBackground(Color.black);

    JPanel buttonMenu = new JPanel();
    buttonMenu.setLayout(null);
    buttonMenu.setBackground(Color.DARK_GRAY);
    buttonMenu.setPreferredSize(new Dimension(150, height));

    JPanel dragFrame = new JPanel();
    dragFrame.setLayout(null);
    dragFrame.setPreferredSize(new Dimension(200, 100));
    dragFrame.setSize(new Dimension(490, height));
    dragFrame.setBackground(Color.gray);

    container.add(buttonMenu, BorderLayout.WEST);
    container.add(dragFrame, BorderLayout.CENTER);

    // container.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

    add(container);
    pack();

    setTitle("internal_test V0.1");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(width, height);
    setLocationRelativeTo(null);
}

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            internal_test c = new internal_test();

            c.setVisible(true);

        }
    });
}

}

The dragFrame is going to be a DragLayout since that layout does what I need it to, but the button menu could technically be any layout, as long as it would allow me to place buttons/other items in a list with a label next to each.

Any help is greatly appreciated.

Was it helpful?

Solution

This should help you add the type of border you want: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html

You can start with a red line border like this: buttonMenu.setBorder(BorderFactory.createLineBorder(Color.red));

OTHER TIPS

I would use the BoxLayout and for the spacing use

panel.add(Box.createRigidArea(new Dimension(x, y)));

Here are some decent examples.

I Suggest GridBagLayout

beacause it is more easy to intent spaces between the components

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top