Results 1 to 2 of 2
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default Java Swing JTable Simple Doubt

    Hello there,

    In the below code of a JTable Example I wanted to know how to print a cell value when you double click on a cell. I was wrong in my logic. Could someone help me with this. I tried using a Point Class.

    Java Code:
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableModel;
    
    public class JTableDoubtDemo extends JPanel {
    
    	public JTableDoubtDemo() {
    		super(new GridLayout(1, 0));
    
    		String[] columnHeaders = { "First Name", "Last Name", "Subject",
    				"Ecode", "Pass/Fail" };
    
    		Object[][] data = {
    				{ "Bob", "Marley", "English", new Integer(35),
    						new Boolean(false) },
    				{ "Rob", "barley", "German", new Integer(63), new Boolean(true) } };
    
    		final JTable jtab = new JTable(data, columnHeaders);
    		jtab.setPreferredScrollableViewportSize(new Dimension(500, 70));
    //		table.setFillsViewportHeight(true);
    
    		jtab.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {
    				printDebugData(jtab);
    			}
    		});
    
    		//Create the scroll pane and add the table to it.
    		JScrollPane scrollPane = new JScrollPane(jtab);
    
    		//Add the scroll pane to this panel.
    		add(scrollPane);
    	}
    
    	private void printDebugData(JTable table) {
    		
    		Point p = table.getMousePosition();
    		int x = (int)(p.getX());
    		int y = (int)(p.getY());
    		System.out.println(x+"\t"+y);
    		TableModel model = table.getModel();
    		System.out.println("Value of data : "+model.getValueAt(x, y));
    	
    	}
    
    	private static void createAndShowGUI() {
    
    		JFrame frame = new JFrame("JTable Example");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		//Create and set up the content pane.
    		JTableDoubtDemo contentPane = new JTableDoubtDemo();
    		contentPane.setOpaque(true); //content panes must be opaque
    		frame.setContentPane(contentPane);
    
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    }
    Regards,
    Hemanth

  2. #2
    YuanVsXin is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 02:45 AM
  2. Swing problem: JTable/TableModel
    By Levish2002 in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2008, 09:53 PM
  3. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  4. A great doubt in Java Applet,will u solve it!!
    By anithababu in forum Java Applets
    Replies: 6
    Last Post: 01-27-2008, 02:20 PM
  5. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 08:07 AM

Posting Permissions

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