Swing, Hvordan får jeg et JTextField ind i min JTable
HejJeg er fuldkommen novice indenfor Swing og jeg sidder med en JTable hvor jeg vil have et tekstfelt hvor brugeren kan indtaste en oplysning. Min JTable bliver bygget op v.hj.a. en extension af AbstractTableModel. Når jeg indsætter en Boolean får jeg en checkbox (hvilket er fint )men når jeg indsætter et JTextField får jeg blot vist værdien af JTextField.toString(), hvilket ikke er så fint...
Her er koden jeg benytter:
/*
* Created on 25-11-2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DistributeRuleListPane implements EcwPane
{
private static Logger log = Logger.getLogger(DistributeRuleListPane.class);
private MainEcWebClientFrame mainpage;
public DistributeRuleListPane()
{
}
public void init(MainEcWebClientFrame mainpage)
{
this.mainpage = mainpage;
do_init();
}
protected void do_init()
{
}
public static void main(String[] args)
{
}
public JScrollPane getJTable(DistributeRuleList listofrules)
{
m_table = new JTable();
m_table.setShowGrid(false);
m_table.setBackground(EcwLookAndFeel.getColor(EcwLookAndFeel.FILELISTCOLOR));
m_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(m_table);
scrollPane.setBackground(EcwLookAndFeel.getColor(EcwLookAndFeel.FILELISTCOLOR));
if (listofrules != null)
{
editJTable(listofrules);
}
return scrollPane;
}
protected JTable m_table;
protected RuleStockTableData m_data;
public DistributeRuleList directory;
public void editJTable()
{
editJTable(directory);
}
public void editJTable(DistributeRuleList listofrules)
{
log.debug("editJTable called");
directory = listofrules;
m_data = new RuleStockTableData(listofrules);
m_table.setModel(m_data);
TableColumn column = null;
for (int i = 0; i < RuleStockTableData.m_columns.length; i++)
{
column = m_table.getColumnModel().getColumn(i);
RuleColumnData datainfo = RuleStockTableData.m_columns[i];
column.setPreferredWidth(datainfo.m_width);
if (i <= 1)
{
column.setMaxWidth(datainfo.m_width);
column.setResizable(false);
}
}
m_table.validate();
}
public ArrayList getSelectedFiles()
{
return m_data.getSelectedFiles();
}
}
class RuleFileData
{
private static Logger log = Logger.getLogger(RuleFileData.class);
public String rulename;
public String ruleString;
public String priority;
private SimpleDateFormat formatdate = new SimpleDateFormat("dd-MM-yyyy, hh:mm");
public RuleFileData(DistributeRule rule)
{
log.debug("RuleFileData :" + rule);
rulename = rule.getName();
ruleString = rule.toString();
priority = rule.getPriority();
}
}
class RuleColumnData
{
public String m_title;
public int m_width;
public int m_alignment;
public RuleColumnData(String title, int width,
int alignment)
{
m_title = title;
m_width = width;
m_alignment = alignment;
}
}
class RuleStockTableData
extends AbstractTableModel
{
private static Logger log = Logger.getLogger(RuleStockTableData.class);
static public RuleColumnData m_columns[];
static final public
RuleColumnData m_columnsfiles[] = {
new RuleColumnData("", 1, JLabel.LEFT),
new RuleColumnData("RuleName", 200, JLabel.LEFT),
new RuleColumnData("RuleString", 100, JLabel.RIGHT),
new RuleColumnData("Priority", 50, JLabel.RIGHT)
};
protected SimpleDateFormat m_frm;
protected Vector m_vector;
private String directory;
public RuleStockTableData(DistributeRuleList listofrules)
{
m_vector = new Vector();
m_columns = m_columnsfiles;
setDefaultData(listofrules);
}
public void setDefaultData(DistributeRuleList listofrules)
{
log.debug("setDefalutData called");
m_vector.removeAllElements();
if (listofrules != null && listofrules.size()>0)
{
Collection rulecollection = listofrules.values();
ArrayList rulelist = new ArrayList(rulecollection);
Collections.sort(rulelist);
for (int i = 0; i < rulelist.size(); i++)
{
DistributeRule rule = (DistributeRule)rulelist.get(i);
m_vector.addElement(new RuleFileData(rule));
}
}
}
public int getRowCount()
{
return m_vector == null ? 0 :
m_vector.size();
}
public int getColumnCount()
{
return m_columns.length;
}
public String getColumnName(int column)
{
return m_columns[column].m_title;
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col)
{
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
boolean returning = true;
if (col == 0)
{
returning = true;
}
else
{
returning = false;
}
return returning;
}
public Object getValueAt(int
nRow, int nCol)
{
if (nRow < 0 || nRow
>= getRowCount())
{
return "";
}
Object o = m_vector.elementAt(nRow);
if (o instanceof RuleFileData)
{
RuleFileData row = (RuleFileData) o;
switch (nCol)
{
case 1:
return row.rulename;
case 2:
return row.ruleString;
case 3:
return row.priority;
}
}
return "";
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
boolean DEBUG = true;
public void setValueAt(Object value, int nRow, int nCol)
{
Object o = m_vector.elementAt(nRow);
if (o instanceof RuleFileData)
{
RuleFileData row = (RuleFileData) o;
switch (nCol)
{
case 1:
row.rulename = (String) value;
break;
case 2:
row.ruleString = (String) value;
break;
case 3:
row.priority = (String) value;
break;
}
}
//data[row][col] = value;
fireTableCellUpdated(nRow, nCol);
selectRow(nRow, nCol);
}
private void selectRow(int nRow, int nCol)
{
int numRows = getRowCount();
int numCols = getColumnCount();
log.debug("--------------------------");
/* for (int j=0; j < numCols; j++) {
if(j!=1){
log.debug(getValueAt(nRow,j));
}
if(j==0){
}
}*/
String filename = filename = (String) getValueAt(nRow, 2);
if (((Boolean) getValueAt(nRow, 0)).booleanValue())
{
selectedFiles.add(directory + "\\" + filename);
}
else
{
selectedFiles.remove(directory + "\\" + filename);
}
log.debug(selectedFiles);
log.debug("");
log.debug("--------------------------");
}
ArrayList selectedFiles = new ArrayList();
public ArrayList getSelectedFiles()
{
return selectedFiles;
}
}
