DataModel not showing correctly in JTable

Discussion in 'Java' started by ebresie@usa.net, Aug 12, 2005.

  1. Guest

    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

    // ----------------------------------------------------

    //
    // 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;
    }
     
    , Aug 12, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 13, 2005
    #2
    1. Advertising

  3. Pete Barrett Guest

    On 12 Aug 2005 14:43:01 -0700, "" <>
    wrote:

    > 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
     
    Pete Barrett, Aug 13, 2005
    #3
  4. Roedy Green Guest

    On Sat, 13 Aug 2005 16:51:37 +0100, Pete Barrett
    <> wrote or quoted :

    >
    >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 );
     
    Roedy Green, Aug 15, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Awah Teh

    SQL Server Datamodel Viewer

    Awah Teh, Oct 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    437
    Awah Teh
    Oct 8, 2003
  2. =?Utf-8?B?Q2FtbWllIFdhdHNvbg==?=

    CSS style sheet not showing correctly on the server

    =?Utf-8?B?Q2FtbWllIFdhdHNvbg==?=, Nov 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    919
    =?Utf-8?B?Q2FtbWllIFdhdHNvbg==?=
    Nov 29, 2005
  3. Abraham Khalil
    Replies:
    0
    Views:
    4,746
    Abraham Khalil
    Sep 17, 2003
  4. Replies:
    0
    Views:
    437
  5. stereotyyppi

    Td - cell not showing correctly in IE

    stereotyyppi, May 29, 2007, in forum: HTML
    Replies:
    7
    Views:
    707
    Andy Dingley
    May 30, 2007
Loading...

Share This Page