aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Rendering 1+JTable in same JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rendering 1+JTable in same JPanel" Watch "Rendering 1+JTable in same JPanel" New topic
Author

Rendering 1+JTable in same JPanel

Jon Davies
Greenhorn

Joined: Nov 21, 2003
Posts: 6
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();
}
}
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Moving to Swing...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rendering 1+JTable in same JPanel
 
Similar Threads
JTable ........
Change Text for JButton rendered in a JTable
jtable
JTable switching
JTable problem