DataModel not showing correctly in JTable

E

ebresie

Okay...this may be a newbie mistake somewhere involving JTables, but I
am perplexed...

I am making a custom dialog, which uses a custom panel I made, which
loads a large quantity of data into a vector, which is used as the data
for a DataModel. I load the data, show my dialog and its contained
panel, but the panel only shows about the first 18 rows of data out of
over 5000 rows.

Using a debugger, the data seems to be contained correctly in the
dataModel (and the vector it was derrived from), but the data only
shows the first few rows.

The scroll bars don't show up either.

I'm thinking I maybe forgetting to implement some listeners, or fire
some events or something, or maybe I'm not handling the Viewport of the
scroll pane, or something, but I'm not sure.

Anyone have any suggestions?

See code belows.

Eric Bresie
(e-mail address removed)
// ----------------------------------------------------

//
// Snippit from MyDialog Code
//
public class MyDialog extends JDialog {
JPanel mainPanel = new JPanel();
MyPanel sPanel = new MyPanel();

public MyDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setupGUI();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}

public MyDialog() {
this(new Frame(), "MyDialog", false);
}

private void setupGUI() throws Exception {
mainPanel.setLayout(borderLayout1);
getContentPane().add(mainPanel);
mainPanel.add(sPanel, java.awt.BorderLayout.CENTER);
}

//
// Snippit from MyPanel Code
//
public class MyPanel extends JPanel {
DefaultTableModel dataModel = new DefaultTableModel();
Vector myData = new Vector();

String[] headers = { "Column1", "Column2" };

Vector columnNames = new Vector(Arrays.asList(headers));
JTable myTable = new JTable(dataModel);
JScrollPane scrollPane = new JScrollPane(myTable);
BorderLayout borderLayout1 = new BorderLayout();
public MyPanel(DefaultTableModel dm) {
dataModel = dm;
myTable = new JTable(dataModel);
try {
setUpGUI();
} catch (Exception exception) {
exception.printStackTrace();
}
}

public MyPanel() {
super();
try {
setupGUI();
} catch (Exception exception) {
exception.printStackTrace();
}
load();
}

private void setupGUI() throws Exception {

this.setLayout(borderLayout1);
scrollPane.setRowHeader(null);
scrollPane.setBorder(BorderFactory.createEtchedBorder());
scrollPane.setPreferredSize(new Dimension(200, 300));
scrollPane.createHorizontalScrollBar();
scrollPane.createVerticalScrollBar();
myTable.setBorder(BorderFactory.createEtchedBorder());
myTable.setMaximumSize(new Dimension(400, 300));
myTable.setMinimumSize(new Dimension(105, 100));
myTable.setPreferredSize(new Dimension(190, 290));
this.setBorder(BorderFactory.createEtchedBorder());
scrollPane.getViewport().add(myTable);
this.add(scrollPane, java.awt.BorderLayout.CENTER);
}

public void load() {
myData = DataSource.getMyData(); // also tried casting it to a
Vector but no luck

dataModel.setDataVector(myData , columnNames);
}
}

//
// Snippet from DataSource code
//
static public Vector getMyData() {
try {
myDataSet = getMyData(); // returns a attribute set

} catch (Exception ex) {
System.out.println("load:Exception:(" + ex + ")");
}

SomeAttributes atts = null;
Vector myData = new Vector(myDataSet.size());


try {
Vector v = null;
for (Iterator i = myDataSet.iterator(); i.hasNext(); ) {
v = new Vector();

atts = ((SomeAttributes) i.next());

String col1 = atts.getAttrValue("col1").toString();
v.add(col1);
String col2 = atts.getAttrValue("col2").toString();
v.add(col2);

myData.add((Vector) v);
}
} catch (Exception ex) {
System.out.println(
"load():(" + ex + ")");
return null;
}
return myData;
}
 
T

Thomas Hawtin

I am making a custom dialog, which uses a custom panel I made, which
loads a large quantity of data into a vector, which is used as the data
for a DataModel. I load the data, show my dialog and its contained
panel, but the panel only shows about the first 18 rows of data out of
over 5000 rows.

Using a debugger, the data seems to be contained correctly in the
dataModel (and the vector it was derrived from), but the data only
shows the first few rows.

The scroll bars don't show up either.
[...]
myTable.setMaximumSize(new Dimension(400, 300));

That's probably your problem at a guess.

In general try to get your example down to the minimum that is still
compilable and demonstrates the problem. There's an awful lot of awful
code to sort through there.

Also don't cross post like that.

Tom Hawtin
 
P

Pete Barrett

scrollPane.getViewport().add(myTable);

I'm not sure what effect this would have. The normal way of adding the
table to the scroll pane would be:

scrollPane.setViewportView(myTable);

It may be that setViewportView has some side effects apart from adding
the table to the scroll pane, such as setting the scroll bars.

Pete Barrett
 
R

Roedy Green

scrollPane.setViewportView(myTable);

Just looking at some of the code I wrote a year or so ago that uses
JTables.

You might want code like this:

jtable.setBackground( Config.TABLE_BACKGROUND );
jtable.setForeground( Config.TABLE_FOREGROUND );
jtable.setPreferredScrollableViewportSize(new Dimension(300,
400));

//Create the scroll pane and add the table to it.
final JScrollPane scrollPane = new JScrollPane( jtable );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top