Solved

ScrollBar Problem

Posted on 2003-11-28
8
264 Views
Last Modified: 2010-03-31
hi ...
i have a form that has a table...
i wanted to add scroll bars to that table but i don't really know how to do it.
can someone help me please??

this is my code for creating the table :
final String[] colName = { "CPR", "Name", "Address", "Phone", "Sex", "Date OF Birth" };

TableModel pageModel = new AbstractTableModel()
{
public int getColumnCount()
{
return tableData[0].length;
} //getColumnCount

public int getRowCount()
{
return tableData.length;
} //getRowCount

public Object getValueAt(int row, int col)
{
return tableData[row][col];
} //getValueAt

public String getColumnName(int column)
{
return colName[column];
} //getcolName

public Class getColumnClass(int col)
{
return getValueAt(0, col).getClass();
} //getColumnClass

public boolean isCellEditable(int row, int col)
{
return false;
} //isCellEditable

public void setValueAt(String aValue, int row, int column)
{
tableData[row][column] = aValue;
} //setValueAt
}; //pageModel

//-----------------------------------------------------------------------------------------------------
//Create the JTable from the table model:

dataTable = new JTable(pageModel);

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

if (scrollpane != null)
{
scrollpane.setVisible(false);
scrollpane = null;
} //if

scrollpane = new JScrollPane(dataTable);
scrollpane.setVisible(true);

if (inputPanel == null)
makeGUI();
customerPanel.add(scrollpane, BorderLayout.SOUTH);

c.add(tabs);
id.grabFocus();
pack();
repaint();
adupdateTable();
} //try
catch (Exception e)
{
System.out.println("Caught updateTable exception: " + e);
} //catch
} //updatetable

thankxx

0
Comment
Question by:deena18
  • 4
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 9837208
scrollpane = new JScrollPane(dataTable);

is quite right. The scrollbars will only appear if they need to unless you set them to always appear.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9837225
scrollpane = new JScrollPane(dataTable, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

I would use subclass of DefaultTableModel rather than AbstractTableModel as the latter contains a few gotchas.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9837228
Oops - the constants should be the other way around!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 20 total points
ID: 9837540
CEHJ is correct, in that your code would work.  However, I've had problems in the past where the horizontal scroll bar doesn't always appear when you would expect it to (with the table).  Instead of allowing you to scroll using a scroll bar (this is true even if you set it to always show), it will resize all of the columns to fit into the space available (the vertical scroll bar is fine though).

To get around it you can indicate that you do not want your table to resize inside the scroll pane, by using:
        dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
0
 

Author Comment

by:deena18
ID: 9844768
thankx grim_toaster and CEHJ for your solutions :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9845129
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 126
Java Jpanels and Jframe 8 28
Java Options in Zimbra Server 2 46
table example 4 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now