Search code examples
javaswingawtjtextfield

JTextField Everything looks ok in Eclipse but won't compile


Can someone please tell me what's wrong with this code? I need it to pop up a frame and fill the frame with certain field a namer field and an intake field and also display a button at the bottom of the grid. Any help would be welcomed, Thanks!!

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

    public class FrameDemo 
    //To create the gui and show it.
    {   
    public static void createAndShowGUI()
    {
        //create and set up the window.
        JFrame frame = new JFrame("RungeKutta");
        GridLayout first = new GridLayout(1,14);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create, name and Populate TextField   
        JTextField PL = new JTextField("Pendulum Length", 20);
        //Set TextField to Uneditable. Each will have Empty Field Below For Variables   
        PL.setEditable(false);  
        //Set Textfield for user entered dat
        JTextField PLv = new JTextField();
        //Allow handler for user input on Empty Textfield?

        JTextField AD = new JTextField("Angular Displacement", 20);
        AD.setEditable(false);
            JTextField ADv = new JTextField();

        JTextField AV = new JTextField("Angular Velocity", 20);
        AV.setEditable(false);
        JTextField Avv = new JTextField();

        JTextField TS= new JTextField("Time Steps", 20);
        TS.setEditable(false);
        JTextField TSv = new JTextField();

        JTextField MT = new JTextField("Max Time", 20);
        MT.setEditable(false);
        JTextField MTv = new JTextField();

        JTextField V = new JTextField("Viscosity (0-1)", 20);
        V.setEditable(false);
        JTextField Vv = new JTextField();

        //Create Button to Restart
    JButton BNewGraph = new JButton("Draw New Graph"); //Button to restart entire drawing process
        JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(600,500));
    frame.getContentPane().add(PL, first);
    frame.getContentPane().add(PLv, first);
    frame.getContentPane().add(AD, first);
    frame.getContentPane().add(ADv, first);
    frame.getContentPane().add(AV, first);
    frame.getContentPane().add(Avv, first);
    frame.getContentPane().add(TS, first);
    frame.getContentPane().add(TSv, first);
    frame.getContentPane().add(MT, first);
    frame.getContentPane().add(MTv, first);
    frame.getContentPane().add(V, first);
    frame.getContentPane().add(Vv, first);
    frame.getContentPane().add(BNewGraph, first);
    //display the window
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        //add job to event scheduler
        //create and show GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
            }
    }

Solution

  • This is not how you use GridLayout:

    frame.getContentPane().add(PL, first);
    

    Instead you'd set the container's layout using the layout manager:

    frame.getContentPane().setLayout(first);
    

    and then add components to the container:

    frame.getContentPane().add(PL);
    frame.getContentPane().add(PLv);
    frame.getContentPane().add(AD);
    frame.getContentPane().add(ADv);
    frame.getContentPane().add(AV);
    frame.getContentPane().add(Avv);
    frame.getContentPane().add(TS);
    frame.getContentPane().add(TSv);
    frame.getContentPane().add(MT);
    frame.getContentPane().add(MTv);
    // and so on for all the components.
    

    You will want to read the tutorial on how to use GridLayout which you can find here: GridLayout Tutorial.

    As an aside, note that this:

    frame.getContentPane().add(PL);
    

    can be shortened to this:

    frame.add(PL);
    

    Also you will want to study and learn Java naming conventions: class names begin with an upper case letter and all method and variables with lower case letters. Also avoid variable names like pl or plv, or ad or adv, and instead use names that are a little bit longer and have meaning, names that make your code self-commenting. So instead of AD, consider angularDisplacementField for the JTextfield's name.


    Myself, I'd use a GridBagLayout and do something like:

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    import javax.swing.*;
    
    public class GuiDemo extends JPanel {
    
       // or better -- use an enum for this
       public static final String[] FIELD_LABELS = {
          "Pendulum Length", "Angular Displacement", "Angular Velocity",
          "Time Steps", "Max Time", "Viscocity (0-1)"
       };
       private static final int TEXT_FIELD_COLUMNS = 10;
       private static final int GAP = 3;
       private static final Insets RIGHT_GAP_INSETS = new Insets(GAP, GAP, GAP, 3 * GAP);
       private static final Insets BALANCED_INSETS = new Insets(GAP, GAP, GAP, GAP);
       private Map<String, JTextField> labelFieldMap = new HashMap<>();
    
       public GuiDemo() {
          JPanel labelFieldPanel = new JPanel(new GridBagLayout());
          int row = 0;
    
          // to make sure that no focusAccelerator is re-used
          Set<Character> focusAccelSet = new HashSet<>();
          for (String fieldLabelLText : FIELD_LABELS) {
             JLabel fieldLabel = new JLabel(fieldLabelLText);
             JTextField textField = new JTextField(TEXT_FIELD_COLUMNS);
             labelFieldPanel.add(fieldLabel, getGbc(row, 0));
             labelFieldPanel.add(textField, getGbc(row, 1));
             labelFieldMap.put(fieldLabelLText, textField);
    
             for (char c : fieldLabelLText.toCharArray()) {
                if (!focusAccelSet.contains(c)) {
                   textField.setFocusAccelerator(c);
                   fieldLabel.setDisplayedMnemonic(c);
                   focusAccelSet.add(c);
                   break;
                }
             }
    
             row++;
          }
          JButton button = new JButton(new DrawGraphAction("Draw New Graph"));
    
          labelFieldPanel.add(button, getGbc(row, 0, 2, 1));
    
          setLayout(new BorderLayout(GAP, GAP));
          add(labelFieldPanel, BorderLayout.CENTER);
       }
    
       private class DrawGraphAction extends AbstractAction {
          public DrawGraphAction(String name) {
             super(name);
             int mnemonic = (int) name.charAt(0);
             putValue(MNEMONIC_KEY, mnemonic);
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             // TODO calculation method
    
          }
       }
    
       public static GridBagConstraints getGbc(int row, int column) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = column;
          gbc.gridy = row;
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          if (column == 0) {
             gbc.anchor = GridBagConstraints.LINE_START;
             gbc.fill = GridBagConstraints.BOTH;
             gbc.insets = RIGHT_GAP_INSETS;
          } else {
             gbc.anchor = GridBagConstraints.LINE_END;
             gbc.fill = GridBagConstraints.HORIZONTAL;
             gbc.insets = BALANCED_INSETS;
          }
    
          return gbc;
       }
    
       public static GridBagConstraints getGbc(int row, int column, int width, int height) {
          GridBagConstraints gbc = getGbc(row, column);
          gbc.gridwidth = width;
          gbc.gridheight = height;
          gbc.insets = BALANCED_INSETS;
          gbc.fill = GridBagConstraints.BOTH;
          return gbc;
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Gui Demo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new GuiDemo());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }