• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rendering 1+JTable in same JPanel

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can display the JTable in a JPanel, but in my program I need to allow the user to search on different names, each time rendering a different JTable in the same JPanel. I can specify a new JTable, but it is 'layered' on top of any previous JTable(s) which really confuses the JVM. Is there any way of completely deleting a JTable before rendering a new one?
The relevant code is below. Thanks in advance for any help!
public void actionPerformed (ActionEvent e)
{
JTable table;
if (e.getSource() == search)
{
searchString = searchCriteria.getText();
searchCriteria.setText("");
results.setText("");
if (searchString.length() == 0)
{
results.setText("Please enter search details");
}
else
{
Object[][] data = SearchPhoneList1.objectArrayFiller();
if (data.length == 0)
{
results.setText("No matches");
}
else
{
String[] columnNames = {"Ext", "DDI", "Surname", "First Name", "Title", "Room", "Dept/Prog", "Description"};
table = new JTable(data, columnNames);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
controlPanelBottom.setLayout(new BorderLayout());
TableColumn zero = table.getColumn(columnNames[0]);
TableColumn one = table.getColumn(columnNames[1]);
TableColumn two = table.getColumn(columnNames[2]);
TableColumn three = table.getColumn(columnNames[3]);
TableColumn four = table.getColumn(columnNames[4]);
TableColumn five = table.getColumn(columnNames[5]);
TableColumn six = table.getColumn(columnNames[6]);
TableColumn seven = table.getColumn(columnNames[7]);
int zeroWidth = getPreferredWidthForColumn(zero, columnNames[0], table);
int oneWidth = getPreferredWidthForColumn(one, columnNames[1], table);
int twoWidth = getPreferredWidthForColumn(two, columnNames[2], table);
int threeWidth = getPreferredWidthForColumn(three, columnNames[3], table);
int fourWidth = getPreferredWidthForColumn(four, columnNames[4], table);
int fiveWidth = getPreferredWidthForColumn(five, columnNames[5], table);
int sixWidth = getPreferredWidthForColumn(six, columnNames[6], table);
int sevenWidth = getPreferredWidthForColumn(seven, columnNames[7], table);
zero.setMinWidth(zeroWidth);
zero.setMaxWidth(zeroWidth);
one.setMinWidth(oneWidth);
one.setMaxWidth(oneWidth);
two.setMinWidth(twoWidth);
two.setMaxWidth(twoWidth);
three.setMinWidth(threeWidth);
three.setMaxWidth(threeWidth);
four.setMinWidth(fourWidth);
four.setMaxWidth(fourWidth);
five.setMinWidth(fiveWidth);
five.setMaxWidth(fiveWidth);
six.setMinWidth(sixWidth);
six.setMaxWidth(sixWidth);
seven.setMinWidth(sevenWidth);
seven.setMaxWidth(sevenWidth);
table.sizeColumnsToFit(0);
//table.revalidate();
//repaint();
JScrollPane JSP = new JScrollPane(table);
controlPanelBottom.add(JSP);
contentPane.add(controlPanelBottom, "Center");
contentPane.validate();
}
}
String errors = SearchPhoneList1.getErrormessages();
searchCriteria.setText(errors);
}
else if (e.getSource() == clear)
{
searchCriteria.setText("");
results.setText("");
JPanel controlPanelBottomNew = new JPanel();
controlPanelBottomNew.setBorder(BorderFactory.createTitledBorder("Search Results"));
controlPanelBottomNew.revalidate();
repaint();
contentPane.add(controlPanelBottomNew, "Center");
contentPane.validate();
}
}
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing...
 
reply
    Bookmark Topic Watch Topic
  • New Topic