-2

I have a school project which requires creating a GUI. I am using flow-layout for the first time, and cannot figure out what I should use to create an area where I can enter text. None of the classes I used before work with flow-Layout. It needs to be editable, be able to go to a string. I just can't find a class that will work.

Here is my code as is:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*; 
public class RedAliance extends JFrame
{
    JRadioButton number;
    JRadioButton name;
    JRadioButton city;
    JRadioButton state;
    FlowLayout experimentLayout = new FlowLayout();
    final String numberString = "By numer";
    final String nameString = "By Name";
    final String cityString = "By city";
    final String stateString = "By State";
    JButton search = new JButton("search");

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
            try 
            {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } 
            catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }
            catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } 
            catch (InstantiationException ex) {
                ex.printStackTrace();
            } 
            catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        } 

    public RedAliance(String name) {
        super(name);
    }

    public void addComponentsToPane(final Container pane) {
        final JPanel p = new JPanel();
        p.setLayout(experimentLayout);
        experimentLayout.setAlignment(FlowLayout.TRAILING);
        JPanel controls = new JPanel();
        controls.setLayout(new FlowLayout());

        number = new JRadioButton(numberString);
        number.setActionCommand(numberString);
        number.setSelected(true);

         name = new JRadioButton(nameString);
        name.setActionCommand(nameString);

        city  = new JRadioButton(cityString);
        city.setActionCommand(cityString);

        state = new JRadioButton(stateString);
        state.setActionCommand(stateString);

        final ButtonGroup group = new ButtonGroup();
        group.add(number);
        group.add(name);
        group.add(city);
        group.add(state);

        controls.add(number);
        controls.add(name);
        controls.add(city);
        controls.add(state);
        controls.add(search);

        //Process the Apply component orientation button press
        search.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String command = group.getSelection().getActionCommand();
                String text= "";
                if (command.equals("By Name")) { byName(text);} 
                else if (command.equals("By Numer")) {
                }
                else if(command.equals("By State")){
                }
                else if(command.equals("By City")){
                }
                else {}
                //update the experiment layout
                p.validate();
                p.repaint();
            }

            });
        pane.add(p, BorderLayout.CENTER);
        pane.add(controls, BorderLayout.SOUTH); ;
    }
    private static void createAndShowGUI() {
        RedAliance frame = new RedAliance("red"); //Create window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }
    public void byName(String n)//returns the info on a team with this name
    {

    }
    public void byNumber(int n) //returns the info on a team with this number
    {

    }
    public void byCity(String c)//returns the info on all teams in this city
    {

    }
    public void byState(String s)//returns the info on all teams in this city
    {

    }


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • What exactly do you mean when you say "None of the classes I used before work with flow-Layout"? Did the size of your JTextField or other text component come out wrong? – PakkuDon May 22 '14 at 06:38
  • I have literally never heard of JTextFields before. The "resource" I have been given for this is an 11 year old book. How do you convert the entered text to a string? – user3395120 May 22 '14 at 06:43

1 Answers1

0

flow layout javadoc and sample code below should work.

A flow layout arranges components in a directional flow, much like lines of text in a paragraph. The flow direction is determined by the container's componentOrientation property and may be one of two values:

  • ComponentOrientation.LEFT_TO_RIGHT
  • ComponentOrientation.RIGHT_TO_LEFT

    import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class test2 extends JFrame
    {
        JRadioButton number;
        JRadioButton name;
        JRadioButton city;
        JRadioButton state;
        JTextField textField;
        FlowLayout experimentLayout = new FlowLayout();
        final String numberString = "By numer";
        final String nameString = "By Name";
        final String cityString = "By city";
        final String stateString = "By State";
        JButton search = new JButton("search");
    
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
                try 
                {
                    //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                } 
                catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } 
                catch (InstantiationException ex) {
                    ex.printStackTrace();
                } 
                catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                /* Turn off metal's use of bold fonts */
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        createAndShowGUI();
                    }
                });
            } 
    
        public test2(String name) {
            super(name);
        }
    
        public void addComponentsToPane(final Container pane) {
            final JPanel p = new JPanel();
            p.setLayout(experimentLayout);
            experimentLayout.setAlignment(FlowLayout.TRAILING);
            JPanel controls = new JPanel();
            controls.setLayout(new FlowLayout());
    
            number = new JRadioButton(numberString);
            number.setActionCommand(numberString);
            number.setSelected(true);
    
             name = new JRadioButton(nameString);
            name.setActionCommand(nameString);
    
            city  = new JRadioButton(cityString);
            city.setActionCommand(cityString);
    
            state = new JRadioButton(stateString);
            state.setActionCommand(stateString);
            textField = new JTextField(10);
            final ButtonGroup group = new ButtonGroup();
            group.add(number);
            group.add(name);
            group.add(city);
            group.add(state);
    
            controls.add(number);
            controls.add(name);
            controls.add(city);
            controls.add(state);
            controls.add(textField);
            controls.add(search);
    
    
            //Process the Apply component orientation button press
            search.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    String command = group.getSelection().getActionCommand();
                    String text= "";
                    if (command.equals("By Name")) { byName(text);} 
                    else if (command.equals("By Numer")) {
                    }
                    else if(command.equals("By State")){
                    }
                    else if(command.equals("By City")){
                    }
                    else {}
                    //update the experiment layout
                    p.validate();
                    p.repaint();
                }
    
                });
            pane.add(p, BorderLayout.CENTER);
            pane.add(controls, BorderLayout.SOUTH); ;
        }
        private static void createAndShowGUI() {
            test2 frame = new test2("red"); //Create window
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.addComponentsToPane(frame.getContentPane());
            frame.pack();
            frame.setVisible(true);
        }
        public void byName(String n)//returns the info on a team with this name
        {
    
        }
        public void byNumber(int n) //returns the info on a team with this number
        {
    
        }
        public void byCity(String c)//returns the info on all teams in this city
        {
    
        }
        public void byState(String s)//returns the info on all teams in this city
        {
    
        }
    
    
    }
    
Hirak
  • 3,601
  • 1
  • 22
  • 33