Search code examples
javaarraysswingevent-handlingjfr

how to array,ascending,descending,bubble,inputarea,outputarea in JFrame


I don't know where to start I don't want to be spoon fed either. Help me with tons of problem.

My goal in this program is to get input from the user by letting them type in the input area and there area three button (ascending, array, bubble sort) must them to pick and then the output must show in outputarea.

my code only ends up in getting the input of the user in input area.

My problems are:

  1. Can I tell the user to input like this (1, 2, 3, 5, 6) with comma and neglected the comma then convert it to array to sort it.
  2. After they click the either three buttons how can i output it in the output area.
  3. Is my code in the right track or not? :D

Sorry for my bad english. i dont want to be spoonfeed just help me guys :D more power stackoverflow

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

public class JavaGui205 extends JPanel
{


    final JTextField inputarea,outputarea;
    final JButton asc,desc,bubble;
    int getsd;

  JavaGui205()
  {


      //initialize textfield and buttons
      inputarea=new JTextField("Inputarea",20);
      outputarea=new JTextField("Outputarea",20);
      asc=new JButton("Ascending");
      desc=new JButton("Descending");
      bubble=new JButton("BubbleSort");

      //adding function on fields
      inputarea.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent e)
          {
                  if(e.getSource()==inputarea)
            {
                String sd=e.getActionCommand();
                getsd=Integer.parseInt(sd);

            }
          }
      });

      //ascending function
      asc.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent e)
          {

          }
      });



      //adding to frame
      add(inputarea);
      add(asc);
      add(desc);
      add(bubble);
      add(outputarea);



  }

  public static void main(String[]args)
  {
      JFrame frame = new JFrame("WTF");
      frame.add(new JavaGui205());
      frame.setVisible(true);
      frame.setSize(300,150);

  }
}

I try some fixixation.I add these code tnx to mr Wyatt Lowery.but i have some problems how can i convert those string array into integer array then contain its values to use to three buttons then the product of those will display in the output area.Im sorry guys im slowpoke T_T :D i try my best to research but nothing happens

public void actionPerformed(ActionEvent e)
          {
                  if(e.getSource()==inputarea)
            {
                String sd=inputarea.getText();
                String[] inputArray=sd.split(",\\s*");                              
            }

          }

Solution

  • Your problems in order:

    1. Get the string from the text field inputarea.getText() and store it into a variable(E.g. inputText = inputarea.getText()). You can use the method split() to separate the values and put it into an array(E.g. String[] inputArray = inputText.split(", "))

    2. When the button is clicked, set the outputarea text equal to the array (E.g. outputarea.setText(inputArray.toString()))

    3. Try working on your coding conventions :-)