Results 1 to 13 of 13
Thread: how to add JScrollPane in JTable
- 03-31-2010, 11:20 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
- 03-31-2010, 12:00 PM #2
U can pass the object of JTable into the constructor of JScrollPane,,
code will look something like .
see documentation.. for more..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
- 03-31-2010, 12:19 PM #3
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
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.
- 03-31-2010, 12:25 PM #4
lets debug ur code .. post ur efforts here
- 03-31-2010, 12:58 PM #5
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
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();
}
}
- 03-31-2010, 01:00 PM #6
something is missing in with ur code .. that turned me off ..
uknow wht? code tags.. next time.use it.
- 03-31-2010, 01:06 PM #7
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
- 03-31-2010, 01:08 PM #8
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 } }
- 03-31-2010, 01:33 PM #9
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
thank pow. How about helping me out with ComboBox to choose a date from a calendar
- 03-31-2010, 01:41 PM #10
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 } }
- 03-31-2010, 01:44 PM #11
NP..thank pow.
Dude do u have API documentation?.. see JComboBox and Calender classes..How about helping me out with ComboBox to choose a date from a calendar
Generate some code.. urself.. and give it to me .. i will play with it .. if u fail to play.
- 03-31-2010, 01:47 PM #12
Member
- Join Date
- Jun 2009
- Posts
- 43
- Rep Power
- 0
wow !!!!!!!!!!! that great pow. u really help who are in need.
- 03-31-2010, 01:54 PM #13
Similar Threads
-
How to clear data in Jtable in JScrollPane
By simmi in forum AWT / SwingReplies: 5Last Post: 08-17-2009, 11:26 AM -
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
[SOLVED] JTable in JScrollPane renders partly invisible
By bronai in forum AWT / SwingReplies: 3Last Post: 01-11-2009, 11:20 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks