0

I am creating a GUI for a project. I have a small problem. How does one implement the reset option? I have created a button at the bottom. And when I click the button, I should be able to move up the code. I tried using a label. But I guess labels used in the main code cannot be used within the actionPerformed() method of the reset button. Is there any alternative?

EDIT - I'm still working on the code. But here is the part of the code that I'm having trouble with. I've mentioned the label as --LABEL--

import java.sql.*;

import javax.swing.*;

import java.awt.Component;
import java.awt.event.*;

public class Console
{
public static String v1;
public static String v2;
Connection conn;
Statement stmt;
ResultSet rs;

JFrame f = new JFrame("Automation Testing Tool");

JPanel p1 = new JPanel();
JLabel l1 = new JLabel("Username");
JTextField t1 = new JTextField(10);
JButton b1 = new JButton("Login");
JLabel val = new JLabel();

JPanel p2 = new JPanel();
JLabel l2 = new JLabel("Schema");
final JComboBox c1 = new JComboBox();
JButton b2 = new JButton("OK");

JPanel p3 = new JPanel();
JLabel l3 = new JLabel("Query");
final JComboBox c2 = new JComboBox();
JButton b3 = new JButton("OK");
JButton reset = new JButton("Reset");
JLabel desc = new JLabel();
JButton b4 = new JButton("Submit");

public Console()
{
    connect();
    frame();
}

public void connect()
{
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:luser/lpass@localhost");
        stmt = conn.createStatement();
    }
    catch(Exception e1)
    {
        System.out.println("Exception caught - "+e1);

    }
}

public void frame()
{
    f.setLayout(null);

    c1.addItem("--Select--");
    c1.addItem("ADMIN");
    c1.addItem("FINANCE");
    c1.addItem("VSS");
    c1.setEditable(true);

    c2.addItem("--Select--");

    p1.setBounds(0,10,580,160);

    p1.add(l1);
    p1.add(t1);
    p1.add(b1);
    p1.add(val);
    f.add(p1);
    for(Component c:p1.getComponents())
        c.setEnabled(true);

    b1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                String user = t1.getText().trim();
                String sql = "SELECT usr from data WHERE usr='"+user+"'";
                rs = stmt.executeQuery(sql);
                int count=0;
                while(rs.next())
                {
                    count++;
                }
                if(count==1)
                {
                    val.setText("Valid user!");
                    for(Component c:p2.getComponents())
                    c.setEnabled(true);
                }
                else
                {
                    val.setText("Invalid User!");
                }
            }
            catch(Exception e2)
            {
                System.out.println("Exception caught -"+e2);
            }
        }
    });

    p2.setBounds(0,180,580,100);

    p2.add(l2);
    p2.add(c1);
    p2.add(b2);
    f.add(p2);
    for(Component c:p2.getComponents())
        c.setEnabled(false);

    //--LABEL
    b2.addActionListener(new ActionListener()
    { 
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                v1 = (String) c1.getSelectedItem();
                if(v1.equals("ADMIN"))
                {
                    c2.addItem("Query 3");
                    c2.addItem("Query 4");
                    c2.addItem("Query 5");
                    c2.addItem("Query 6");
                    c2.addItem("Query 7");
                    c2.addItem("Query 8");
                    c2.addItem("Query 9");

                }
                else if(v1.equals("FINANCE"))
                {
                    c2.addItem("Query 1");
                    c2.addItem("Query 2");
                    c2.addItem("Query 5");
                    c2.addItem("Query 10");

                }
                else if(v1.equals("VSS"))
                {
                    c2.addItem("Query 3");
                    c2.addItem("Query 4");
                    c2.addItem("Query 6");
                    c2.addItem("Query 9");

                }
                for(Component c:p2.getComponents())
                    c.setEnabled(false);
                for(Component c:p3.getComponents())
                    c.setEnabled(true);
            }
            catch(Exception e3)
            {
                System.out.println("Exception caught -"+e3);
            }
        }
    });

    p3.setBounds(0,290,580,300);

    p3.add(l3);
    p3.add(c2);
    p3.add(b3);
    f.add(p3);
    p3.add(desc);
    p3.add(reset);
    p3.add(b4);

    for(Component c:p3.getComponents())
        c.setEnabled(false);

    b4.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                //continue execution
            }
            catch(Exception e5)
            {
                System.out.println("Exception caught -"+e5);
            }
        }
    });

    reset.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
                //--LABEL--
        }
    });

    f.setSize(600,600);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}
}
user3222635
  • 37
  • 1
  • 1
  • 4

1 Answers1

1

Maybe you should create a function my_fn() which is executed first and contains all the other method calls, initialisation etc.

When you click on the reset button you can call this method.

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • Good suggestion, but maybe add a little bit more code -- It looks like user3222635 doesn't _really_ know how to use functions/methods yet. – Simon Groenewolt Jan 22 '14 at 20:43