javaswingscrollscrollbars

JScrollPane does not add scroll bars to dynamically growing table


I have a table that is growing as the user inputs data. What I am looking to do is to put the table inside some type of container and add a scroll bar which manually shifts the view of the container when it is scrolled. I do no want to use JScrollPane because I have been painstakingly trying to get it to work for the past few days. I just found out that it does not work with a null layout which seems to be the only layout which allows my table to print out normally. Therefore, I am throwing the idea of a JScrollPane out and I am instead trying to implement the idea of programming scroll bars myself which shift the view inside of an overflowing container. So far, I have found nothing on the web that can shift the view of a java container. Any ideas on how I can do this?

UPDATE:

Per recommendation, I am going to try to use a JScrollPane for scrolling. So the new question is: why is the following code not working?

Background: DataTable is a class I designed which extends java.awt.Container and it contains a grid full of JLabels. This class contains methods such as addRow and addCol which increase the size of the container to add cells, and this class also contains methods for setting the values of the labels.

What I am trying to do is place my class inside a JScrollPane with a static width and height which can be scrolled to see the entire table as it grows.

DataTable table = new DataTable(1,4,100,30); //creates table with 1 row (30px tall) and 4 columns (100px wide each)
table.setLocation(0, 0);
JScrollPane scroll = new JScrollPane(table);//scroll pane
scroll.setLayout(null);
scroll.setBounds(10, 50, 500, 100);
scroll.add(table);
Frame frame = new Frame();
frame.add(scroll);
frame.setLayout(null);
frame.setSize(600, 500);
frame.setVisible(true);
for ( int i=0 ; i<20 ; i++ ) {
    table.addRow();//add twenty rows to table
}

When I run this code, no scroll bars show up. Why? And how do I fix this? (Even when I remove the line setting the JSrollPane's layout to null, the code still does not work. In fact, a table does not show up at all).


Solution

  • I have not been able to find the problem, so I instead found an alternate solution using java.awt.ScrollPane. The only change I made to DataTable was that it now extends java.awt.Panel.

    DataTable table = new DataTable(1,4,100,30); //creates table with 1 row (30px tall) and 4 columns (100px wide each)
    //table extends java.awt.Panel and it consists a 2D array of Labels which contain text
    //class has methods for adding rows and cols, and methods for setting values etc.
    table.setLocation(0, 0);
    ScrollPane scroll = new ScrollPane();//scroll pane
    scroll.setBounds(10, 50, 500, 100);
    scroll.add(table);
    Frame frame = new Frame();
    frame.add(scroll);
    frame.setLayout(null);
    frame.setSize(600, 500);
    frame.setVisible(true);
    for ( int i=0 ; i<20 ; i++ ) {
        table.addRow();//add twenty rows to table
    }