aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to set size of columns in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set size of columns in JTable" Watch "How to set size of columns in JTable" New topic
Author

How to set size of columns in JTable

daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Hi Guys,
I have a Jtable inside a JScrollPane.I am using a AbstractTableModel class for creating the Table.But am unable to set the particular width column wise. Please advice me.
Thanks in Advance,
Daman
[This message has been edited by daman sidhu (edited November 08, 2001).]
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Try something like

Good luck
Tom
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Thanks a lot Thomas, i will surely try out the same. Thanks again
Daman
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
HI Tom, i tried out the code and it still did not work, please see the same if u can, i am pasting the 2 classes over here
First: This is the class where the table is displayed, run the main of this class:
The method in which i am making the table is getScrollPaneTable()
*************************************************
package app.cores.directentrysorting.client.application;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
public class CoreReturnReportDialog extends JDialog implements ActionListener,WindowListener
{
private JPanel ivjCoreReturnReportDialogPane = null;
private JPanel ivjJFrameContentPane = null;
private JPanel ivjJPanel1 = null;
private JPanel ivjJPanel2 = null;
private JPanel ivjJPanel3 = null;
private JPanel ivjJPanel4 = null;
private JLabel ivjCustomerAddressLabel = null;
private JTextArea ivjCustomerAddressTextArea = null;
private JButton ivjOkButton = null;
private JLabel ivjPalletNumberLabel = null;
private JTextField ivjPalletNumberTextField = null;
private JLabel ivjShipmentIdLabel = null;
private JTextField ivjShipmentIdTextField = null;
private javax.swing.table.TableColumn ivjClassColumn = null;
private javax.swing.table.TableColumn ivjCoreGroupColumn = null;
private javax.swing.table.TableColumn ivjGradeColumn = null;
private JScrollPane ivjJScrollPane1 = null;
private javax.swing.table.TableColumn ivjQuantityColumn = null;
private JTable ivjScrollPaneTable = null;
private javax.swing.table.TableColumn ivjCrmNumberColumn = null;
/**
* CoreReturnReportDialog constructor comment.
*/
public CoreReturnReportDialog() {
super();
initialize();
}
/**
* Return the ClassColumn property value.
* @return javax.swing.table.TableColumn
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.table.TableColumn getClassColumn() {
if (ivjClassColumn == null) {
try {
ivjClassColumn = new javax.swing.table.TableColumn();
ivjClassColumn.setWidth(200);
ivjClassColumn.setHeaderValue("Class");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjClassColumn;
}
/**
* Return the CoreGroupColumn property value.
* @return javax.swing.table.TableColumn
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.table.TableColumn getCoreGroupColumn() {
if (ivjCoreGroupColumn == null) {
try {
ivjClassColumn = new javax.swing.table.TableColumn();
ivjClassColumn.setWidth(200);
ivjClassColumn.setHeaderValue("Core Group");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjClassColumn;
}
/**
* Return the CoreReturnReportDialogPane property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getCoreReturnReportDialogPane() {
if (ivjCoreReturnReportDialogPane == null) {
try {
ivjCoreReturnReportDialogPane = new javax.swing.JPanel();
ivjCoreReturnReportDialogPane.setName("CoreReturnReportDialogPane");
ivjCoreReturnReportDialogPane.setLayout(null);
getCoreReturnReportDialogPane().add(getJPanel1(), getJPanel1().getName());
getCoreReturnReportDialogPane().add(getJPanel2(), getJPanel2().getName());
getCoreReturnReportDialogPane().add(getJPanel4(), getJPanel4().getName());
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in Core Return Report Dialog Pane "+ivjExc);
handleException(ivjExc);
}
}
return ivjCoreReturnReportDialogPane;
}
/**
* Return the CoreGroupColumn property value.
* @return javax.swing.table.TableColumn
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.table.TableColumn getCrmNumberColumn() {
if (ivjCrmNumberColumn == null) {
try {
ivjCrmNumberColumn = new javax.swing.table.TableColumn();
ivjCrmNumberColumn.setWidth(10);
ivjCrmNumberColumn.setHeaderValue("CRM No.");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjCrmNumberColumn;
}
/**
* Return the CustomerAddressLabel property value.
* @return javax.swing.JLabel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JLabel getCustomerAddressLabel() {
if (ivjCustomerAddressLabel == null) {
try {
ivjCustomerAddressLabel = new javax.swing.JLabel();
ivjCustomerAddressLabel.setName("CustomerAddressLabel");
ivjCustomerAddressLabel.setFont(new java.awt.Font("dialog", 0, 12));
ivjCustomerAddressLabel.setText("Customer Address:");
ivjCustomerAddressLabel.setBounds(78, 49, 113, 17);
ivjCustomerAddressLabel.setForeground(java.awt.Color.black);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjCustomerAddressLabel;
}
/**
* Return the CustomerAddressTextArea property value.
* Modified Date : 24-October-2001
* @return javax.swing.JTextArea
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextArea getCustomerAddressTextArea() {
if (ivjCustomerAddressTextArea == null) {
try {
ivjCustomerAddressTextArea = new javax.swing.JTextArea();
ivjCustomerAddressTextArea.setName("CustomerAddressTextArea");
ivjCustomerAddressTextArea.setLineWrap(true);
ivjCustomerAddressTextArea.setBorder(new javax.swing.border.EtchedBorder());
ivjCustomerAddressTextArea.setBackground(java.awt.Color.white);
ivjCustomerAddressTextArea.setBounds(198, 47, 202, 76);
//to set the value of the text area to the value of address (class variable)
ivjCustomerAddressTextArea.setText(address);
ivjCustomerAddressTextArea.setEditable(false);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjCustomerAddressTextArea;
}
/**
* Return the GradeColumn property value.
* @return javax.swing.table.TableColumn
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.table.TableColumn getGradeColumn() {
if (ivjGradeColumn == null) {
try {
ivjGradeColumn = new javax.swing.table.TableColumn();
ivjGradeColumn.setWidth(70);
ivjGradeColumn.setHeaderValue("Grade");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjGradeColumn;
}
/**
* Return the JFrameContentPane property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJFrameContentPane() {
if (ivjJFrameContentPane == null) {
try {
ivjJFrameContentPane = new javax.swing.JPanel();
ivjJFrameContentPane.setName("JFrameContentPane");
ivjJFrameContentPane.setLayout(new java.awt.BorderLayout());
getJFrameContentPane().add(getCoreReturnReportDialogPane(), "Center");
System.out.println("after adding the Core return Report dialog pane ");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in JFrameContentPane "+ivjExc);
handleException(ivjExc);
}
}
return ivjJFrameContentPane;
}
/**
* Return the JPanel1 property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJPanel1() {
if (ivjJPanel1 == null) {
try {
ivjJPanel1 = new javax.swing.JPanel();
ivjJPanel1.setName("JPanel1");
ivjJPanel1.setLayout(null);
ivjJPanel1.setBounds(12, 3, 675, 138);
getJPanel1().add(getShipmentIdLabel(), getShipmentIdLabel().getName());
getJPanel1().add(getCustomerAddressLabel(), getCustomerAddressLabel().getName());
getJPanel1().add(getPalletNumberLabel(), getPalletNumberLabel().getName());
getJPanel1().add(getShipmentIdTextField(), getShipmentIdTextField().getName());
getJPanel1().add(getPalletNumberTextField(), getPalletNumberTextField().getName());
getJPanel1().add(getCustomerAddressTextArea(), getCustomerAddressTextArea().getName());
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in JPanel1 "+ivjExc);
handleException(ivjExc);
}
}
return ivjJPanel1;
}
/**
* Return the JPanel2 property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJPanel2() {
if (ivjJPanel2 == null) {
try {
ivjJPanel2 = new javax.swing.JPanel();
ivjJPanel2.setName("JPanel2");
ivjJPanel2.setLayout(null);
ivjJPanel2.setBounds(10, 141, 675, 279);
//getJPanel2().add(getJPanel3(), getJPanel3().getName());
getJPanel2().add(getJScrollPane1(), getJScrollPane1().getName());
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in JPanel2 "+ivjExc);
handleException(ivjExc);
}
}
return ivjJPanel2;
}
/**
* Return the JPanel3 property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJPanel3() {
if (ivjJPanel3 == null) {
try {
ivjJPanel3 = new javax.swing.JPanel();
ivjJPanel3.setName("JPanel3");
ivjJPanel3.setLayout(null);
ivjJPanel3.setBounds(15, 264, 500, 71);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in JPanel3 "+ivjExc);
handleException(ivjExc);
}
}
return ivjJPanel3;
}
/**
* Return the JPanel4 property value.
* @return javax.swing.JPanel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJPanel4() {
if (ivjJPanel4 == null) {
try {
ivjJPanel4 = new javax.swing.JPanel();
ivjJPanel4.setName("JPanel4");
ivjJPanel4.setLayout(null);
ivjJPanel4.setBounds(11, 427, 675, 66);
getJPanel4().add(getOkButton(), getOkButton().getName());
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in JPanel4 "+ivjExc);
handleException(ivjExc);
}
}
return ivjJPanel4;
}
/**
* Return the JScrollPane1 property value.
* @return javax.swing.JScrollPane
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JScrollPane getJScrollPane1() {
if (ivjJScrollPane1 == null) {
try {
ivjJScrollPane1 = new javax.swing.JScrollPane(getScrollPaneTable());
ivjJScrollPane1.setName("JScrollPane1");
//ivjJScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
ivjJScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ivjJScrollPane1.setBounds(0, 2, 673, 260);
ivjJScrollPane1.setAutoscrolls(true);
System.out.println("after creating the object of JScrollPane");
//getJScrollPane1().setViewportView(getScrollPaneTable());
System.out.println("after setting view port view ");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in getting JScrollPane1 "+ivjExc);
handleException(ivjExc);
}
}
return ivjJScrollPane1;
}
/**
* Return the OkButton property value.
* Modified Date : 24-October-2001
* @return javax.swing.JButton
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JButton getOkButton() {
if (ivjOkButton == null) {
try {
ivjOkButton = new javax.swing.JButton();
ivjOkButton.setName("OkButton");
ivjOkButton.setFont(new java.awt.Font("dialog", 0, 12));
ivjOkButton.setMnemonic('O');
ivjOkButton.setText("Ok");
ivjOkButton.setBounds(292, 10, 85, 25);
// to enable the Ok button to listen to action events
ivjOkButton.addActionListener(this);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjOkButton;
}
/**
* Return the PalletNumberLabel property value.
* @return javax.swing.JLabel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JLabel getPalletNumberLabel() {
if (ivjPalletNumberLabel == null) {
try {
ivjPalletNumberLabel = new javax.swing.JLabel();
ivjPalletNumberLabel.setName("PalletNumberLabel");
ivjPalletNumberLabel.setFont(new java.awt.Font("dialog", 0, 12));
ivjPalletNumberLabel.setText("Pallet Number:");
ivjPalletNumberLabel.setBounds(417, 15, 90, 17);
ivjPalletNumberLabel.setForeground(java.awt.Color.black);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjPalletNumberLabel;
}
/**
* Return the PalletNumberTextField property value.
* Modified Date : 24-October-2001
* @return javax.swing.JTextField
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextField getPalletNumberTextField() {
if (ivjPalletNumberTextField == null) {
try {
ivjPalletNumberTextField = new javax.swing.JTextField();
ivjPalletNumberTextField.setName("PalletNumberTextField");
ivjPalletNumberTextField.setBackground(java.awt.Color.white);
ivjPalletNumberTextField.setBounds(511, 15, 99, 20);
//to set the pallet number text field text to the value of palletNumber
ivjPalletNumberTextField.setText(palletNumber);
ivjPalletNumberTextField.setEditable(false);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjPalletNumberTextField;
}
/**
* Return the QuantityColumn property value.
* @return javax.swing.table.TableColumn
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.table.TableColumn getQuantityColumn() {
if (ivjQuantityColumn == null) {
try {
ivjQuantityColumn = new javax.swing.table.TableColumn(10);
//ivjQuantityColumn.setPreferredWidth(10);
ivjQuantityColumn.setHeaderValue("Quantity");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjQuantityColumn;
}
/**
* Return the ScrollPaneTable property value.
* Modified Date : 24-October-2001
* @return javax.swing.JTable
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTable getScrollPaneTable() {
int size = rows.size();
Object data[][] = new Object[size][5];
for(int i=0;i<size;i++)>
{
for(int j = 0;j<5;j++)
{
Vector obj = (Vector) rows.elementAt(i);
Object obj1 = obj.elementAt(j);
data[j] = obj1;//((Vector)rows.elementAt(i)).elementAt(j);
}
}
Object cols[] = new Object[5];
for(int i = 0;i<5;i++)
{
cols[i] = (String) columns.elementAt(i);
}
//System.out.println("after making the object array of columns ");
if (ivjScrollPaneTable == null) {
try {
//to create a table with the rows and columns as specified in the double dimensional array
//System.out.println("before creating the object of JTable ");
TableModel tableModel = new CoreReturnTableModel(rows);
ivjScrollPaneTable = new javax.swing.JTable(tableModel);
//ivjScrollPaneTable = new javax.swing.JTable(rows,columns);
//System.out.println("data is "+data);
//System.out.println("cols is "+cols);
//*ivjScrollPaneTable = new javax.swing.JTable(data,cols);
//ivjScrollPaneTable.setAutoscrolls(true);
//ivjScrollPaneTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//ivjScrollPaneTable.setName("ScrollPaneTable");
//getJScrollPane1().setColumnHeaderView(ivjScrollPaneTable.getTableHeader());
getJScrollPane1().getViewport().setBackingStoreEnabled(true);
ivjScrollPaneTable.setBounds(0, 0, 650, 237);
ivjScrollPaneTable.setAutoCreateColumnsFromModel(true);
ivjScrollPaneTable.getColumnModel().getColumn(0).setWidth(10);
ivjScrollPaneTable.getColumnModel().getColumn(1).setWidth(100);
ivjScrollPaneTable.getColumnModel().getColumn(2).setWidth(100);
ivjScrollPaneTable.getColumnModel().getColumn(3).setWidth(100);
ivjScrollPaneTable.getColumnModel().getColumn(4).setWidth(10);
System.out.println("before adding the columns to the table");
/*
ivjScrollPaneTable.addColumn(getCrmNumberColumn());
ivjScrollPaneTable.addColumn(getCoreGroupColumn());
ivjScrollPaneTable.addColumn(getClassColumn());
ivjScrollPaneTable.addColumn(getGradeColumn());
ivjScrollPaneTable.addColumn(getQuantityColumn());
*/
System.out.println("after adding the columns to the table");
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
System.out.println("Exception in getting the scroll pane table "+ivjExc);
ivjExc.printStackTrace();
handleException(ivjExc);
}
}
return ivjScrollPaneTable;
}
/**
* Return the ShipmentIdLabel property value.
* @return javax.swing.JLabel
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JLabel getShipmentIdLabel() {
if (ivjShipmentIdLabel == null) {
try {
ivjShipmentIdLabel = new javax.swing.JLabel();
ivjShipmentIdLabel.setName("ShipmentIdLabel");
ivjShipmentIdLabel.setFont(new java.awt.Font("dialog", 0, 12));
ivjShipmentIdLabel.setText("Shipment ID:");
ivjShipmentIdLabel.setBounds(78, 15, 76, 17);
ivjShipmentIdLabel.setForeground(java.awt.Color.black);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjShipmentIdLabel;
}
/**
* Return the ShipmentIdTextField property value.
* Modified Date : 24-October-2001
* @return javax.swing.JTextField
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextField getShipmentIdTextField() {
if (ivjShipmentIdTextField == null) {
try {
ivjShipmentIdTextField = new javax.swing.JTextField();
ivjShipmentIdTextField.setName("ShipmentIdTextField");
ivjShipmentIdTextField.setBackground(java.awt.Color.white);
ivjShipmentIdTextField.setBounds(198, 15, 99, 20);
//to assign the value of the shipment id text field to shipmentId(class var.)
ivjShipmentIdTextField.setText(shipmentId);
ivjShipmentIdTextField.setEditable(false);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjShipmentIdTextField;
}
/**
* Called whenever the part throws an exception.
* @param exception java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {
/* Uncomment the following lines to print uncaught exceptions to stdout */
// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
// exception.printStackTrace(System.out);
}
/**
* Initialize the class.
* Initialize the class variable columns and add elements to it.
* Modified Date : 24-October-2001
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
try {
// user code begin {1}
// user code end
/*
block to initialize the class variable columns with the headings of the columns.
to be displayed in the table.
*/
columns.addElement("CRM No.");
columns.addElement("Core Group");
columns.addElement("Class");
columns.addElement("Grade");
columns.addElement("Quantity");
System.out.println("after adding the values in vector of columns "+columns.size());
setName("CoreReturnReportDialog");
//setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
setSize(700, 500);
setModal(true);
setTitle("Core Return Report Dialog");
System.out.println("before setting the content pane ");
setContentPane(getJFrameContentPane());
//setContentPane(getContentPane());
System.out.println("after setting the content pane ");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
// user code begin {2}
// user code end
}
private String address = "";
private Vector columns = new Vector(5);
private String palletNumber = "";
/*
block to define class variables for address, shipment id, pallet
number , vector of rows and vector of columns
*/
private Vector rows = new Vector(2);
private String shipmentId = "";
/**
* CoreReturnReportDialog constructor comment.
* Initializes the class variables with the parameters passed and initializes
* the dialog box.
* Creation Date : 24-October-2001
* @param shipmentId java.lang.String
* @param palletNumber java.lang.String
* @param address java.lang.String
* @param rows java.util.Vector
*/
public CoreReturnReportDialog(String shipmentId,String palletNumber,String address, Vector rows)
{
super();
this.shipmentId = shipmentId;
this.palletNumber = palletNumber;
this.address = address;
this.rows = rows;
System.out.println("after initializing the variables ");
System.out.println("size of rows "+this.rows.size());
initialize();
}/**
* This method is invoked when the OK button is pressed.
* On pressing Ok button the dialog box is closed
* Creation Date : 24-October-2001
* @param ae java.awt.event.ActionEvent
*/
public void actionPerformed(ActionEvent ae)
{
this.dispose();
return;
}public void windowActivated(WindowEvent we)
{
}public void windowClosed(WindowEvent we)
{
}/**
* This method is called when the window has to be closed.
* Creation Date : 25-October-2001
* @param we java.awt.event.WindowEvent
*/
public void windowClosing(WindowEvent we)
{
this.dispose();
return;
}public void windowDeactivated(WindowEvent we)
{
}public void windowDeiconified(WindowEvent we)
{
}public void windowIconified(WindowEvent we)
{
}public void windowOpened(WindowEvent we)
{
}/**
* CoreReturnReportDialog constructor comment.
* Initializes the class variables with the parameters passed and initializes
* the dialog box.
* Creation Date : 24-October-2001
* @param shipmentId java.lang.String
* @param palletNumber java.lang.String
* @param address java.lang.String
* @param rows java.util.Vector
*/
public CoreReturnReportDialog(String shipmentId,String palletNumber,String address, Vector rows,ShipmentIdPalletNumberDialog shipPalletDialog)
{
super();
this.shipmentId = shipmentId;
this.palletNumber = palletNumber;
this.address = address;
this.rows = rows;
shipPalletDialog.dispose();
System.out.println("after initializing the variables ");
System.out.println("size of rows "+this.rows.size());
initialize();
}/**
* This method is invoked when the OK button is pressed.
* On pressing Ok button the dialog box is closed
* Creation Date : 24-October-2001
* @param ae java.awt.event.ActionEvent
*/ /**
* Insert the method's description here.
* Creation date: (10/18/01 8:57:17 PM)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
String shipmentId = "2";
String palletNumber = "4";
String address = "SAFETY EQUIPMENT COMPANY"+"\n"+"191 TALMADGE ROAD 0 "+"\n"+"EDISON NJ-08817";
Vector records = new Vector(2,5);
for(int i = 1; i<=5;i++)
{
Vector record = new Vector(5);
record.addElement("11111");
record.addElement("16 - GRP 16-DASH VALVES");
record.addElement("36 - PP-1 PUSH PULL CONTROL VALVE");
record.addElement("GOOD");
record.addElement(""+1);
records.addElement(record);
//System.out.println("record that has been added " +((Vector)records.elementAt(i)).elementAt(1));
}

/*
Create the frame for Core Return Report Dialog using the four parameter
constructor for shipmentId, palletNumber, address and records for displaying
rows of the table.
*/
System.out.println("before instantiating the object of cor return report dialog ");
CoreReturnReportDialog aCoreReturnReportDialog = new CoreReturnReportDialog(shipmentId,palletNumber,address,records);
System.out.println("after instantiating the object of cor return report dialog ");
/*
Calculate the screen size
*/
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
/*
Center Core Return Report Dialog on the screen
*/
Dimension frameSize = aCoreReturnReportDialog.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
aCoreReturnReportDialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
/*
Making the Core Return Report Dialog visible
*/
aCoreReturnReportDialog.setVisible(true);
}
}
***********************************************

