skip to Main Content

i want save input that i get from user and save them in element .
i want to access elements that user write in my UI.
and if i want save the elements in array list which kind of array list i should build.

in my UI i have text field name and text field middle name and combo box city has got 3 city name and and a radio box that it depend sex.

in final show them in console what should i do ?

this all of my code:

package ui;
import java.awt.*;
import javax.swing.*;
public class UI extends JFrame
{
 public static void main(String[] args)
{
   JFrame frame = new JFrame();
   frame.setVisible(true);
   frame.setSize(500, 600);
   BorderLayout blayout = new BorderLayout();
   JButton center = new JButton();
   JButton north = new JButton();
   JButton south = new JButton();
   JComboBox combo = new JComboBox();
   combo.addItem("-");
   combo.addItem("Tehran");
   combo.addItem("Tabriz");
   combo.addItem("Shiraz");
   JRadioButton rb1 = new JRadioButton("man");
   JRadioButton rb2 = new JRadioButton("weman");
   frame.setLayout(blayout);
   FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
   center.setLayout(fLoyout);
   south.setLayout(fLoyout);
   JLabel jb1 = new JLabel("Name :");
   JTextField name = new JTextField(20);
   center.add(jb1);
   center.add(name);
   JLabel jb2 = new JLabel("Family :");
   JTextField family = new JTextField(20);
   center.add(jb2);
   center.add(family);
   JLabel jb4 = new JLabel("City :");
   center.add(jb4);
   center.add(combo);
   JLabel jb5 = new JLabel("Sex :");
   center.add(jb5);
   center.add(rb1);
   center.add(rb2);
   JLabel jb6 = new JLabel("Comment :");
   JTextField comment = new JTextField(50);
   JLabel jb7 = new JLabel("Save");
   south.add(jb7);
   JPanel cpanel = new JPanel();
   cpanel.add(center);
   JPanel spanel = new JPanel();
   spanel.add(south);
   cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
   cpanel.add(jb6);
   cpanel.add(comment);
   frame.add(cpanel,BorderLayout.CENTER);
   frame.add(spanel,BorderLayout.SOUTH);
}

}

2

Answers


  1. At first, you need to check your GUI, because from skimming it works not good. Later you can get data from the different component using an appropriate listener for each component or you can use any General button to reading data from all components and print it in a console or anywhere else.
    To get data from combo, for example, will be here:

    String data = combo.getSelectedIndex();
    

    More information you can get here:
    How can I get the user input in Java?

    Login or Signup to reply.
  2. You need to use listeners to create code that runs when ui component is pressed. I added listener to every component. try it:

    public class UI extends JFrame {
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(500, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BorderLayout blayout = new BorderLayout();
            JButton center = new JButton();
            JButton north = new JButton();
            JButton south = new JButton();
            south.addActionListener(e->{System.out.println("Save button is pressed");});
            JComboBox combo = new JComboBox();
            combo.addItem("-");
            combo.addItem("Tehran");
            combo.addItem("Tabriz");
            combo.addItem("Shiraz");
            combo.addActionListener(e -> {
                System.out.println(((JComboBox<String>) e.getSource()).getSelectedItem());
            });
            JRadioButton rb1 = new JRadioButton("man");
            rb1.addActionListener(e -> {
                System.out.println("man: " + ((JRadioButton) e.getSource()).isSelected());
            });
            JRadioButton rb2 = new JRadioButton("weman");
            rb2.addActionListener(e -> {
                System.out.println("weman: " + ((JRadioButton) e.getSource()).isSelected());
            });
            frame.setLayout(blayout);
            FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
            center.setLayout(fLoyout);
            south.setLayout(fLoyout);
            JLabel jb1 = new JLabel("Name :");
            JTextField name = new JTextField(20);
            center.add(jb1);
            center.add(name);
            name.addActionListener(e -> {
                System.out.println("name: " + ((JTextField) e.getSource()).getText());
            });
            JLabel jb2 = new JLabel("Family :");
            JTextField family = new JTextField(20);
            center.add(jb2);
            center.add(family);
            family.addActionListener(e -> {
                System.out.println("family: " + ((JTextField) e.getSource()).getText());
            });
            JLabel jb4 = new JLabel("City :");
            center.add(jb4);
            center.add(combo);
            JLabel jb5 = new JLabel("Sex :");
            center.add(jb5);
            center.add(rb1);
            center.add(rb2);
            JLabel jb6 = new JLabel("Comment :");
            JTextField comment = new JTextField(50);
            comment.addActionListener(e -> {
                System.out.println("comment: " + ((JTextField) e.getSource()).getText());
            });
            JLabel jb7 = new JLabel("Save");
            south.add(jb7);
            JPanel cpanel = new JPanel();
            cpanel.add(center);
            JPanel spanel = new JPanel();
            spanel.add(south);
            cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
            cpanel.add(jb6);
            cpanel.add(comment);
            frame.add(cpanel, BorderLayout.CENTER);
            frame.add(spanel, BorderLayout.SOUTH);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    You will have to put JRadioButton into RadioGroup to select one of them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search