File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Adding Scroll Bar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Scroll Bar" Watch "Adding Scroll Bar" New topic

Adding Scroll Bar

menwa ms

Joined: Nov 14, 2003
Posts: 12
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 = null;
} //if
scrollpane = new JScrollPane(dataTable);
if (inputPanel == null)
customerPanel.add(scrollpane, BorderLayout.SOUTH);
} //try
catch (Exception e)
System.out.println("Caught updateTable exception: " + e);
} //catch
} //updatetable
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
What exactly is the problem, is the scrollbar not showing up correctly or not at all. I seem to recall that tables do funny things with JScrollPanes, something to do with the size of one or the other not being determined correctly. Unfortunately, I can't be of much real help right now but maybe that will give you an idea of something to look at for the source of the problem, I know I saw it disussed in a thread on this forum within the last few month.
menwa ms

Joined: Nov 14, 2003
Posts: 12
the problem is that the scroll bar is not showing at all.. so when i add users to the table they will be added but because there are many data in the tables so i will not be able to see but has been added it goes below the page,,,
is my code correct?
menwa ms

Joined: Nov 14, 2003
Posts: 12
hi ...
no one yet had replied i really need someone help , everything is working but i'm stuck with this part..

plzz help
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Can you post the code where you are adding new data to the table? I didn't see anything in the code you posted that would prevent the scrollbars from showing up.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
menwa ms

Joined: Nov 14, 2003
Posts: 12
hi ,,,
this is the code for the add button:
public void actionPerformed(ActionEvent e)
FileInputStream fis = null;
if (e.getSource() == add) //The ADD button.
//User has not populated all the input fields.
if(name.getText().equals("")|| address.getText().equals("")|| phone.getText().equals("")|| sex.getText().equals("")|| dob.getText().equals("")|| photo.getText().equals(""))
JOptionPane.showMessageDialog(null, "Please fill in all the fields","Missing Fields",JOptionPane.INFORMATION_MESSAGE);
// save the new customer:
//1. take the customer's data and photo:
int userId= Integer.parseInt(id.getText());
String userName = name.getText();
String userAddress = address.getText();
String userPhone = phone.getText();
String userSex = sex.getText();
String userDateBirth = dob.getText();
String photoName = photo.getText();
File file = new File(photoName);
int fileLength = (int)file.length();
//2. Set the user's photo into the photoHolder:
photoHolder = null;
comments = null;
Icon[] custPhotos = {new ImageIcon(photoName)};
JList photosList = new JList(custPhotos);
photoHolder = new JPanel();
//3. Insert the data and photo into the database:
if(fileLength > 0)
fis = new FileInputStream(file);
String query = " INSERT INTO CUSTOMER VALUES('"+userId+"', '"+ userName+ "', '"+ userAddress+ "', " +" '"+ userPhone+ "', '"+ userSex+ "', '"+ userDateBirth+ "', ? ) ";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBinaryStream(1, fis, fileLength);
comments.setText(userName+", added.");
String query = " INSERT INTO CUSTOMER (id, name, address, phone, sex, dob) VALUES('"+userId+"', '"+userName+"', '"+userAddress+"', '"+userPhone+"', '"+userSex+"', '"+userDateBirth+"') ";
comments.setText("Customer saved without a photo.");
} //try
catch (Exception ee)
//The danger of putting creating the JOptionPane in here is that it will show the same message regardless of the error.
JOptionPane.showMessageDialog(null, "Customers CPR already exits!!Please enter another CPR","Invalid",JOptionPane.INFORMATION_MESSAGE);
System.out.println("Caught exception in add action: " + ee);
} //catch
} //if
}//add button
I agree. Here's the link:
subject: Adding Scroll Bar