Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ScrollBar Problem

Posted on 2003-11-28
8
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 80 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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 …
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

610 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