Solved

ScrollBar Problem

Posted on 2003-11-28
8
256 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 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

26 Experts available now in Live!

Get 1:1 Help Now