0

I am creating a program that will have a MainFrame that contains a panel along the top containing two buttons side by side (kind of like a very unique toolbar) and then below will be the main panel (flipPanel) area to the left and another panel to the right which will be a form (flipFormPanel). flipPanel is inside a scroll pane, named scrollPane, and the scrollPane is in the left area inside MainFrame. Items from the form will be passed into a new panel called itemPanel that will then be placed inside flipPanel each time the OK button is pressed. I want the itemPanel to span the width of flipPanel, but to only be about 100 pixels high.

I am having trouble getting the scrollPane to fill the left area of MainFrame. After scrollPane fills this area, I need flipPanel to fill scrollPane, and then have the itemPanels be added with the full width of scrollPane but only be 100 pixels high.

The form, flipFormPanel, is also not sizing as I would like. It needs to be 250 pixels wide and have the height from the bottom of the toolbar to the bottom of MainFrame.

I am unsure of why the scrollPane and flipFormPanel are not resizing properly in MainFrame and was wondering if you guys could help me out. I am new to Swing and am still learning, so if you have any pointers please don't hold them back.

Here's an image of how I would like the final positioning to be in case I did not explain it well enough: https://www.dropbox.com/s/nrqy7bsaabyqip5/FlipProgramLayout.png?dl=0

Here is MainFrame:

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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


public class MainFrame extends JFrame {

private ToolBar toolBar;
private FlipFormPanel flipFormPanel;
private LogFormPanel logFormPanel;
private FlipPanel flipPanel;
private LogPanel logPanel;
private MenuBarPanel menuBar;

private JPanel panelCards;
private JPanel flipCard;
private JPanel logCard;
private JPanel formCards;
private JPanel flipFormCard;
private JPanel logFormCard;
final static String FPANEL = "Flip Panel";
final static String FFORM = "Flip Form";
final static String LPANEL = "Log Panel";
final static String LFORM = "Log Form";


public MainFrame() {
    super("Flipping Log");





    setSize(960, 540);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    toolBar = new ToolBar();
    flipFormPanel = new FlipFormPanel();
    logFormPanel = new LogFormPanel();
    flipPanel = new FlipPanel();
    flipPanel.setPreferredSize(new Dimension(500, 500));
    logPanel = new LogPanel();
    logPanel.setPreferredSize(new Dimension(500, 500));

    createCards();
    layoutComponents();



    // ToolBar to Switch Panels
    toolBar.choosePanel(new PanelListener() {
        public void panelChosen(String panel) {
            if(panel.equals("Current Flips")) {
                showCurrentFlips();
            }
            else if(panel.equals("Flipping Log")) {
                showFlippingLog();
            }
        }
    });

    // Flip Form Panel
    flipFormPanel.setFlipFormListener(new FlipFormListener() {
        public void flipFormEventOccurred(FlipFormEvent e) {
            String item = e.getItem();
            String buyPrice = e.getBuyPrice();
            String sellPrice = e.getSellPrice();
            String quantity = e.getQuantity();
            String pcBuyPrice = e.getPcBuyPrice();
            String pcSellPrice = e.getPcSellPrice();

            flipPanel.addItemPanel(item, buyPrice, sellPrice, quantity,
                    pcBuyPrice, pcSellPrice);
            flipPanel.revalidate();
            flipPanel.repaint();
        }
    });

}

public void showCurrentFlips() {
    CardLayout c1 = (CardLayout)panelCards.getLayout();
    CardLayout c2 = (CardLayout)formCards.getLayout();
    c1.show(panelCards, "Flip Panel");
    c2.show(formCards, "Flip Form");
}

public void showFlippingLog() {
    CardLayout c1 = (CardLayout)panelCards.getLayout();
    CardLayout c2 = (CardLayout)formCards.getLayout();
    c1.show(panelCards, "Log Panel");
    c2.show(formCards, "Log Form");
}

public void createCards(){      
    // Create the panel that contains the cards
    panelCards = new JPanel(new CardLayout());
    panelCards.add(flipPanel, "Flip Panel");
    panelCards.add(logPanel, "Log Panel");
    Dimension dim = new Dimension();
    dim = panelCards.getPreferredSize();
    dim.width = 500;
    dim.height = 500;
    panelCards.setPreferredSize(dim);

    formCards = new JPanel(new CardLayout());
    formCards.add(flipFormPanel, "Flip Form");
    formCards.add(logFormPanel, "Log Form");
    dim = formCards.getPreferredSize();
    dim.width = 500;
    dim.height = 500;
    formCards.setPreferredSize(dim);
}

public void layoutComponents(){
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    gc.fill = GridBagConstraints.NONE;

    // First Row
    gc.gridy = 0;
    gc.weightx = 3.0;
    gc.weighty = 0;

    gc.gridwidth = 2;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(toolBar, gc);

    // Second Row
    gc.gridy = 1;
    gc.weighty = 2.0;
    gc.gridwidth = 1;

    gc.gridx = 0;
    gc.weightx = 1.0;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(0, 0, 0, 0);
    add(panelCards, gc);

    gc.gridx = 1;
    gc.weightx = 0;
    gc.anchor = GridBagConstraints.NORTHEAST;
    gc.insets = new Insets(40, 0, 0, 0);
    add(formCards, gc);
}


}

