0

I'm making a java program to turn any text into capital capital letters. It worked fine with system.out.print but now I want to turn it into a GUI. I tried getting the text from the original textField input, turn it into a double, turn the double into a string then I used addText but I don;t think it works. Here is how it is supposed to work: I write a bunch a text into a the input box then when I click the GO button it converts every character into capital letters. The output is supposed to to inside another TextField

here is my code (using Eclipse)

    import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Frame1 {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

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

    /**
     * Create the application.
     */
    public Frame1() {
        initialize();
    }
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 710, 508);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblInputTextTo = new JLabel("Input Text to Capitalise:");
        lblInputTextTo.setFont(new Font("Segoe UI", Font.BOLD, 26));
        lblInputTextTo.setBounds(210, 0, 289, 54);
        frame.getContentPane().add(lblInputTextTo);

        textField = new JTextField();
        textField.setBounds(34, 77, 624, 150);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(34, 354, 613, 90);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10); 

        JButton btnGo = new JButton("GO!");
        btnGo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double TextInput = Double.parseDouble(textField.getText());
                String Str = String.valueOf(TextInput);
                textField_1.setText(Str.toUpperCase());
                textField_1.setEditable(false);

            }
        });
        btnGo.setFont(new Font("Segoe UI", Font.BOLD, 18));
        btnGo.setBounds(250, 239, 180, 23);
        frame.getContentPane().add(btnGo);



        JLabel lblOutput = new JLabel("Output:");
        lblOutput.setFont(new Font("Segoe UI", Font.BOLD, 26));
        lblOutput.setBounds(304, 300, 95, 43);
        frame.getContentPane().add(lblOutput);
    }}

I'm not done yet if you have some suggestion it would be nice.

TaPaK
  • 1
  • 1
  • 1
  • "but I don;t think it works" -- please help us out and elaborate on this. **Why** don't you think it doesn't work? How is it not working? What is it doing? How have you tried to debug this? – Hovercraft Full Of Eels Feb 23 '19 at 03:51
  • Oh, you've got two JFrames in that code, one the class, that you're displaying, the other, the frame variable, that you're putting all your components into, that you're not displaying. Solution: use and create only one JFrame. – Hovercraft Full Of Eels Feb 23 '19 at 03:52
  • I don't understand what you mean that I have 2 Jframes. Are you talking about JtextField? – TaPaK Feb 23 '19 at 03:58
  • I'm mistaken, sorry. Again, please explain how your code is not working. Your question is sparse on detail. – Hovercraft Full Of Eels Feb 23 '19 at 04:00
  • 2
    Oh hell, you're trying to parse a String to a number -- why? The code isn't supposed to do this. Get rid of `double TextInput = Double.parseDouble(textField.getText());`, and instead simply get the text from textField. It's like you're trying to combine two programs into one. – Hovercraft Full Of Eels Feb 23 '19 at 04:02

1 Answers1

0

You don't need to convert the input String into double to make it uppercase. I did below change to your program and it works now.

btnGo.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    //Remove these 2 lines
    //double TextInput = Double.parseDouble(textField.getText());
    //String Str = String.valueOf(TextInput);

    //Directly take input text, make it uppercase and set it in output field.
    textField_1.setText(textField.getText().toUpperCase());
    textField_1.setEditable(false);

  }
});

Additional tip:
Don't do setLayout(null) and use setBounds() to set components in absolute positions. Instead use layout managers. See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16