0

I have 2 text areas with a button in between them in a frame in swing and I am using netbeans.

Clicking the button picks up an sql query from textArea1, using getText().

The input is processed (i.e. checked the spellings of the keywords after splitting the query) with SubmitData(). Inside that method, it only uses setText() to set the output to textArea2.

My Problem Is: The frame just doesn't stay or hold after I press the button.

Here is my code:

void createUI() throws Exception
{
    JFrame frame = new JFrame("JDBC All in One");

    // Layout of Main Window
    Container c = frame.getContentPane();
    c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

    textArea1 = new JTextArea(10, 50);
    textArea1.setBounds(10, 10, 30, 30);

    btnInsert = new JButton("Submit");
    btnInsert.setBounds(10, 10, 10, 10);
    btnInsert.addActionListener(this);

    textArea2 = new JTextArea(10, 50);
    textArea2.setBounds(10, 10, 30, 30);

    JPanel pnlInput1 = new JPanel();
    JPanel pnlInput2 = new JPanel();
    JPanel pnlInput3 = new JPanel();

    pnlInput1.add(textArea1);
    pnlInput3.add(btnInsert);
    pnlInput2.add(textArea2);

    frame.add(pnlInput1);
    frame.add(pnlInput3);
    frame.add(pnlInput2);

    frame.setSize(400, 500);
    frame.pack();
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent evt)

{
    String cmd = evt.getActionCommand();

    if (cmd.equals("Submit")) {
        try {
            SubmitData();
        } catch (Exception e) {}

    }
}

public static void SubmitData() throws Exception {

    s1 = textArea1.getText();

    String[] s2 = s1.split("\\s+");

    for (int i = 0; i < s2.length; i++) {
        if (s2[i] .equals("elect")|| s2[i] .equals("selct") || s2[i].equals("slect" )|| s2[i].equals("selec")|| s2[i].equals("seect")

        {
            textArea2.setText("use 'select' instead of " + s2[i]);
            System.exit(0);
        }
        if (s2[i] == "updat" || s2[i] == "updae" || s2[i] == "updte" || s2[i] == "upate") {
            textArea2.setText("use 'update' instead of " + s2[i]);
            System.exit(0);
        }

        if (s2[i] == "delet" || s2[i] == "delte" || s2[i] == "elete" || s2[i] == "dlete") {
            textArea2.setText("use 'delete' instead of " + s2[i]);
            System.exit(0);
        }
        if (s2[i] == "fro" || s2[i] == "frm" || s2[i] == "fom") {
            textArea2.setText("use 'from' instead of " + s2[i]);
            System.exit(0);
        }
    }
}

Edited-i've changed "==" fro string comparision with .equals() but the problem doesnt seem to go away.

eldo
  • 1,327
  • 2
  • 16
  • 27
sam
  • 3
  • 2
  • Welcome to StackOverflow! Here's a few tips to help you get answers: 1. If something doesn't work, post the stack trace, error log, whatever. If it's too long, it's okay to post relevant snippet to a paste service like [pastebin](http://pastebin.com/). Look at it from our perspective - it's really hard for us to figure out why your window closes when you hit the button if you don't show us your output! – Leo Izen Jul 16 '16 at 10:15
  • 2. Try to avoid seriously changing the content of questions when you edit them. If you edit for typoes/etc. that's good but in general if you change the content, it's polite to have a line at the bottom starting with "EDIT:" so people know what you changed. – Leo Izen Jul 16 '16 at 10:16
  • 3. When posting code, it's a good idea to post a [SSCCE](http://sscce.org/), which allows people to quickly test your code. It's very hard for us to make use of what you posted because it's very long AND it's not everything we need. If you need to post all the code, post a snippet, and then at the bottom post everything in full. – Leo Izen Jul 16 '16 at 10:17
  • 1
    Also: See [this question](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) :-) – Leo Izen Jul 16 '16 at 10:27
  • Also: See [this helpful guide on formatting](https://stackoverflow.com/help/formatting) – Leo Izen Jul 16 '16 at 10:28
  • Method names should NOT start with an upper case character!!! Do you see any method names in the API start with an upper case character? Follow conventions and don't make up your own! – camickr Jul 16 '16 at 13:59
  • Comparing Strings with `==` will not work. Use the `equals` method instead. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java . – VGR Jul 16 '16 at 14:32

1 Answers1

2

System.exit(0);terminates your JVM. Remove it if you want to keep your frame.

Edit for second question : After your first if, replace your if by else if in order that the others if don't execute when a condition is true.

for (int i = 0; i < s2.length; i++) {
    if (s2[i] .equals("elect")|| s2[i] .equals("selct") || s2[i].equals("slect" )|| s2[i].equals("selec")|| s2[i].equals("seect")
    {
        textArea2.setText("use 'select' instead of " + s2[i]);
    }
    else if (s2[i] == "updat" || s2[i] == "updae" || s2[i] == "updte" || s2[i] == "upate") {
        textArea2.setText("use 'update' instead of " + s2[i]);
    }

    else if (s2[i] == "delet" || s2[i] == "delte" || s2[i] == "elete" || s2[i] == "dlete") {
        textArea2.setText("use 'delete' instead of " + s2[i]);
    }
    else if (s2[i] == "fro" || s2[i] == "frm" || s2[i] == "fom") {
        textArea2.setText("use 'from' instead of " + s2[i]);
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • is there any alternate that can be used to stop further execution of code without disrupting the frame? – sam Jul 16 '16 at 14:23