javaswinglayout-managergrouplayout

Create a footer in GroupLayout


I have a Java Swing app. written. I want to add a footer to it like using a JPanel statusPanel. I know that this can be done in BorderLayout, but I want it in GroupLayout. Currently I'm using something like below:

JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setText("Status Bar");

And below is my full code

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import org.NXI.CreateNxi_Main;

import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JProgressBar;

@SuppressWarnings("serial")
public class DummyFrame extends JFrame {

    private JPanel contentPane;
    private JTextField srcTextField;
    private JTextField destTextField;
    private JButton btnNewButton;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DummyFrame frame = new DummyFrame();
                    frame.setResizable(false);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DummyFrame() {
        setTitle("DummyFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 604, 452);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.showOpenDialog(null);
                File f = fChoose.getSelectedFile();
                srcTextField.setText(f.getAbsolutePath());
            }
        });

        srcTextField = new JTextField();
        srcTextField.setColumns(10);

        destTextField = new JTextField();
        destTextField.setColumns(10);

        JButton destBtn = new JButton("Output Excel");
        destBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fChoose.setAcceptAllFileFilterUsed(false);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    destTextField.setText(fChoose.getSelectedFile().getAbsolutePath() + "\\");
                    System.out.println(fChoose.getCurrentDirectory());
                } else {
                    System.out.println("No Selection");
                }

            }
        });
        final JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        final JTextArea outputTextArea = new JTextArea();
        outputTextArea.setEditable(false);
        btnNewButton = new JButton("Convert");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CreateNxi_Main createNxi_Main = new CreateNxi_Main();
                try {
                    createNxi_Main.getInputAndOutputPaths(srcTextField.getText(), destTextField.getText() + "\\",
                            outputTextArea, progressBar);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }
        });
        JScrollPane scrollPane = new JScrollPane(outputTextArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(100, 100));
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setText("Developed by 0138039");

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
                                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(destTextField, Alignment.TRAILING,
                                                        GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                                        .addComponent(srcTextField, GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
                                .addGap(122))
                        .addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 543,
                                                Short.MAX_VALUE)
                                .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 543,
                                        Short.MAX_VALUE)
                                .addGroup(gl_contentPane.createSequentialGroup()
                                        .addPreferredGap(ComponentPlacement.RELATED, 446, Short.MAX_VALUE).addComponent(
                                                destBtn, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                        .addPreferredGap(ComponentPlacement.RELATED, 446, Short.MAX_VALUE).addComponent(
                                                srcBtn, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))))
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(46)
                                .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                        GroupLayout.PREFERRED_SIZE)
                                .addGap(36).addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                .addGap(218)))
                        .addGap(25)));
        gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane
                .createSequentialGroup().addGap(14)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(3).addComponent(srcTextField,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addComponent(srcBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE, false)
                        .addComponent(destTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                        .addComponent(destBtn, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(18).addComponent(btnNewButton,
                                GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
                        .addGroup(gl_contentPane.createSequentialGroup().addGap(31).addComponent(progressBar,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addGap(18).addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8)));
        contentPane.setLayout(gl_contentPane);
    }
}

I don't want to use a label, but instead use a StatusPanel. Please let me know if I can get this, if so, how can I?


Solution

  • There is no such thing as a StatusPanel in Swing. In any layout, the status line is usually just a narrow line at the bottom of the JFrame. A more exciting approach than a JLabel would be to roll your own StatusPanel by extending JPanel. This is how they do it in Sabacc-Online:

    public class JStatusPanel extends JPanel {
    
        protected JLabel message = new JLabel("Status");
    
        public JStatusPanel() {
            this.setLayout(new BorderLayout());
            message.setAlignmentY(JLabel.CENTER_ALIGNMENT);
            message.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            add(message, BorderLayout.CENTER);
        }
    
        public void showInfo(String text) {
            message.setForeground(new Color(0, 0, 0));
            message.setText(text);
        }
    
        public void showError(String text) {
            message.setForeground(new Color(255, 0, 0));
            message.setText(text);
        }
    
    }