February 10, 2011

Simple sample to create a dynamic flash card layout in Swing

  • Solid white background
  • Two dynamically resizing text fields
  • Buttons have equal width
  • Buttons float to the top, no more weird grid-based spacing issues
  • Text areas are scrollable
  • Starts out small, but window can be adjusted to any reasonable size, even maximized
package migLayoutCenterExpandTest;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

public class Main {
    static JFrame      frame;

    static JPanel      firstPanel;
    static JPanel      secondPanel;

    static JLabel      firstPanelLabel;
    static JButton     firstPanelButton;

    static JTextArea   textArea1;
    static JTextArea   textArea2;
    static JScrollPane textScrollPane1;
    static JScrollPane textScrollPane2;
    static JButton     button1;
    static JButton     button2;
    static JButton     button3;
    static JButton     button4;
    static JButton     button5;
    static JButton     button6;

    /**
     * @param args
     */
    public static void main(String[] args) {
        createObjects();

        setUpFrame();
        setUpFirstPanel();
        setUpSecondPanel();

        frame.getContentPane().add(firstPanel);
        frame.setVisible(true);
    }

    public static void createObjects() {
        frame = new JFrame();

        firstPanel = new JPanel();

        firstPanelLabel = new JLabel("first label");
        firstPanelButton = new JButton("change screens");

        secondPanel = new JPanel();

        textArea1 = new JTextArea("Text area 1");
        textArea2 = new JTextArea("Text area 2");
        textScrollPane1 = new JScrollPane();
        textScrollPane2 = new JScrollPane();
        button1 = new JButton("Button one text");
        button2 = new JButton("Button two text");
        button3 = new JButton("Button three text");
        button4 = new JButton("Button four text");
        button5 = new JButton("Button five text");
        button6 = new JButton("Button six text");
    }

    public static void setUpFrame() {
        frame.setSize(400, 300);

        JPanel contentPane = (JPanel) frame.getContentPane();
        contentPane.setLayout(new MigLayout("align 50% 50%, filly"));
        contentPane.setBackground(Color.WHITE); // Disable this to debug panel sizes

        frame.addWindowListener(new ExitListener());
    }

    public static void setUpFirstPanel() {
        firstPanel.setLayout(new MigLayout("flowy"));
        firstPanel.setBackground(Color.WHITE);

        firstPanel.add(firstPanelLabel, "align 50%");
        firstPanel.add(firstPanelButton, "align 50%");

        firstPanelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container contentPane = frame.getContentPane();
                contentPane.removeAll(); // Required for new stuff to appear
                contentPane.add(secondPanel); // Required for new stuff to appear; this _is_ the new stuff, after all
                ((JPanel) contentPane).revalidate(); // Required for new stuff to appear
                contentPane.repaint(); // Without this, old stuff isn't removed before new stuff is painted and is still visible underneath
            }
        });
    }

    public static void setUpSecondPanel() {
        secondPanel.setLayout(new MigLayout("fill, flowy"));
        secondPanel.setBackground(Color.WHITE);

        secondPanel.setPreferredSize(new Dimension(1650, 1080));
        secondPanel.setMinimumSize(new Dimension(300, 200));
        secondPanel.setMaximumSize(new Dimension(1920, 1080));

        textScrollPane1 = new JScrollPane(textArea1);
        textScrollPane2 = new JScrollPane(textArea2);

        textScrollPane1.setBackground(Color.WHITE);
        textScrollPane2.setBackground(Color.WHITE);

        textScrollPane1.setBorder(BorderFactory.createTitledBorder("Text area 1"));
        textScrollPane2.setBorder(BorderFactory.createTitledBorder("Text area 2"));

        textScrollPane1.setPreferredSize(new Dimension(400, 300));
        textScrollPane2.setPreferredSize(new Dimension(400, 300));

        // cell col row [span x [span y]]
        secondPanel.add(textScrollPane1, "cell 0 0 1 1, push, grow");
        secondPanel.add(textScrollPane2, "cell 0 1 1 1, push, grow");
        secondPanel.add(button1, "cell 1 0 1 2, aligny top, growx");
        secondPanel.add(button2, "cell 1 0, growx");
        secondPanel.add(button3, "cell 1 0, growx");
        secondPanel.add(button4, "cell 1 0, growx");
        secondPanel.add(button5, "cell 1 0, growx");
        secondPanel.add(button6, "cell 1 0, growx");
    }
}

class ExitListener extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
        System.exit(0);
    }
}

February 4, 2011

Simple sample to replace the contents of a content pane in Swing


package swingTest;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class Main
{
static JFrame frame;

static JPanel firstPanel;
static JPanel secondPanel;

static JLabel firstPanelLabel;
static JButton firstPanelButton;

static JLabel secondPanelLabel;

/**
* @param args
*/
public static void main(String[] args)
{
createObjects();

firstPanel.add(firstPanelLabel);
firstPanel.add(firstPanelButton);

firstPanelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
Container contentPane = frame.getContentPane();
contentPane.remove(firstPanel); // Required for new stuff to appear
contentPane.add(secondPanel); // Required for new stuff to appear; this _is_ the new stuff, after all
((JPanel) contentPane).revalidate(); // Required for new stuff to appear
contentPane.repaint(); // Without this, old stuff isn't removed before new stuff is painted and is still visible underneath
}
});

secondPanel.add(secondPanelLabel);

frame.addWindowListener(new ExitListener());
frame.getContentPane().add(firstPanel);
frame.setVisible(true);
}

public static void createObjects()
{
frame = new JFrame();
frame.setSize(500, 500);

firstPanel = new JPanel();
firstPanel.setLayout(new FlowLayout());
firstPanelLabel = new JLabel("first label");
firstPanelButton = new JButton("first button");

secondPanel = new JPanel();
secondPanel.setLayout(new FlowLayout());
secondPanelLabel = new JLabel("second label");
}
}

class ExitListener extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}