2
\$\begingroup\$

The following code creates a JFrame with a JTextField array in it. It also creates a JMenuBar at the top which allows you to select how many JTextFields you want to display (1-20) and then uses various layouts to display them on the JPanel.

I'm looking mainly for comments/thoughts on how the program lays out the JTextFields. Could this be done quicker or easier?

StitchSorts

import GUIMain.*;

public class StitchSorts
{   
    public static void main(String[] args)
    {
        StitchSorts sS = new StitchSorts();
        SortsGui.main(args);
    }
}

SortsGui

package GUIMain;

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

public class SortsGui
{
    JFrame myMainWindow = new JFrame("Sorts");

    JPanel sortPanel = new JPanel();

    public int nextTextBox = 10;
    JTextField[] allField = new JTextField [20];

    public void runGUI()
    {
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        MyMenuBar mbr = new MyMenuBar(this);

        myMainWindow.setJMenuBar(mbr);

        myMainWindow.getContentPane().add(sortPanel);

        myMainWindow.setVisible(true);

        myMainWindow.pack();

        myMainWindow.setMinimumSize(new Dimension(myMainWindow.getBounds().getSize()));
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("" , "[grow]");
        sortPanel.setLayout(layout);

        refreshScreen();
    }

    public void refreshScreen()
    {
        sortPanel.removeAll();

        MigLayout layout = new MigLayout("", "[grow]");
        if(nextTextBox<12)
        {
            layout = new MigLayout("", "[grow]");
        }
        else if(nextTextBox>11)
        {
            layout = new MigLayout("wrap 1", "[grow]");
        }

        sortPanel.setLayout(layout);

        for(int i = 0; i<nextTextBox;i++)
        {
            int fix = i+1;
            allField[i] = new JTextField("");

            if(nextTextBox<12)
            {
                sortPanel.add(allField[i],"growx, width 100:100:");
            }

            else if(nextTextBox==12)
            {
                sortPanel.add(allField[i],"growx, width 100:100:, split 6");
            }

            else if(nextTextBox==13)
            {
                if(fix<8)
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 7");
                }
                else
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 6");
                }
            }

            else if(nextTextBox==14)
            {
                sortPanel.add(allField[i],"growx, width 100:100:, split 7");
            }

            else if(nextTextBox==15)
            {
                if(fix<9)
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 8");
                }
                else
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 7");
                }
            }

            else if(nextTextBox==16)
            {
                sortPanel.add(allField[i],"growx, width 100:100:, split 8");
            }

            else if(nextTextBox==17)
            {
                if(fix<10)
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 9");
                }
                else
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 8");
                }
            }

            else if(nextTextBox==18)
            {
                sortPanel.add(allField[i],"growx, width 100:100:, split 9");
            }

            else if(nextTextBox==19)
            {
                if(fix<11)
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 10");
                }
                else
                {
                    sortPanel.add(allField[i],"growx, width 100:100:, split 9");
                }
            }

            else if(nextTextBox==20)
            {
                sortPanel.add(allField[i],"growx, width 100:100:, split 10");
            }
        }

        myMainWindow.setMinimumSize(new Dimension(0,0));
        myMainWindow.pack();
        Dimension dD = new Dimension(myMainWindow.getBounds().getSize());
        myMainWindow.setMinimumSize(dD);
        sortPanel.repaint();
        sortPanel.validate();
        myMainWindow.repaint();
        myMainWindow.validate();
    }

    public static void main(String[] args)
    {
        SortsGui sG = new SortsGui();
        sG.runGUI();
    }
}

MyMenuBar

package GUIMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.lang.*;

public class MyMenuBar extends JMenuBar
{
    int fontMetrics;
    FontMetrics fM;
    String lblSortsString = "Select amount of JTextFields to display";

    String sortsArray[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"};
    JComboBox sortComboBox = new JComboBox(sortsArray);

    JLabel lblSorts = new JLabel(lblSortsString, SwingConstants.CENTER);

    SortsGui sG;

    public MyMenuBar(SortsGui sG)
    {
        this.sG = sG;
        setBorderPainted(true);
        makePopUpMenu();
    }

    void makePopUpMenu()
    {
        fM = lblSorts.getFontMetrics(lblSorts.getFont());
        fontMetrics = fM.stringWidth(lblSortsString);
        lblSorts.setMinimumSize(new Dimension(fontMetrics+10,25));
        lblSorts.setPreferredSize(new Dimension(fontMetrics+10,25));
        lblSorts.setMaximumSize(new Dimension(fontMetrics+10,25));
        add(lblSorts);

        sortComboBox.setMinimumSize(new Dimension(60,25));
        sortComboBox.setPreferredSize(new Dimension(60,25));
        sortComboBox.setMaximumSize(new Dimension(60,25));
        sortComboBox.addItemListener(new sortComboBoxChanged());
        sortComboBox.setFocusable(false);
        add(sortComboBox);
        sortComboBox.setSelectedItem("10");
    }

    class sortComboBoxChanged implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent e)
        {
            String s = sortComboBox.getSelectedItem()+"";
            sG.nextTextBox = Integer.parseInt(s);
            sG.refreshScreen();
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

I'm looking mainly for comments/thoughts on how the program lays out the JTextFields. Could this be done quicker or easier?

If you're talking about the lots of if (nextTextBox == ?) and if (fix < ?), I have a few things in mind:

  1. Creating custom graphical layouts is often full of details with no general logic. Very often there is no escape from large blocks of code setting layout details, or complicated if-else branches

  2. On closer look, between nextTextBox==12 and nextTextBox==20, I see a pattern that can be generated using a formula. It might be worth rewriting it that way, especially if you intend to add more text boxes that would follow the same logic

  3. The common string part "growx, width 100:100:, split " should definitely be in a variable, rather than duplicated so many times

Naming issues

sS, sG, dD, fL are terrible names. Try to find better names that describe what these variables represent.

Instead of allField, allFields (plural) would be more natural for an array (also considering the "all" prefix).

Style issues

The placement of braces doesn't follow common Java practices. It's also recommended to put spaces around operators. For example, instead of this:

    for(int i = 0; i<nextTextBox;i++)
    {
        int fix = i+1;
        allField[i] = new JTextField("");

        if(nextTextBox<12)
        {
            sortPanel.add(allField[i],"growx, width 100:100:");
        }

The recommended writing style:

    for (int i = 0; i < nextTextBox; i++) {
        int fix = i + 1;
        allField[i] = new JTextField("");

        if (nextTextBox < 12) {
            sortPanel.add(allField[i], "growx, width 100:100:");
        }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.