Hi...
I have three panels.In the top panel there is one label,one textfield and one button.On click of that button it will verify the textfield.If the value in the textfield is a valid one it will display a table in the second panel or else a label in the second panel...but my problem is that whenever the button is clicked a table or a label opens depending on the textvalue....whereas the panel has to get refreshed when the button is clicked....Any suggestions..
Following is the code
package custQuery;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class table4 extends javax.swing.JFrame
{
// private boolean DEBUG = true;
/** Creates new form table */
public table4() {
initComponent();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
public void initComponent() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField(20);
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
//jButton1 = new javax.swing.JButton();
button1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
getContentPane().setLayout(new java.awt.GridLayout(3, 2));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridBagLayout());
jPanel1.setBackground(java.awt.Color.white);
jLabel1.setForeground(java.awt.Color.black);
jLabel1.setText("Name"
jPanel1.add(jLabel1, new java.awt.GridBagConstraints());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
jPanel1.add(jTextField1, gridBagConstraints);
jPanel1.add(jLabel2, new java.awt.GridBagConstraints());
button1.setText("Search"
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 14;
gridBagConstraints.gridy = 0;
ButtonListener listen = new ButtonListener();
button1.addActionListener(listen);
jPanel1.add(button1, gridBagConstraints);
getContentPane().add(jPanel1);
jPanel2.setLayout(new java.awt.GridLayout(1,3));
jPanel2.setBackground(java.awt.Color.white);
getContentPane().add(jPanel2);
jPanel3.setBackground(java.awt.Color.white);
getContentPane().add(jPanel3);
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new table4().show();
}
// Variables declaration - do not modify
private javax.swing.JButton button1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
//private javax.swing.JTable jTable1;
private javax.swing.JTable table;
private javax.swing.JTextField jTextField1;
//private javax.swing.JPanel jPane1;
// End of variables declaration
class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
if (source==button1)
{
String txtName=jTextField1.getText();
if ("Sanjay".equals(txtName.trim()))
{
System.out.println(jTextField1.getText());
Object[][] data = {
{"1", "SANJAY KUMAR",
"36706004208005", new Integer(18), "SALT LAKE CAL"},
{"2", "SANJAY KUMAR",
"36706005764006", new Integer(18), "CALCUTTA"},
{"3", "SANJAY KUMAR DAS",
"36706002427003", new Integer(18), "CALCUTTA"},
{"4", "SANJAY RAMESHLAL CHAWLA",
"4937149044948102", new Integer(31), "NEW DELHI"},
{"5", "SANJAY RAMESHLAL CHAWLA",
"4937149044949100", new Integer(31), "NEW DELHI"},
{"6", "SANJAY RAMESHLAL CHAWLA",
"4937149044949308", new Integer(31), "NEW DELHI"},
{"7", "SANJAY RAMESHLAL CHAWLA",
"4937149044950108", new Integer(31), "NEW DELHI"},
{"8", "SANJAY RAMESHLAL CHAWLA",
"4937149044950207", new Integer(31), "NEW DELHI"},
{"9", "SANJAY RAMESHLAL CHAWLA",
"4937149044950306", new Integer(31), "NEW DELHI"},
{"10", "SANJAY RAMESHLAL CHAWLA",
"4937149044951106", new Integer(31), "MUMBAI"},
{"11", "SANJAY RAMESHLAL CHAWLA",
"4937149044951205", new Integer(31), "MUMBAI"}
};
String[] columnNames = {"S.No", "Name", "Card No","Card Type","Location"};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//jPanel2.add(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBackground(java.awt.Color.white);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jPanel2.add(scrollPane);
}
else
{JLabel lab1=new JLabel("Not Valid"
jPanel2.add(lab1);}
}
}
}
}
I have three panels.In the top panel there is one label,one textfield and one button.On click of that button it will verify the textfield.If the value in the textfield is a valid one it will display a table in the second panel or else a label in the second panel...but my problem is that whenever the button is clicked a table or a label opens depending on the textvalue....whereas the panel has to get refreshed when the button is clicked....Any suggestions..
Following is the code
package custQuery;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class table4 extends javax.swing.JFrame
{
// private boolean DEBUG = true;
/** Creates new form table */
public table4() {
initComponent();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
public void initComponent() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField(20);
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
//jButton1 = new javax.swing.JButton();
button1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
getContentPane().setLayout(new java.awt.GridLayout(3, 2));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridBagLayout());
jPanel1.setBackground(java.awt.Color.white);
jLabel1.setForeground(java.awt.Color.black);
jLabel1.setText("Name"
jPanel1.add(jLabel1, new java.awt.GridBagConstraints());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
jPanel1.add(jTextField1, gridBagConstraints);
jPanel1.add(jLabel2, new java.awt.GridBagConstraints());
button1.setText("Search"
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 14;
gridBagConstraints.gridy = 0;
ButtonListener listen = new ButtonListener();
button1.addActionListener(listen);
jPanel1.add(button1, gridBagConstraints);
getContentPane().add(jPanel1);
jPanel2.setLayout(new java.awt.GridLayout(1,3));
jPanel2.setBackground(java.awt.Color.white);
getContentPane().add(jPanel2);
jPanel3.setBackground(java.awt.Color.white);
getContentPane().add(jPanel3);
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new table4().show();
}
// Variables declaration - do not modify
private javax.swing.JButton button1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
//private javax.swing.JTable jTable1;
private javax.swing.JTable table;
private javax.swing.JTextField jTextField1;
//private javax.swing.JPanel jPane1;
// End of variables declaration
class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
if (source==button1)
{
String txtName=jTextField1.getText();
if ("Sanjay".equals(txtName.trim()))
{
System.out.println(jTextField1.getText());
Object[][] data = {
{"1", "SANJAY KUMAR",
"36706004208005", new Integer(18), "SALT LAKE CAL"},
{"2", "SANJAY KUMAR",
"36706005764006", new Integer(18), "CALCUTTA"},
{"3", "SANJAY KUMAR DAS",
"36706002427003", new Integer(18), "CALCUTTA"},
{"4", "SANJAY RAMESHLAL CHAWLA",
"4937149044948102", new Integer(31), "NEW DELHI"},
{"5", "SANJAY RAMESHLAL CHAWLA",
"4937149044949100", new Integer(31), "NEW DELHI"},
{"6", "SANJAY RAMESHLAL CHAWLA",
"4937149044949308", new Integer(31), "NEW DELHI"},
{"7", "SANJAY RAMESHLAL CHAWLA",
"4937149044950108", new Integer(31), "NEW DELHI"},
{"8", "SANJAY RAMESHLAL CHAWLA",
"4937149044950207", new Integer(31), "NEW DELHI"},
{"9", "SANJAY RAMESHLAL CHAWLA",
"4937149044950306", new Integer(31), "NEW DELHI"},
{"10", "SANJAY RAMESHLAL CHAWLA",
"4937149044951106", new Integer(31), "MUMBAI"},
{"11", "SANJAY RAMESHLAL CHAWLA",
"4937149044951205", new Integer(31), "MUMBAI"}
};
String[] columnNames = {"S.No", "Name", "Card No","Card Type","Location"};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//jPanel2.add(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBackground(java.awt.Color.white);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jPanel2.add(scrollPane);
}
else
{JLabel lab1=new JLabel("Not Valid"
jPanel2.add(lab1);}
}
}
}
}