Here is FlipFormPanel:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FlipFormPanel extends JPanel {

private JComboBox itemCombo;
private JFormattedTextField buyPriceField;
private JFormattedTextField sellPriceField;
private JFormattedTextField quantityField;
private JFormattedTextField pcBuyPriceField;
private JFormattedTextField pcSellPriceField;

private JLabel itemLabel;
private JLabel buyPriceLabel;
private JLabel sellPriceLabel;
private JLabel quantityLabel;
private JLabel pcBuyPriceLabel;
private JLabel pcSellPriceLabel;

private JButton okBtn;
private FlipFormListener flipFormListener;

public FlipFormPanel() {

    Dimension dim = getPreferredSize();
    dim.width = 250;
    dim.height = 500;
    setPreferredSize(dim);

    okBtn = new JButton("OK");

    setupLabels();
    setupComboBox();
    createFormat();
    createFormattedTextFields();
    setupOkButton();

    layoutComponents();
}

public void setupOkButton() {
    okBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String item = (String) itemCombo.getSelectedItem();
            String buyPrice = buyPriceField.getText();
            String sellPrice = sellPriceField.getText();
            String quantity = quantityField.getText();
            String pcBuyPrice = pcBuyPriceField.getText();
            String pcSellPrice = pcSellPriceField.getText();

            FlipFormEvent ev = new FlipFormEvent(this, item, buyPrice,
                    sellPrice, quantity, pcBuyPrice, pcSellPrice);

            if (flipFormListener != null) {
                flipFormListener.flipFormEventOccurred(ev);
            }

        }
    });
}

public void setFlipFormListener(FlipFormListener listener) {
    this.flipFormListener = listener;
}

public void setupLabels() {
    itemLabel = new JLabel("Item: ");
    buyPriceLabel = new JLabel("Buy Price: ");
    sellPriceLabel = new JLabel("Sell Price: ");
    quantityLabel = new JLabel("Quantity: ");
    pcBuyPriceLabel = new JLabel("PC Buy Price: ");
    pcSellPriceLabel = new JLabel("PC Sell Price: ");
}

public void setupComboBox() {
    itemCombo = new JComboBox();
    DefaultComboBoxModel itemModel = new DefaultComboBoxModel();
    itemModel.addElement("Bandos Helmet");
    itemModel.addElement("Bandos Chestplate");
    itemModel.addElement("Bandos Tassets");
    itemModel.addElement("Bandos Gloves");
    itemModel.addElement("Bandos Boots");
    itemModel.addElement("Bandos Warshield");
    itemModel.addElement("Armadyl Helmet");
    itemModel.addElement("Armadyl Chestplate");
    itemModel.addElement("Armadyl Chainskirt");
    itemModel.addElement("Armadyl Gloves");
    itemModel.addElement("Armadyl Boots");
    itemModel.addElement("Armadyl Crossbow");
    itemModel.addElement("Armadyl Buckler");
    itemModel.addElement("Hood of Subjugation");
    itemModel.addElement("Garb of Subjugation");
    itemModel.addElement("Gown of Subjugation");
    itemModel.addElement("Gloves of Subjugation");
    itemModel.addElement("Boots of Subjugation");
    itemModel.addElement("Ward of Subjugation");
    itemCombo.setModel(itemModel);
    itemCombo.setSelectedIndex(-1);
    itemCombo.setEditable(false);
}

