Solved

ScrollBar Problem

Posted on 2003-11-28
8
271 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 121
replace a word with other 1 43
check java version using powershell 13 80
login form jsp example 2 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

815 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

8 Experts available now in Live!

Get 1:1 Help Now