aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable - JDBC - Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - JDBC - Vectors" Watch "JTable - JDBC - Vectors" New topic
Author

JTable - JDBC - Vectors

Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
Can anyone tell me of anywhere that i can find a good example of how to populate a JTable with data from a database using a vector.
I'd like to ask a more specific question about my code but i'm not sure where it's not working, and there's about 6-7 hundred lines of code. But if anyone's brave enough to read all of it, let me know and i'll post it.


Pete<br />"Reality is an illusion <br />brought on by a lack of <br />drink, drugs and smut"
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Hi Peter,
In my project, I have several tables to display the data which I retrieved from the database. I have 2 type of tables. In one table each row is a vector. This kind of table is to display data only. Another type of table each row is an object. If you don't mind, post your code and let us take a look.
Sincerely
Renee
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
Ok, here's all of the code:

Hope that u can find what's wrong with it.
[ April 15, 2002: Message edited by: Peter Phung ]
[ April 15, 2002: Message edited by: Peter Phung ]
[ April 15, 2002: Message edited by: Peter Phung ]
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
I have found that my table is querying the database and displaying results properly, but the column names are not appearing as i wanted. The column names appear as A, B, etc.. Can anyone tell me how to change this.
Also my table does not appear wide enough to show the values in the cells. Can someone point me in the right direction on this one too.
Thanks in advance for any help
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Hi Peter,
If your table column names appear as A, B, etc, that's because you didn't override getColumnNames in your talbeModel.Please add public String getColumnName(int col) {
return m_columns[col];
} to your table model.
I have no idea why
for (int k = 0; k < ResultTableData.m_columns.length; k++) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(ResultTableData.m_columns[k].m_alignment);
TableColumn column = new TableColumn(k, ResultTableData.m_columns[k].m_width, renderer, null);
resTable.addColumn(column);
}
is not working. How about try another way which is working in my project.
for (int k = 0; k <
TableColumn column = resTable.getColumnModel().getColumn(k);
column.setPreferredWidth(ResultTableData.m_columns[k].m_width);
column.setMaxWidth(ResultTableData.m_columns[k].m_width);
column.setMinWidth(ResultTableData.m_columns[k].m_width);
}
resTable = new JTable() {
public TableCellRenderer getCellRenderer(int row, int col) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(ResultTableData.m_columns[col].m_alignment);
return renderer;
}
};
Good luck!
Renee
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Sorry it should be
resTable = new JTable(t_data) {
public TableCellRenderer getCellRenderer(int row, int col) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(ResultTableData.m_columns[col].m_alignment);
return renderer;
}
};
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
I've tried adding

like u suggested, but i get this compile error :

The other part of code works fine. Thank you.
Any further help that u can offer would be appreciated.
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
I've solved the problem now by adding .m_title to the code :

Thanks Renee for your help
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
Does anyone know how to make a Table scrollable?
I've tried adding the table to a scrollpane but it doesn't work.
The code that i have so far is :

I've also tried adding the table to the scrollpane using a seperate command after instantiating the scrollpane.
Please help because this is really confusing me
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

What do you mean by "it doesn't work"?

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
I mean that looking at the display, the table is obviously bigger than the viewable area, but there are no scrollbars.
I've tried to use the line
sp.getViewport().add(resTable);
after calling the constructor, but i get the same result. I've had to change the preferred size of the columns to take account of this, but this causes the table to truncate the column names. If u have any suggestions, i would be grateful
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Not sure why you aren't getting the default behavior...

Here's some code that shows the usual way to use a table in a scroll pane... ( also, table.setPreferredScrollableViewportSize() is usually better than calling scrollpane.setPreferredSize()... ) Hopefully this will help you out some...



-Nate
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
Thanks for the help Nate, but i've come up with another problem.
I've got my code compiling and running now but i'm unsure of how to add my table to the rest of my gui.
THe code i have so far is :

normally i would use a line like
container.add(component);
But i've been unsuccessful in my attempts so far.
Please could u suggest a way of adding the table to my container.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

For adding a component to a JScrollPane after it is constructed you would call scollPane.setViewportView( component )...

-Nate
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
What i actually meant was,
my scrollpane is created using the method
getTableScrollPane(String params)
I don't know wether or not the table has been placed in the scrollpane because the scrollpane does not appear in my GUI.
All code relating to this was in my last post. Please can you help
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Peter Phung:
the table is obviously bigger than the viewable area, but there are no scrollbars.

JScollPane = ViewPort + ScrollBars
If you use JScrollPane, the scrollbars are included. If you could parse the resultset into the arrays as required in the code example below, using JTable is quite convenient:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Java {
public static void main(String args[]) {
String tableName = "Coffee Price List";
Object[] columnNames =
{"Brand", "Supplier", "Price", "Origin"};
Object[][] tableData = {
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
};
new Display(tableName, columnNames, tableData );
}
}

class Display {
JFrame window = new JFrame();
String title;
String text = "";

public Display(String tableName, Object[] columnNames, Object[][] tableData) {
JTable table = new JTable(tableData, columnNames);
JScrollPane scrollableTable = new JScrollPane(table);
JFrame mainWindow = new JFrame(tableName);
mainWindow.setContentPane(scrollableTable);
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
mainWindow.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = mainWindow.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frameSize.height = 35 * tableData.length;
if (frameSize.height > 300) frameSize.height = 300;
mainWindow.setSize(frameSize.width, frameSize.height);
mainWindow.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
mainWindow.setVisible(true);
}

}
[ April 19, 2002: Message edited by: Doanh Nguyen ]
[ April 19, 2002: Message edited by: Doanh Nguyen ]
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
Doanh,
How do i parse the data into arrays like you suggested?
All of my data is in a vector populated by objects of type TableData.
 
 
subject: JTable - JDBC - Vectors