sam sam - 4 months ago 10
Java Question

Swing frame doesnt hold or stay after clicking jbutton

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.

Answer

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]);
    }
}
Comments