I'm working on a Java program that reads text files, creates the appropriate number of panels according to the text files, and saves any changes made. I have everything but the saving down and it's confusing me.
I've done several web searches but all I get are "saving is trivial, you should be able to do it", "create your own table model", and "I have no clue". The first example is what I get most of the time.
Here's the code snippet (the JTable gets placed into a JPanel which is then added to a JTabbedPane):
[code]
//---dynamically create button entries
for (int i = 0; i < screenParse.getButtons().size(); i++)
{
JPanel temp = new JPanel(); // create a new panel
TableModel jTable1Model = // create a new table
new DefaultTableModel(
screenParse.getButtons().get(i).tableize(), // get the properties of the button
new String[] { "Column 1", "Column 2" })
{
public boolean isCellEditable(int row, int column)
{
if (column == 0) // make the left side uneditable.
{
return false; // can't edit leftmost cells
}
else
{
return true; // rest of cells can be edited
}
}
};
JTable tempTableProperties = new JTable(); // create the new JTable
tempTableProperties.setModel(jTable1Model);
tempTableProperties.setPreferredSize(new java.awt.Dimension(381,435));
temp.add(tempTableProperties); // add the table to the panel
//temp.setName(screenParse.getButtons().get(i).getName()); // doesn't work for setting the tab me when scrolling
buttonPanelNames.add(screenParse.getButtons().get(i).getName()); // add the name to the names list
buttonPanels.add(temp);
}
//---End button entry creation
[/code]
I tried creating a custom table model, but I don't know how to get to the table to access the model's methods. Do I have to declare the table model outside of the method instead of inside and access it that way?
I can't seem to to the table after this point either. I can access the JTabbedPane the JPanel is in, but I only get Components back, not JPanel (superclass back only).
Does anyone know how I can save the contents of the JTable? Getting access to each cell individually would be ideal, that way I could format the output.
[url]http://java.sun.com/javase/6/docs/api/javax/swing/JTable.html[/url]
To get data from a cell, you want this method specifically:
[url]http://java.sun.com/javase/6/docs/api/javax/swing/JTable.html#getValueAt(int,%20int[/url])
public Object JTable.getValueAt(int row, int column)
[editline]07:24PM[/editline]
To save the contents, if they're Strings you just write them to a text file.
So I should cast the Component received from JTabbedPane.getContainer(0) as a JPanel and cast whatever I can extract from that as a JTable? I'll give it a shot.
I can't get access to the JTable, that is my problem. I apologize if that wasn't clear.
EDIT:
Got it. Casting did the trick, thanks.
Sorry, you need to Log In to post a reply to this thread.