Frage zu JTable

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo,

kann ich in einem JTable die erste Zeile (nicht Spalte) (oder auch gerne die Überschriftenzeile) mit Selectboxen / Comboboxen füllen? Jedoch nur die erste Zeile.

Hintergrund: Ich bastel mir einen kleinen CSV Importer und der Nutzer soll auswählen welche Spalte in welches Feld der Datenbank übernommen werden soll.
 
Danke für den Link.

Ich habe mir das ganze angesehen und für meine Bedürfnisse abgeändert. Das Problem ist nur ich bekomme weder in der Zeile 1 noch in einer anderen Zeile eine Combobox zu sehen.

Hier mal der Code. Vielleicht kann mir jemand helfen:

Code:
public class CSVReaderCellEditor implements TableCellEditor{
	private final static int      COMBO = 0;
	private final static int    BOOLEAN = 1;
	private final static int     STRING = 2;
	private final static int NUM_EDITOR = 3;
	DefaultCellEditor[] cellEditors;
	JComboBox comboBox;
	int flg;

	public CSVReaderCellEditor() {
		cellEditors = new DefaultCellEditor[NUM_EDITOR];
		comboBox = new JComboBox();
		comboBox.addItem("Spalte 1");
		comboBox.addItem("Spalte 2");
		cellEditors[COMBO]   = new DefaultCellEditor(comboBox);
		/*
		    JCheckBox checkBox   = new JCheckBox();
		    //checkBox.setOpaque( true );
		    checkBox.setHorizontalAlignment(JLabel.CENTER);
		    cellEditors[BOOLEAN] = new DefaultCellEditor(checkBox);
		    JTextField textField = new JTextField();
		    cellEditors[STRING]  = new DefaultCellEditor(textField);
		    flg = NUM_EDITOR;      // nobody
		    */
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		if (row==1) {                       // ComboString
			flg = COMBO;
		    String str = (value == null) ? "" : value.toString();
		    return cellEditors[COMBO].getTableCellEditorComponent(table, str, isSelected, row, column);
		} else if (value instanceof Boolean) {                    // Boolean
		    flg = BOOLEAN;
		    return cellEditors[BOOLEAN].getTableCellEditorComponent(table, value, isSelected, row, column);
		} else if (value instanceof String) {                     // String
		    flg = STRING;
		    return cellEditors[STRING].getTableCellEditorComponent(table, value, isSelected, row, column);
		}
		return null;
	}

	public Object getCellEditorValue() {
		switch (flg) {
			case   COMBO:
				String str = (String)comboBox.getSelectedItem();
		        return str;
		    case BOOLEAN:
		    case  STRING:
		        return cellEditors[flg].getCellEditorValue();
		    default:         return null;
		}
	}

	public Component getComponent() {
		return cellEditors[flg].getComponent();
	}
	
	public boolean stopCellEditing() {
		return cellEditors[flg].stopCellEditing();
	}
	
	public void cancelCellEditing() {
		cellEditors[flg].cancelCellEditing();
	}
		  
	public boolean isCellEditable(EventObject anEvent) {
		return cellEditors[flg].isCellEditable(anEvent);
	}
	
	public boolean shouldSelectCell(EventObject anEvent) {
		return cellEditors[flg].shouldSelectCell(anEvent);
	}
	
	public void addCellEditorListener(CellEditorListener l) {
		cellEditors[flg].addCellEditorListener(l);
	}
	
	public void removeCellEditorListener(CellEditorListener l) {
		cellEditors[flg].removeCellEditorListener(l);
	}
	
	public void setClickCountToStart(int n) {
		cellEditors[flg].setClickCountToStart(n);
	}
	
	public int getClickCountToStart() {
		return cellEditors[flg].getClickCountToStart();
	}
}

Und hier der Table den ich einbinde und mit dem CellEditor verknüpfe:

Code:
table = new JTable(data,new Object[]{"Spalte 1","Spalte 2"});
		table.setCellEditor(new CSVReaderCellEditor());
		
		table.repaint();

Kann ich eigentlich auch den TableHeader mit einer Combobox ersetzen?
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben