Question

I am trying to write a program with JPanels and for the life of me, I can't seem to get the JPanels to go into the proper positions. I don't have a clue what I am doing wrong.

Here is some of the code I have so far:

package mainGUIWindowFrames;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CustomerWindow extends JFrame
{
//Attribute
private JTextField      textTF;
private JButton         copyButton;
private JLabel          copyLabel;
private Border          panelEdge;

//Constructor
public CustomerWindow()
{
    this.setBounds(100,100,800,600);
    panelEdge = BorderFactory.createEtchedBorder();

    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
    mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
    mainPanel.add(createSearchPanel(), BorderLayout.WEST);
}

//Operational Methods
public JPanel createCustomerPanel()
{
    JPanel customerPanel = new JPanel();

    JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
    customerL.setForeground(Color.blue);
    customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    customerPanel.add(customerL);


    customerPanel.setBorder(panelEdge);

    return customerPanel;
}

public JPanel createCustomerInfoPanel()
{
    JPanel infoPanel = new JPanel();
    infoPanel.setBorder(panelEdge);

    JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
    infoL.setForeground(Color.blue);
    infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    infoPanel.add(infoL);


    //add a text field
    textTF = new JTextField(50);
    infoPanel.add(textTF);

    //add a button
    copyButton = new JButton("Copy Text");
    copyButton.addActionListener(new ButtonListener());
    infoPanel.add(copyButton);

    copyLabel = new JLabel("-----------------");

    infoPanel.add(copyLabel);


    return infoPanel;
}

public JPanel createSearchPanel()
{
    JPanel lowerPanel = new JPanel();
    JLabel label = new JLabel("Text Transferred from JList:");

    //the spot where the data shows up

    lowerPanel.add(label);
    return lowerPanel;
}

The only Panel that shows up is the CreateCustomerPanel(). I have no idea what I need to do to get the other two panels to work.

If you could help me out that would be great!!


well I eventually wound up solving it by creating another panel and moving the panels I had out of the main constructor.

public CustomerWindow() {

    panelEdge = BorderFactory.createEtchedBorder();




}
public  JPanel createNorthPanel()
{
    JPanel customerPanel = new JPanel();
    JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
    customerL.setForeground(Color.blue);
    customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    customerPanel.add(customerL);


    customerPanel.setBorder(panelEdge);

    return customerPanel;

}   
//Operational Methods
public JPanel createCustomerPanel()
{
    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.add(createNorthPanel(), BorderLayout.NORTH);
    mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
    mainPanel.add(createSearchPanel(), BorderLayout.WEST);

    return mainPanel;


}


public JPanel createCustomerInfoPanel()
{
    JPanel infoPanel = new JPanel();
    Box vBox = Box.createVerticalBox();
    infoPanel.setBorder(panelEdge);


    JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
    infoL.setForeground(Color.blue);
    infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    infoPanel.add(infoL);


    //add a text field
    clientId = new JTextField(10);
    vBox.add(clientId);
    vBox.add(Box.createVerticalStrut(25));
    fName = new JTextField(10);
    vBox.add(fName);
    vBox.add(Box.createVerticalStrut(25));
    lName = new JTextField(10);
    vBox.add(lName);
    vBox.add(Box.createVerticalStrut(25));
    address = new JTextField(10);
    vBox.add(address);
    vBox.add(Box.createVerticalStrut(25));
    postalCode = new JTextField(10);
    vBox.add(postalCode);
    vBox.add(Box.createVerticalStrut(25));
    number = new JTextField(10);
    vBox.add(number);
    vBox.add(Box.createVerticalStrut(25));
    type = new JTextField(10);
    vBox.add(type);


    infoPanel.add(vBox);


    return infoPanel;

Ive still got a lot of work to do but thanks to all those who helped me out!!

Was it helpful?

Solution

Based on your example, nothing should show up, as you've not added mainPanel to anything.

Once I did that, I was able to get

Clients

to show up...

Modified code example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class CustomerWindow extends JFrame {
//Attribute

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                CustomerWindow frame = new CustomerWindow();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private JTextField textTF;
    private JButton copyButton;
    private JLabel copyLabel;
    private Border panelEdge;

//Constructor
    public CustomerWindow() {
        this.setBounds(100, 100, 800, 600);
        panelEdge = BorderFactory.createEtchedBorder();

        JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
        mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
        mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
        mainPanel.add(createSearchPanel(), BorderLayout.WEST);

        add(mainPanel);
    }

//Operational Methods
    public JPanel createCustomerPanel() {
        JPanel customerPanel = new JPanel();

        JLabel customerL = new JLabel("Clients", SwingConstants.CENTER);
        customerL.setForeground(Color.blue);
        customerL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
        customerPanel.add(customerL);

        customerPanel.setBorder(panelEdge);

        return customerPanel;
    }

    public JPanel createCustomerInfoPanel() {
        JPanel infoPanel = new JPanel();
        infoPanel.setBorder(panelEdge);

        JLabel infoL = new JLabel("Clients", SwingConstants.CENTER);
        infoL.setForeground(Color.blue);
        infoL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
        infoPanel.add(infoL);

        //add a text field
        textTF = new JTextField(50);
        infoPanel.add(textTF);

        //add a button
        copyButton = new JButton("Copy Text");
//        copyButton.addActionListener(new ButtonListener());
        infoPanel.add(copyButton);

        copyLabel = new JLabel("-----------------");

        infoPanel.add(copyLabel);

        return infoPanel;
    }

    public JPanel createSearchPanel() {
        JPanel lowerPanel = new JPanel();
        JLabel label = new JLabel("Text Transferred from JList:");

        //the spot where the data shows up
        lowerPanel.add(label);
        return lowerPanel;

    }

}

OTHER TIPS

You didn't add the mainpanel in the constructor.

add(mainPanel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top