Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
Hello,I have a java sing form and search box,when I search an item,the search result outputs over the column,how I must fix it?
here is code:
package aaa;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
 
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.*;
import java.util.Vector;
 
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class StudJFrame extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JTable jTableStudents;
	private JButton jButtonSearch;
	private JComboBox jComboBoxGender;
	private JPanel jPanel5;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private JLabel jLabel2;
	private JTextField jTextFieldSurname;
	private JLabel jLabel1;
	private JPanel jPanel2;
    Connection con= null;
    
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		StudJFrame inst = new StudJFrame();
	
		inst.setVisible(true);
	}
	
	public StudJFrame() {
		super();
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection("jdbc:odbc:StudDBDS","","");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initGUI();
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			thisLayout.setVgap(20);
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel2 = new JPanel();
				GridLayout jPanel2Layout = new GridLayout(3, 1);
				jPanel2Layout.setHgap(5);
				jPanel2Layout.setVgap(5);
				jPanel2Layout.setColumns(1);
				jPanel2Layout.setRows(3);
				jPanel2.setLayout(jPanel2Layout);
				getContentPane().add(jPanel2, BorderLayout.NORTH);
				jPanel2.setPreferredSize(new java.awt.Dimension(584, 94));
				{
					jPanel3 = new JPanel();
					FlowLayout jPanel3Layout = new FlowLayout();
					jPanel3Layout.setAlignment(FlowLayout.LEFT);
					jPanel3.setLayout(jPanel3Layout);
					jPanel2.add(jPanel3);
					{
						jLabel1 = new JLabel();
						jPanel3.add(jLabel1);
						jLabel1.setText("Surname");
					}
					{
						jTextFieldSurname = new JTextField();
						jPanel3.add(jTextFieldSurname);
						jTextFieldSurname.setPreferredSize(new java.awt.Dimension(289, 23));
					}
				}
				{
					jPanel4 = new JPanel();
					FlowLayout jPanel4Layout = new FlowLayout();
					jPanel4Layout.setAlignment(FlowLayout.LEFT);
					jPanel4.setLayout(jPanel4Layout);
					jPanel2.add(jPanel4);
					{
						jLabel2 = new JLabel();
						jPanel4.add(jLabel2);
						jLabel2.setText("   Gender");
					}
					{
						
						jComboBoxGender = new JComboBox();
						Statement stmt = con.createStatement();
						ResultSet rs = stmt.executeQuery("Select * From Gender");
						jComboBoxGender.addItem("All");
						while (rs.next())
						{
							jComboBoxGender.addItem(rs.getString(2));
						}
						
						jPanel4.add(jComboBoxGender);
					}
				}
				{
					jPanel5 = new JPanel();
					jPanel2.add(jPanel5);
					{
						jButtonSearch = new JButton();
						jPanel5.add(jButtonSearch);
						jButtonSearch.setText("Search");
						jButtonSearch.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButtonSearchActionPerformed(evt);
							}
						});
					}
				}
			}
			{
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				{
					jScrollPane1 = new JScrollPane();
					jPanel1.add(jScrollPane1, BorderLayout.CENTER);
					{
						String[] header = {"Surname","Name","Group","Gender" };
						jTableStudentsModel = new DefaultTableModel(header,0);
						jTableStudents = new JTable();
						jScrollPane1.setViewportView(jTableStudents);
						jTableStudents.setModel(jTableStudentsModel);
					}
				}
			}
			pack();
			setSize(600, 450);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	DefaultTableModel jTableStudentsModel  = null;
	private void jButtonSearchActionPerformed(ActionEvent evt) {
		String gender = jComboBoxGender.getSelectedItem().toString();
		String query = "Select Surname,FName,Groupe,Gender.GName FROM Student,Gender"+
		" WHERE Student.GenderID = Gender.ID and Surname LIKE ? ";
		if (!gender.equals("All"))
				{
					query +="and Gender.GName = ?";
				}
				
		
		try
	    {
	      PreparedStatement ps= con.prepareStatement(query);
	      
 
	      ps.setString(1, jTextFieldSurname.getText()+"%");
	      if (!gender.equals("All"))   ps.setString(2,gender );
	      
	      ResultSet rs = ps.executeQuery();
	      int n= jTableStudentsModel.getRowCount();
	      for (int i = 0; i < n; i++) {
	    	  jTableStudentsModel.removeRow(i);
	      }
	      int i=0;
	      while (rs.next() )
	      {
	        Vector vec= new Vector();
	        vec.add(rs.getString(1));
	        vec.add(rs.getString(2));
	        vec.add(rs.getString(3));
	        vec.add(rs.getString(4));
	        jTableStudentsModel.addRow(vec);
	      }
	      jTableStudents.repaint();
	    }
	    catch (Exception ex) {
	    		int i= 1;
	    		i++;
	    }
 
		
		
	}
 
}
Posted 27-Mar-13 11:04am
Comments
Jonathan [Darka] at 27-Mar-13 17:21pm
   
First of all, you need to tell us where it's going wrong and ask specific questions, no one here has time to read hundreds of lines of your code to determine the problem you are having.
 
Also, try to only post a small snippet of the code, instead of the entire application - makes it much easier to read.
WaZoX at 28-Mar-13 20:36pm
   
Code is normally the worst way to explain a problem. Try to be more specific, there are a lot of people here who probably can help you out then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 384
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 CPallini 170
4 Shweta N Mishra 165
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100