Horizontal Scrollbar not visible on JTable

M

Mirela

Hi!!


I have a problem concerning the display of the Horizontal ScrollBar on
a Jtable. It may seem silly, but there is no way I can get it visible
and active.
So, I have a frame, that contains a tree on the left and a table on
the right. The constructor of my class is posted at the end of the
message.
What exactly is it that I do wrong???? The scollbar is "visible", but
inactive. And the columns of the table can not be scrolled.
The thing is the Vertical scroll bar has always been displayed without
doing anythig. Just using the default constructor ( JScollPane
(myJTable) ); But never the Horizontal one..

WHY!!!

Here is the code where the table is defined, as well as its scollpane,
the frame, the tree, etc ..

Thanks for ANY ides on the subject!!

Mirela

.... imports ...

public class emjrcf extends JFrame implements dm.include.Rcf,
dm.include.ii, TableModelListener {

private static JTree tree;
private DefaultTreeModel treeModel;
private String lineStyle = "Angled";

private static JSplitPane splitPane = null;
private static JTable myJTable;
private static JScrollPane myTableJScrollPane = null;
private JScrollPane myTreeScrollPane = null;

private PopupMenu myPopupMenu = null;
private Ma ma = null;
private Tsl tsl = null;

private static DefaultMutableTreeNode top = null;
private DefaultMutableTreeNode selectedNode = new
DefaultMutableTreeNode ();

.. other declarations ...

public emjrcf(String title, String server ) {

super("emjrcf " + title);

// create an arg to set the name of the root node through an ArgInfo
object
DriArg topArg = new DriArg();

try {
topArg.set(rcfResNameId, rcfResNameType, server);
} catch (iiException e) {
System.out.println("Exception is : " + e.getMessage());
}

/* for the construction of the tree */
Ptable formatTable = getTreeFormat();

ArgInfo ai = new ArgInfo(topArg) ;
ai.paramTable = formatTable ;
top = new DefaultMutableTreeNode(ai);
top.setUserObject(ai);

createNodes(top, formatTable);

//Create a tree that allows one selection at a time.
treeModel = new DefaultTreeModel(top);
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

// Listen for the right click to show popup
myPopupMenu = new PopupMenu(this);
myPopupMenu.createPopupMenuRoot();
MouseListener popupListener = myPopupMenu.popupListener;


//Listen for when the selection changes.
tree.addTreeSelectionListener(new Tsl(tree, this));
tree.addMouseListener(new Ma(tree, this));
tree.addMouseListener(popupListener);

//Create the scroll pane and add the tree to it.
myTreeScrollPane = new JScrollPane(tree);

tableModel = new TableModel("Root View ");
tableModel.addTableModelListener(this);
tableModel.hookTree = this;

/* _____________________________________
here is the creation of the table
___________________________________*/

myJTable = new JTable(tableModel);
myJTable.setPreferredScrollableViewportSize( new Dimension(100,
50));

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;

myTableJScrollPane = new JScrollPane(myJTable, v, h);

//Add the scroll panes to a split pane.
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(myTreeScrollPane);
splitPane.setRightComponent(myTableJScrollPane);

Dimension minimumSize = new Dimension(100, 50);

myTableJScrollPane.setMinimumSize(minimumSize);
myTreeScrollPane.setMinimumSize(minimumSize);
splitPane.setDividerLocation(200);

splitPane.setPreferredSize(new Dimension(500, 300));

createMenu();

//Add the split pane to this frame.
getContentPane().add(splitPane, BorderLayout.CENTER);
setRootView();

}
 
I

Ike

The horizontal scrollbar is not showing up because the default resize mode
of the JTable is set to
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS. This means that when you adjust a
particular column's width, the JTable will automatcially adjust the width of
all columns to the right of this column, so that the *total* table width
remains the same, and the subsequent column widths have been decreased to
the point you cannot view them. But all table columns are still within the
view port, therefore, the horizontal scrollbar is not being displayed.

To get the horizontal scrollbar to show up when you adjust the column
widths,
use the JTable.setAutoResizeMode() method and set its value to
JTable.AUTO_RESIZE_OFF, as folloows:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

//Ike
 
M

Mirela

Thanks alot, I had done this already.

The problem is that the table has a number of columns that can vary
from 1 to n (many!), and when I did set the AUTO_RESIZE to OFF, the
scrollbar did become visible, if there were more then 3 columns. If
there were 1, 2 or 3, the columns had a fixed size and the rest of the
view was empty. The columns are not adjusted to take the whole size of
the table view (which is normal, because the resizing is off!). So the
trick I found is to check the number of columns and if there are more
then 3, I set AUTO_RESIZE to OFF, which makes the scrollbar vizible
and functional, otherwise I set it to RESIZE_LAST_COLUMN... This way I
do have the scrollbar when there is a large number of columns, and
when there are only a few, there is no scrollbar but the view is not
completely screwed... But I would like to know which is the "clean"
way to handle this.

I found an explanation though, a few days ago. Somebody said on this
forum that if the AUTO_RESIZE is set to OFF, each column has the size
fixed to 70, which would explain why the scrollbar becomes visible
after the fourth column (4x70 = 280, which is bigger then the
dimension of my table, set to 250). It seems to be a possible
explanation ..

Anyway, the view is pretty OK with this trick, but I still need some
help on that column resizing problem. I did try the setPrefferedSize()
trick, nothing is result... :

TableColumn column = null;

for (int i = 0; i < tableModel.columnNames.length; i++) {
column = myJTable.getColumnModel().getColumn(i);
column.setPreferredWidth(100);
column.setWidth(100);
}

And some other tricks that were supposed to give some result, all
found on this forum.. People having teh same kind of problems ... The
only thing that worked was setting different values on the
AUTO_RESIZE, upon the number of columns..

Any other ideas...?


thanks,

Mirela
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top