-1
public class Sort_BenchMark extends JFrame {

   private JPanel contentPane;
   private JTextField textField;
   private JButton btnBubbleSort;
   private JLabel label_1;
   private JButton btnGenerate;
   private JButton btnSelectionSort;
   private JLabel lblSs;
   private JLabel lblStatus;

   /**
    * Launch the application.
    */
   public static void main(String[] args) 
   {
     EventQueue.invokeLater(new Runnable() 
     {
        public void run() 
        {
            try 
              {
                Sort_BenchMark frame = new Sort_BenchMark();
                frame.setVisible(true);
              }
              catch (Exception e) 
              {
                e.printStackTrace();
              }
           }
       });
   }

/**
 * Create the frame.
 */
public Sort_BenchMark() 
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textField = new JTextField("Enter ");
    textField.setForeground(Color.GRAY);
    textField.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void focusGained(FocusEvent e) {
            textField.setText("");
            textField.setForeground(Color.BLACK);
        }
    });
    textField.setBounds(29, 30, 139, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    label_1 = new JLabel("");
    label_1.setBounds(334, 20, 120, 30);
    contentPane.add(label_1);

    btnBubbleSort = new JButton("Bubble Sort");

    btnBubbleSort.setBounds(204, 20, 120, 30);
    contentPane.add(btnBubbleSort);

    btnSelectionSort = new JButton("Selection Sort");
    btnSelectionSort.setBounds(204, 70, 120, 30);
    contentPane.add(btnSelectionSort);

    lblSs = new JLabel("");
    lblSs.setBounds(334, 70, 120, 30);
    contentPane.add(lblSs);

    lblStatus = new JLabel("");
    lblStatus.setBounds(75, 87, 93, 23);
    contentPane.add(lblStatus);

    final JRadioButton rdbtnAvgCase = new JRadioButton("Avg Case");
    rdbtnAvgCase.setBounds(29, 150, 109, 23);
    contentPane.add(rdbtnAvgCase);

    ButtonGroup b = new ButtonGroup();
    b.add(rdbtnAvgCase);

    btnGenerate = new JButton("Generate");
    btnGenerate.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {
            btnBubbleSort.setEnabled(true);
            btnSelectionSort.setEnabled(true);
            final String s = textField.getText();
            if(s.contentEquals(""))
            {
                lblStatus.setText("Enter length");
            }
            else
            {
                lblStatus.setText("Ready");
                if(rdbtnAvgCase.isSelected())
                {
                    btnBubbleSort.addActionListener(new ActionListener() 
                    {
                        public void actionPerformed(ActionEvent e) 
                        {
                            Thread t1 = new Thread(new Runnable() 
                            {
                                @Override
                                public void run() 
                                {
                                    btnBubbleSort.setEnabled(false);
                                    label_1.setText("done");

                                    btnBubbleSort.setEnabled(true);
                                }
                            });
                            t1.start();
                        }
                    });

                    btnSelectionSort.addActionListener(new ActionListener() 
                    {
                        public void actionPerformed(ActionEvent e) 
                        {
                            Thread t3 = new Thread(new Runnable() 
                            {
                                @Override
                                public void run() 
                                {
                                    btnSelectionSort.setEnabled(false);
                                    lblSs.setText("done");
                                    btnSelectionSort.setEnabled(true);
                                }
                            });
                            t3.start();
                        }
                    });
                }
            }
        }
    });
    btnGenerate.setBounds(64, 62, 88, 25);
    contentPane.add(btnGenerate);       
  }
}

The above code is about Swing. The actual code how i designed is:-(In the frame)

  1. Select the Average Case (RadioButton)
  2. Enter any number in textfield (Enter)
  3. click on generate
  4. click on any sort button(Bubble Sort and Selection Sort)

Now, whats the problem is, If I click on BubbleSort the text field gets cleared. But it should not happen as per I designed. Can anyone suggest me the solution so that text field wont get clear after entered anything in it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Android-rocks
  • 89
  • 2
  • 9
  • Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Sep 05 '14 at 17:20
  • Can u alter the code and give me the solution??? – Android-rocks Sep 05 '14 at 17:21
  • 1
    Can you pay me? As an aside, a person who cannot be bothered to spell all 3 letters of the word 'you', or indicates a question with '???' is unlikely to get help from me. – Andrew Thompson Sep 05 '14 at 17:23
  • Oh Sorry, Actually i was worrying about code. – Android-rocks Sep 05 '14 at 17:26
  • Can you alter the code and give me the solution? Please... – Android-rocks Sep 05 '14 at 17:26

2 Answers2

0

These lines here:

  @Override
    public void focusGained(FocusEvent e) {
        textField.setText(""); //HERE
        textField.setForeground(Color.BLACK);
    }

in the focus listener code says that when you click in the textfield then set its text to a empty string.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

Firstly, horrible nested ActionPerformed you've got there.

That aside, Vincent Ramdhanie is right as to where the problem is originating. The reason why it only happens when you click a certain button, is because when you disable a button, then it cannot have focus, which forces the focus to be on something else, which in the disable-btnBubbleSort's case, appears to be your textfield.

Instead of btnSelectionSort.setEnabled(false) and btnSelectionSort.setEnabled(true), try using setVisible(false) and setVisible(true).

If that doesn't work, drop the onfocus-part, and do something with a mouse-click event instead.

Ultroman the Tacoman
  • 642
  • 1
  • 10
  • 16