Results 1 to 13 of 13
  1. #1
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Question how to add JScrollPane in JTable

    :confused: How to add JScrollPane in JTable , and use GridBagLayout to set the JScrollpane in the right side of the JTable.

  2. #2
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    U can pass the object of JTable into the constructor of JScrollPane,,

    code will look something like .
    Java Code:
    DefaultTableModel tablemodel = new DefaultTableModel(); // to get default tablemodel
    
    tablemodel.addColumn(); // u can add colums,
    
    JTable table = new JTable(tablemodel); // create table
    
    table.setRowHeight(); //set rowheights
    
    JScrollPane scroll = new JScrollPane(table); // add scrollpane
    see documentation.. for more..

  3. #3
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Exclamation

    DefaultTableModel tablemodel = new DefaultTableModel(); // to get default tablemodel

    tablemodel.addColumn(); // u can add colums,

    JTable table = new JTable(tablemodel); // create table

    table.setRowHeight(); //set rowheights

    JScrollPane scroll = new JScrollPane(table); // add scrollpane

    **************
    but if i use GridBagLayout the JScrollpane appear in the right side of the table but the table is not scroll when i try to scroll it, also when i add more data in the table the table get resized.

  4. #4
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    lets debug ur code .. post ur efforts here

  5. #5
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by programmer_007 View Post
    lets debug ur code .. post ur efforts here
    Here is my code please check it , i can't use the scroll, it appeared but useless.

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.table.*;

    public class table extends JFrame {

    public table() {

    JFrame jframe = new JFrame();


    JPanel tablepanel = new JPanel();

    String data[][] = {
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    {"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    };
    String col[] = {"Name","Surname","Address","Phone No","Bill No","Batch No","Quality","Date","Rate","Amount"};

    DefaultTableModel model = new DefaultTableModel(data,col);
    JTable jtable = new JTable(model);
    jtable.setSelectionForeground(Color.RED);
    jtable.setSelectionBackground(Color.BLUE);

    JScrollPane tableScrollPane = new JScrollPane(jtable);
    tableScrollPane.setVerticalScrollBarPolicy(JScroll Pane.VERTICAL_SCROLLBAR_ALWAYS);
    tableScrollPane.setHorizontalScrollBarPolicy(JScro llPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


    JPanel paneltry = new JPanel();

    JButton delete = new JButton("Delete");
    delete.setBounds(10,100,100,50);

    JButton update = new JButton("Update");
    update.setBounds(10,150,100,50);

    paneltry.setBorder(BorderFactory.createTitledBorde r(""));
    paneltry.setLayout(new GridBagLayout());

    paneltry.add(delete, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.BELOW_BASELINE_LEADING, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));
    paneltry.add(update, new GridBagConstraints(3, 0, 2, 1, 0, 0, GridBagConstraints.BELOW_BASELINE_LEADING, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));

    JPanel testpanel = new JPanel();

    testpanel.add(tablepanel, new GridBagConstraints(0, 0, 1,1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));
    testpanel.add(paneltry, new GridBagConstraints(0, 1, 13, 1, 0.0, 0.0, GridBagConstraints.BELOW_BASELINE_LEADING, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));


    tablepanel.setBorder(BorderFactory.createTitledBor der("Table"));
    tablepanel.setLayout(new GridBagLayout());
    tablepanel.add(tableScrollPane, new GridBagConstraints(1, 1, 0, 0, 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets(5,5,5,5), 0, 0));
    tablepanel.add(jtable.getTableHeader(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    tablepanel.add(jtable, new GridBagConstraints(0, 2, 0, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    tablepanel.add(new JPanel(), new GridBagConstraints(0, 1, 1, 0, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));


    jframe.add(testpanel);
    jframe.setSize(800,600);
    jframe.setLocation(150,90);
    jframe.setVisible(true);

    }

    public static void main(String args[]){

    new table();
    }
    }

  6. #6
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    something is missing in with ur code .. that turned me off ..

    uknow wht? code tags.. next time.use it.

  7. #7
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by programmer_007 View Post
    something is missing in with ur code .. that turned me off ..

    uknow wht? code tags.. next time.use it.
    ok , so what have i missed in that, and why the scroll is not working.

  8. #8
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    here is a small example code for the proper working of scrollPane with demo rows and colums. hope u can debug ur code with help of these. Run it and see.
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    import java.sql.*;
    import java.util.Vector;
    
    public class Report extends JFrame 
    {   
    	
    	JButton print_report;
    	
    	public static void main(String []args) throws Exception
    	{
    		Report r = new Report();
    
    		
    	}
    
    	
    	
    	private GridBagConstraints getConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
    	{
    		GridBagConstraints c =new GridBagConstraints();
    		c.insets = new Insets(10,10,10,10);
    		c.ipadx = 0;
    		c.ipady = 0;
    		c.gridx = gridx;
    		c.gridy = gridy;
    		c.gridwidth = gridwidth;
    		c.gridheight = gridheight;
    		c.anchor = anchor;
    		return c;
    	}
    	public Report()throws Exception//constructor for creating frame
    	{
    		setTitle("Retrive Information");//sets title to frame
    		Toolkit tk = Toolkit.getDefaultToolkit();
    	    Dimension d = tk.getScreenSize();
    		setSize(500,500);
    		setVisible(true);
    		setResizable(false);//can b resized by user
    		
    		
    		DefaultTableModel tablemodel = new DefaultTableModel();
    		for(int i =0;i<=30;i++)
    		{	
    			tablemodel.addColumn(i);	
    		}
    		
    		
    		JTable table = new JTable(tablemodel);
    		table.setRowHeight(25);
    		
    		JScrollPane scroll = new JScrollPane(table);
    		scroll.createHorizontalScrollBar();
    		JPanel panel = new JPanel();
    		panel.setLayout(new BorderLayout());
    		panel.add(scroll,BorderLayout.CENTER);
    
    		JPanel header_P = new JPanel();
    		header_P.setLayout(new GridBagLayout());
    
    		print_report = new JButton("PRINT");
    		print_report.addActionListener(new ButtonHandler());
    		panel.add(print_report,BorderLayout.SOUTH);
    
    		Container contentPane = getContentPane();
    		contentPane.add(panel);
    		for(int i =1;i<30;i++)
    		{	
    			Vector data = new Vector();
    			data.addElement("ss");
    			tablemodel.addRow(data);
    		}
    	}
    
    }; 
    class ButtonHandler implements ActionListener
    {
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  9. #9
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    thank pow. How about helping me out with ComboBox to choose a date from a calendar

  10. #10
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    just played a bit with ur code.. run it, now.. it will meet ur requirement..

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    import java.sql.*;
    import java.util.Vector;
    
    public class Report extends JFrame 
    {   
    	
    	
    	
    	public static void main(String []args) throws Exception
    	{
    		Report r = new Report();
    
    		
    	}
    
    	
    	
    	private GridBagConstraints getConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
    	{
    		GridBagConstraints c =new GridBagConstraints();
    		c.insets = new Insets(10,10,10,10);
    		c.ipadx = 0;
    		c.ipady = 0;
    		c.gridx = gridx;
    		c.gridy = gridy;
    		c.gridwidth = gridwidth;
    		c.gridheight = gridheight;
    		c.anchor = anchor;
    		return c;
    	}
    	public Report()throws Exception//constructor for creating frame
    	{
    		setTitle("Retrive Information");//sets title to frame
    		Toolkit tk = Toolkit.getDefaultToolkit();
    	    Dimension d = tk.getScreenSize();
    		
    		
    		String data[][] = {
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","first"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","23.90"},
    				{"me","Ldoh","Nongthymmai","23432342","123","201", "Cool","02/08/2010","23.90","last"},
    				};
    				String col[] = {"Name","Surname","Address","Phone No","Bill No","Batch No","Quality","Date","Rate","Amount"};
    		
    		DefaultTableModel tablemodel = new DefaultTableModel();
    		for(int i =0;i<col.length;i++)
    		{	
    			tablemodel.addColumn(col[i]);	
    		}
    		
    		
    		JTable table = new JTable(tablemodel);
    		table.setRowHeight(25);
    		
    		JScrollPane scroll = new JScrollPane(table);
    		scroll.createHorizontalScrollBar();
    		JPanel panel = new JPanel();
    		panel.setLayout(new BorderLayout());
    		panel.add(scroll,BorderLayout.CENTER);
    
    		JPanel header_P = new JPanel();
    		header_P.setLayout(new GridBagLayout());
    
    		JButton delete = new JButton("delete");
    		delete.addActionListener(new ButtonHandler());
    		
    		JButton update = new JButton("Update");
    		update.addActionListener(new ButtonHandler());
    		
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.add(delete);
    		buttonPanel.add(update);
    		
    		
    		panel.add(buttonPanel,BorderLayout.SOUTH);
    
    		Container contentPane = getContentPane();
    		contentPane.add(panel);
    		
    		System.out.println(data.length);
    		System.out.println(col.length);
    		int i=0;
    		while(i<data.length)
    		{	
    			Vector data1 = new Vector();
    			int j=0;
    			while(j<col.length)
    			{
    			data1.addElement(data[i][j]);
    			j++;
    			}
    			tablemodel.addRow(data1);
    			i++;
    			
    		}
    		setSize(600,500);
    		setVisible(true);
    		setResizable(false);//can b resized by user		
    		
    		
    	}
    
    }; 
    class ButtonHandler implements ActionListener
    {
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  11. #11
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    thank pow.
    NP..

    How about helping me out with ComboBox to choose a date from a calendar
    Dude do u have API documentation?.. see JComboBox and Calender classes..

    Generate some code.. urself.. and give it to me .. i will play with it .. if u fail to play.

  12. #12
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    wow !!!!!!!!!!! that great pow. u really help who are in need.

  13. #13
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

Similar Threads

  1. How to clear data in Jtable in JScrollPane
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-17-2009, 12:26 PM
  2. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM
  3. Replies: 3
    Last Post: 01-12-2009, 12:20 AM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 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
  •