private DecimalFormat priceFormat;
private DecimalFormat quantityFormat;
public void createFormat() {
    priceFormat = new DecimalFormat("###,##0.###k");
    quantityFormat = new DecimalFormat("###,###");
}

public void createFormattedTextFields() {
    buyPriceField = new JFormattedTextField(priceFormat);
    buyPriceField.setColumns(10);
    sellPriceField = new JFormattedTextField(priceFormat);
    sellPriceField.setColumns(10);
    quantityField = new JFormattedTextField(quantityFormat);
    quantityField.setColumns(10);
    pcBuyPriceField = new JFormattedTextField(priceFormat);
    pcBuyPriceField.setColumns(10);
    pcSellPriceField = new JFormattedTextField(priceFormat);
    pcSellPriceField.setColumns(10);
}

public void layoutComponents() {

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    gc.fill = GridBagConstraints.NONE;

    // First Row
    gc.gridy = 0;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 10, 3);
    add(itemLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 10, 3);
    add(itemCombo, gc);

    // Second Row
    gc.gridy = 1;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 3, 3);
    add(buyPriceLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(buyPriceField, gc);

    // Third Row
    gc.gridy = 2;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 3, 3);
    add(sellPriceLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(sellPriceField, gc);

    // Fourth Row
    gc.gridy = 3;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 3, 3);
    add(quantityLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(quantityField, gc);

    // Fifth Row
    gc.gridy = 4;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 3, 3);
    add(pcBuyPriceLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(pcBuyPriceField, gc);

    // Sixth Row
    gc.gridy = 5;
    gc.weighty = 1;
    gc.gridwidth = 1;

    gc.weightx = 1;
    gc.gridx = 0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = new Insets(3, 3, 3, 3);
    add(pcSellPriceLabel, gc);

    gc.weightx = 1;
    gc.gridx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(pcSellPriceField, gc);

    // Seventh Row
    gc.gridy = 6;
    gc.weighty = 1;
    gc.gridwidth = 2;

    gc.weightx = 1;
    gc.anchor = GridBagConstraints.CENTER;
    gc.insets = new Insets(3, 3, 3, 3);
    add(okBtn, gc);

}

}

Here is FlipPanel:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class FlipPanel extends JPanel {

private JPanel mainPanel;
private JScrollPane scrollPane;
private ItemPanel itemPanel;
private JLabel lbl;

public FlipPanel() {

    Dimension dim = getPreferredSize();
    dim.width = 500;
    dim.height = 500;
    setPreferredSize(dim);
    setLayout(new BorderLayout());



    // lbl = new JLabel("Flip");
    // add(lbl);

    mainPanel = new JPanel();
    JScrollPane scrollPane = new JScrollPane(mainPanel);
    dim = scrollPane.getPreferredSize();
    dim.width = 500;
    dim.height = 500;
    scrollPane.setPreferredSize(dim);
    add(scrollPane, BorderLayout.CENTER);
    mainPanel.setLayout(new GridLayout(0, 1));

    ItemPanel firstItemPanel = new ItemPanel();
    dim = firstItemPanel.getPreferredSize();
    dim.width = 500;
    dim.height = 100;
    firstItemPanel.setPreferredSize(dim);

    mainPanel.add(firstItemPanel);



}

public void addItemPanel(String item, String buyPrice, String sellPrice,
        String quantity, String pcBuyPrice, String pcSellPrice) {
    this.itemPanel = new ItemPanel(item, buyPrice, sellPrice, quantity,
            pcBuyPrice, pcSellPrice);
    mainPanel.add(itemPanel);

    Dimension dim = this.itemPanel.getPreferredSize();
    dim.width = 500;
    dim.height = 100;
    this.itemPanel.setPreferredSize(dim);
}

public void addItemPanel() {
    this.itemPanel = new ItemPanel();
    mainPanel.add(itemPanel);
}

}