THis is the Table Model Class:
**************************************************
package app.cores.directentrysorting.client.application;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class CoreReturnTableModel extends AbstractTableModel
{
private java.lang.String[] headers = {"CRM No.","Core Group","Device","Grade","Quantity"};
private java.lang.Object[][] data = null;

/**
* Returns the number of columns managed by the data source object. A
* JTable uses this method to determine how many columns it
* should create and display on initialization.
*
* @return the number or columns in the model
* @see #getRowCount
*/
public int getColumnCount() {
return 5;
}
/**
* Returns the name of the column at [I]columnIndex
. This is used
* to initialize the table's column header name. Note, this name does
* not need to be unique. Two columns on a table can have the same name.
*
* @paramcolumnIndexthe index of column
* @return the name of the column
*/
public String getColumnName(int columnIndex) {
return headers[columnIndex];
}

/**
* Returns the number of records managed by the data source object. A
* JTable uses this method to determine how many rows it
* should create and display. This method should be quick, as it
* is call by JTable quite frequently.
*
* @return the number or rows in the model
* @see #getColumnCount
*/
public int getRowCount() {
return rows.size();
}
/**
* Returns an attribute value for the cell at columnIndex
* and rowIndex.
*
* @paramrowIndexthe row whose value is to be looked up
* @paramcolumnIndex the column whose value is to be looked up
* @returnthe value Object at the specified cell
*/
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int row,int col)
{
return false;
}
private java.util.Vector rows = null;
/**
* CoreClassImagesTableModel constructor comment.
*/
public CoreReturnTableModel(java.util.Vector rows)
{
super();
this.rows = rows;
try
{
data = new Object[rows.size()][5];
System.out.println("rows size is "+rows.size());
for(int i = 0;i<rows.size();i++)>
{
for(int j = 0;j<5;j++)
{
data[i][j] = ((Vector)rows.elementAt(i)).elementAt(j);
System.out.println("data["+i+"]["+j+"] = " +data[i][j]);
}
}
}
catch(Exception e)
{
System.out.println("Exception in creating the object of TableModel " +e);
}
}
public void fireTableChanged()
{
setColumnWidths();
}public void fireTableDataChanged()
{
setColumnWidths();
}public TableColumnModel getColumnModel()
{
return new DefaultTableColumnModel();
} public int getColumnWidth(int column)
{
if (column == 0)
{
return 36;
}
if (column == 4)
{
return 10;
}
return 100;
} public void setColumnWidths()
{
System.out.println("set called");
for (int i = 0; i < getColumnModel().getColumnCount(); i ++)
{
TableColumn column = this.getColumnModel().getColumn(i);
column.setPreferredWidth(getColumnWidth(i));
}
}}
****************************************
Thanks in Advance,
Regards,
Daman
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Hi,
No need to go through the same, i was able to achieve the same using setPrefferedWidth() in place of setWidth() in the block of code u mentioned. Thanks again.
Daman
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Hi Daman!
I've found two errors in your code. You wrote both times

but to correct this isn't the solution to your problem.
First remove the columnWidth and fireTableChanged methods out of your table model class 'CoreReturnTableModel', you don't need to do so.
I did a mistake when telling you to use the setWidth() method for sizing the table's columns, sorry!
To correct this replace the setWidth() method calls by setPreferredWidth() method calls in your dialog's method 'getScrollPaneTable()'. It should work then.
Please note that the min width of the table columns is 15. So if you like to set the width to a smaller value, as you did, please take a look at another thread of the swing forum here at javaranch: http://www.javaranch.com/ubb/Forum2/HTML/002780.html
There I described the procedure.
Good luck
Tom
[This message has been edited by Thomas Suer (edited November 09, 2001).]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You can also combine setPreferredSize() with setMinimumSize() if you don't want the size to go below a certain size.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set size of columns in JTable