Results 1 to 2 of 2
  1. #1
    tremon is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Unhappy Selection in JTable

    hi,

    i'm not a very experienced java programmer and i need some help with an assignment.

    i made a connection to a database and i show the records in a JTable.

    i've made a south panel with some labels and textfields and the intention is that when i click on a record, the information of that record is shown in the textfields in the south panel.

    i have no idea how to do this. please help :confused:

    Java Code:
    package voorbeelden;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.sql.*;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    
    public class Frame1 extends JFrame implements WindowListener{
    	static int tellerFrame = 0;
    	private JLabel lblCDid, lblGroep, lblGenre,lblVoorraad, lblUitgDatum;
    	private JTextField txtCDid, txtGroep, txtGenre, txtVoorraad, txtUitgDatum;
    	private JPanel pnlZuid;
    	private JButton btnUpdate;
    
    
    	public Frame1() {
    		super("opgave4");
    		tellerFrame++;
    		Connectie con = new Connectie();
    		con.maakConnectie();
    
    		this.setLayout(new BorderLayout());
    
    			pnlZuid = new JPanel();
    
    		this.add(pnlZuid,BorderLayout.SOUTH );
    
    		pnlZuid.setLayout(new GridLayout(6,2));
    		pnlZuid.setBackground(Color.GREEN);
    
    		lblCDid = new JLabel("CD-id");
    		lblGroep = new JLabel("Groep");
    		lblGenre = new JLabel("Genrenummer");
    		lblVoorraad = new JLabel ("Voorraad");
    		lblUitgDatum= new JLabel ("Uitgave datum");
    
    		txtCDid = new JTextField();
    		txtGroep = new JTextField();
    		txtGenre = new JTextField();
    		txtVoorraad = new JTextField();
    		txtUitgDatum = new JTextField();
    
    		btnUpdate = new JButton("Update");
    
    		pnlZuid.add(lblCDid);
    		pnlZuid.add(txtCDid);
    		pnlZuid.add(lblGroep);
    		pnlZuid.add(txtGroep);
    		pnlZuid.add(lblGenre);
    		pnlZuid.add(txtGenre);
    		pnlZuid.add(lblVoorraad);
    		pnlZuid.add(txtVoorraad);
    		pnlZuid.add(lblUitgDatum);
    		pnlZuid.add(txtUitgDatum);
    		pnlZuid.add(btnUpdate);
    
    		Object[][] data = con.haalGegevensOp();
    
    		for (int i = 0; i < data.length; i++) {
    			for (int j = 0; j < data[i].length; j++) {
    			System.out.print("\t"+data[i][j].toString());
    			}
    			System.out.println();
    			}
    
    
    		String[] columnNames = con.haalKolomNamenOp();
    
    		final JTable table = new JTable(data, columnNames);
    
    		JScrollPane scrollPane = new JScrollPane(table);
    		table.setPreferredScrollableViewportSize(new Dimension(500, 300));
    
    		getContentPane().add(scrollPane, BorderLayout.NORTH);
    
    
    
    
    		setSize(900,800);
    		setLocation(200,200);
    		setVisible(true);
    		addWindowListener(this);
    	}
    
    	public void windowActivated(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void windowClosed(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void windowClosing(WindowEvent e) {
    		// TODO Auto-generated method stub
    		tellerFrame=0;
    	}
    
    	public void windowDeactivated(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void windowDeiconified(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void windowIconified(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void windowOpened(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    this is what i get



    additionally, when i change the textfields and click the button Update, the database should be updated.

    please help!

    Connection class:
    Java Code:
    package voorbeelden;
    
    import java.sql.*;
    
    public class Connectie implements ConnectieInterface {
    	private Connection con = null;
    
    	private String sql;
    
    	public Connectie() {
    	}
    
    	public boolean maakConnectie() {
    
    		try {
    			Class.forName(DRIVER);
    			con = DriverManager.getConnection(URL);
    			return true;
    		} catch (Exception e) {
    			// e.printStackTrace();
    			System.out.println("De connectie met de databank kon niet"
    					+ " gemaakt worden wegens verbindingsproblemen");
    			return false;
    		}
    	}
    
    	public void sluitConnectie() {
    		if (con != null) {
    			try {
    				con.close();
    				con = null;
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public String[] haalKolomNamenOp() {
    		String[] kolomkoppen;
    		try {
    
    			Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    					ResultSet.CONCUR_READ_ONLY);
    			s.execute("SELECT * FROM " + TABEL);
    			ResultSet rs = s.getResultSet();
    			rs.last();
    			rs.beforeFirst();
    			ResultSetMetaData RSMD = rs.getMetaData();
    			int akol = RSMD.getColumnCount();
    			String[] kolnamen = new String[akol];
    			for (int i = 0; i < akol; i++) {
    				kolnamen[i] = RSMD.getColumnName(i + 1);
    			}
    			kolomkoppen = kolnamen;
    			return kolomkoppen;
    		} catch (Exception e) {
    			System.out
    					.println("De kolomhoofdingen kunnen niet opgehaald worden");
    			return null;
    		}
    	}
    
    	public Object[][] haalGegevensOp() {
    		Object mijnArray[][] = null;
    		ResultSet rs;
    		try {
    			Statement stmt = con
    					.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    							ResultSet.CONCUR_READ_ONLY);
    			sql = "SELECT * FROM " + TABEL;
    			rs = stmt.executeQuery(sql);
    
    			rs.last();
    			int arij = rs.getRow();
    			rs.beforeFirst();
    			ResultSetMetaData rsmd = rs.getMetaData();
    			int akol = rsmd.getColumnCount();
    
    			mijnArray = new Object[arij][akol];
    			int r = 0;
    			while (rs.next()) {
    				for (int c = 1; c <= akol; c++) {
    					mijnArray[r][c - 1] = rs.getObject(c);
    					//System.out.print(mijnArray[r][c - 1] + "  ");
    				}
    				r++;
    				//System.out.println();
    			}
    			rs.close();
    
    			for (int i = 0; i < mijnArray.length; i++) {
    				for (int j = 0; j < mijnArray[i].length; j++) {
    				System.out.print("\t"+mijnArray[i][j].toString());
    				}
    				System.out.println();
    				}
    
    			return mijnArray;
    		} catch (Exception e) {
    			System.out.println("De gegevens kunnen niet opgehaald worden");
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    }
    Last edited by tremon; 06-11-2010 at 10:09 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Read the section from the Swing tutorial on How to Write a List Selection Listener.

    So when a row is selected you:

    a) save the slected row number
    b) use the getValueAt(...) method of JTable to get the data for each column of the specific row and update the related text field

    When you want to save the changes you:

    a) get the value from each text field and then use the setValueAt(...) method to update the table.

Similar Threads

  1. Problem in row selection in JTable
    By simmi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2010, 04:30 PM
  2. Jtable selection question
    By casid in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 07:11 PM
  3. Jtable Column Selection Problem
    By REVANSIDDHA in forum Advanced Java
    Replies: 0
    Last Post: 03-31-2009, 07:53 AM
  4. Problem in JTable row selection
    By shanssat in forum AWT / Swing
    Replies: 1
    Last Post: 02-04-2009, 07:50 AM
  5. Multiple selection on JTable
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •