:confused: How to add JScrollPane in JTable , and use GridBagLayout to set the JScrollpane in the right side of the JTable.
Printable View
:confused: How to add JScrollPane in JTable , and use GridBagLayout to set the JScrollpane in the right side of the JTable.
U can pass the object of JTable into the constructor of JScrollPane,,
code will look something like .
see documentation.. for more..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
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.
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();
}
}
something is missing in with ur code .. that turned me off ..
uknow wht? code tags.. next time.use it.
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.
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
}
}
thank pow. How about helping me out with ComboBox to choose a date from a calendar
just played a bit with ur code.. run it, now.. it will meet ur requirement..
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
}
}
NP..Quote:
thank pow.
Dude do u have API documentation?.. see JComboBox and Calender classes..Quote:
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.
wow !!!!!!!!!!! that great pow. u really help who are in need.
by da way .. m a "southpow". dont know .. google itQuote:
pow