permaculture playing cards*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Scroll Bar" Watch "Adding Scroll Bar" New topic
Author

Adding Scroll Bar

menwa ms
Greenhorn

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.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
Dirk Schreckmann
Sheriff

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
Greenhorn

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?
thankxx
menwa ms
Greenhorn

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
Bartender

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.


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

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);
}
else
{
// save the new customer:
try
{
//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.setVisible(false);
photoHolder = null;
comments.setVisible(false);
comments = null;
Icon[] custPhotos = {new ImageIcon(photoName)};
JList photosList = new JList(custPhotos);
photosList.setFixedCellHeight(100);
photosList.setFixedCellWidth(80);
photoHolder = new JPanel();
photoHolder.add(photosList);
makeComments();
//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);
pstmt.executeUpdate();
comments.setText(userName+", added.");
}
else
{
String query = " INSERT INTO CUSTOMER (id, name, address, phone, sex, dob) VALUES('"+userId+"', '"+userName+"', '"+userAddress+"', '"+userPhone+"', '"+userSex+"', '"+userDateBirth+"') ";
stat.executeUpdate(query);
comments.setText("Customer saved without a photo.");
}
backPanel.add(photoHolder);
backPanel.add(comments);
updateTable();
} //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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding Scroll Bar