0

Im trying to make a dynamic list of entries for a budget program. Initially there is supposed to be 10 items in this list and i want to give the user the option to add more items and have the panel scrollable. Every item is a panel that contains 1 label and 1 textbox.

My problem is i have the main panel's layout set to grid, and i have set the number of colums to 2 and the number of rows to 0, when adding an 11th item to this panel it creates a new row which is fine, except the panel's "view" widens when adding things to this panel.

I'd like to have the panel's "view" to stay the same size and be able to scroll down to see its other contents.

Heres my code:

private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    javax.swing.JPanel newCategoryPanel = new javax.swing.JPanel();
    javax.swing.JLabel newLabel = new javax.swing.JLabel();
    javax.swing.JTextField newTextField = new javax.swing.JTextField();

    newCategoryPanel.setLayout(new java.awt.GridLayout(1, 2));

    newLabel.setText("Poop :)"); //for testing
    newCategoryPanel.add(newLabel);

    newTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {

        }
    });
    newCategoryPanel.add(newTextField);
    panel1.add(newCategoryPanel);
    panel1.revalidate();

} 
user3712476
  • 118
  • 1
  • 11
  • Put the JPanel inside a JScrollPane, and set the layout of JPanel to be `GridLayout(0, 2)`. – Compass Nov 28 '14 at 20:02
  • 1. Consider using a `JTable`. 2. *Consider editing you previous [old question](http://stackoverflow.com/questions/27178150/creating-a-dynamic-scrollable-list-in-jframe) instead of making a new one.* – kiheru Nov 28 '14 at 20:05

1 Answers1

0

To have fixed panel inside scroll panel you should call setPreferredSize on your JScrollPane, to master a flexible layout manager try MigLayout it's easy and with it you can do all what you want, here is some code to start with:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame t = new JFrame();
                t.setBounds(100, 100, 500, 500);
                t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel global = new JPanel();
                global.setLayout(new MigLayout("wrap 1", "", ""));
                JButton add = new JButton("Add");

                JPanel listItems = new JPanel();
                listItems.setLayout(new MigLayout("wrap 2", "[]20[]", ""));

                add.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JLabel label = new JLabel("Item Label");
                        JTextField text = new JTextField("Value", 20);
                        listItems.add(label);
                        listItems.add(text);
                        global.validate();
                    }
                });

                global.add(add);
                JScrollPane scroll = new JScrollPane(listItems);
                scroll.setPreferredSize(new Dimension(400, 600));
                global.add(scroll);

                t.add(global);
                t.setVisible(true);
            }
        });
    }
}

and the result:

enter image description here

Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
  • [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513), for [example](http://stackoverflow.com/a/14011536/230513). – trashgod Nov 29 '14 at 03:11