Here is ItemPanel:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class ItemPanel extends JPanel {

private JTextArea textArea;
private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;

private JLabel itemLabel;
private JLabel buyPriceLabel;
private JLabel sellPriceLabel;
private JLabel quantityLabel;
private JLabel pcBuyPriceLabel;
private JLabel pcSellPriceLabel;


public ItemPanel() {
    setLayout(new FlowLayout());

    Dimension dim = getPreferredSize();
    dim.height = 100;
    dim.width = 500;
    setPreferredSize(dim);

    itemLabel = new JLabel("ITEM: ");
    buyPriceLabel = new JLabel("BUY PRICE: ");
    sellPriceLabel = new JLabel("SELL PRICE: ");
    quantityLabel = new JLabel("QUANTITY: ");
    pcBuyPriceLabel = new JLabel("PC BUY PRICE: ");
    pcSellPriceLabel = new JLabel("PC SELL PRICE: ");

    add(itemLabel);
    add(buyPriceLabel);
    add(sellPriceLabel);
    add(quantityLabel);
    add(pcBuyPriceLabel);
    add(pcSellPriceLabel);
}

public ItemPanel(String item, String buyPrice,
        String sellPrice, String quantity, String pcBuyPrice,
        String pcSellPrice) {

    this.item = item;
    this.buyPrice = buyPrice;
    this.sellPrice = sellPrice;
    this.quantity = quantity;
    this.pcBuyPrice = pcBuyPrice;
    this.pcSellPrice = pcSellPrice;

    setLayout(new BorderLayout());

    Dimension dim = getPreferredSize();
    dim.height = 100;
    dim.width = 100;
    setPreferredSize(dim);

    itemLabel = new JLabel("ITEM: ");
    buyPriceLabel = new JLabel("BUY PRICE: ");
    sellPriceLabel = new JLabel("SELL PRICE: ");
    quantityLabel = new JLabel("QUANTITY: ");
    pcBuyPriceLabel = new JLabel("PC BUY PRICE: ");
    pcSellPriceLabel = new JLabel("PC SELL PRICE: ");

    add(itemLabel);
    add(buyPriceLabel);
    add(sellPriceLabel);
    add(quantityLabel);
    add(pcBuyPriceLabel);
    add(pcSellPriceLabel);
}

}

Thank you so much.

1 Answers1

1

You need to use a combination of size hints and layout managers.

The following sets each panels preferred and minimum size to 100, but uses a GridBagLayout to allow the FlipPanel to expand beyond these base constraints...

Layout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(new TestPane()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private GridBagConstraints gbc;

        public TestPane() {
            setLayout(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(createPanel("I be the flip panel"), gbc);
            gbc.weighty = 0;

            add(createPanel("Item Pane 01"), gbc, getComponentCount() - 1);
            add(createPanel("Item Pane 02"), gbc, getComponentCount() - 1);
            add(createPanel("Item Pane 03"), gbc, getComponentCount() - 1);
        }

        protected JPanel createPanel(String text) {
            return new SubPanel(text);
        }

        public class SubPanel extends JPanel {

            public SubPanel(String text) {
                setLayout(new BorderLayout());
                add(new JLabel(text));
                setBorder(new LineBorder(Color.RED));
            }

            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                size.height = 100;
                return size;
            }

            @Override
            public Dimension getMinimumSize() {
                return getPreferredSize();
            }

        }

    }

}

See Laying Out Components Within a Container and How to Use GridBagLayout for more details

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • My error was in needing to use gc.fill = GridBagConstraints.BOTH for the Flip Panel and gc.fill = GridBagConstraints.NONE for the Flip Form Panel, but your answer helped a lot, thank you. – Hunter Corry Dec 01 '14 at 04:33