Problème visuel JTable

Fermé
Gamso - 17 juil. 2012 à 15:07
 Gamso - 18 juil. 2012 à 09:13
Bonjour,

je suis actuellement en train de créer une fenêtre permettant d'afficher mes données contenues dans ma TableModel.

J'ai dû définir mes méthodes héritant de TableCellRenderer et de TableCellEditor car chaque ligne d'une même colonne contient un composant différent.

Mon problème est d'ordre visuel. En effet, mes cellules éditables (définie par isCellEditable()) sont toujours encadrés (même avant une première édition).
Voici le rendu visuel : http://www.hostingpics.net/viewer.php?id=663149jtable23.jpg

Avez-vous une idée de comment le résoudre ?

1 réponse

Voici ma classe, en espérant que quelqu'un puisse voir où se trouve mon problème :


public class TypeConfig extends JTable implements ActionListener, MouseListener, Table
{
	/**
	 * @serial Generated value {@value VarConfig#serialVersionUID} <br />
	 */
	private static final long serialVersionUID = -357661060467107577L;

	/**
	 * Menu which appear when right clicking the panel
	 */
	private ClickMenu clickMenu;
	/**
	 * TableModel which contain all data
	 */
	private TableModel tableModel;
	/**
	 * Editable JTable containing tableModel
	 */
	private JTable table;
	/**
	 * Parent frame
	 */
	private Window window;
	/**
	 * TypeDescription
	 */
	private TypeDescription typeDescription;
	/**
	 * Used for combo box for value1value2
	 */
	private JComboBox comboBoxInteger = new JComboBox(new String[]{"value1", "value2"});
	/**
	 * Type of data
	 */
	private String type;
	@Getter
	public boolean isCellEditable(int row, int col) {
		if (col == 2)
			return true;
		else return false;
	}	

	/**
	 * Main constructor<br />
	 * Create a new table from tb and set its ClickMenu
	 * 
	 * @param tb_type TableModel corresponding to TypeDescription
	 * @param typeDescription TypeDescription
	 * @param window Main frame
	 * @param type return the type of data
	 */
	public TypeConfig(TableModel tb_type, TypeDescription typeDescription, Window window, String type)
	{
		super(tb_type);
		tableModel = tb_type;
		this.typeDescription = typeDescription;
		this.window = window;
		this.type = type;
		
		
		//JTable table = new JTable(tb_type);
		
		setFillsViewportHeight(true);


			TableColumn col = getColumnModel().getColumn(0);
			col.setMaxWidth(25);			
					
			getColumnModel().getColumn(2).setCellRenderer(new MyCellRenderer());
			getColumnModel().getColumn(2).setCellEditor(new MyCellEditor());
					
					
		setAutoCreateRowSorter(true);
		clickMenu = new ClickMenu(this);
	}
	
	/**
	 * Modify display to have JCheckbox, JCombobox, JTextField
	 */
	private class MyCellRenderer implements TableCellRenderer {
		
		public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col) 
		{
			if(obj.toString().equalsIgnoreCase("true") || obj.toString().equalsIgnoreCase("false")) {
		    	 JCheckBox cb = new JCheckBox();
		    	 cb.setHorizontalAlignment(SwingConstants.CENTER);
		    	 cb.setBackground(Color.WHITE); 

		    	 if(obj instanceof String)
		    		 cb.setSelected((Boolean.valueOf((String)obj).booleanValue()));
		    	 else if(obj instanceof Boolean)
		    		 cb.setSelected((Boolean)obj);

				 return cb;
		     }
		     if(obj.toString().equalsIgnoreCase("value1") || obj.toString().equalsIgnoreCase("value2")) {
		    	 comboBoxInteger.setSelectedItem(obj);
		    	 return comboBoxInteger;
		     }
		     if(obj == NumberFormat.getNumberInstance()) {
		    	 JTextField tf = new JTextField();
		    	 tf.setText((String)obj);
		    	 return tf;
		     }
		     return new JTextField(obj.toString());
		}
	}
	/**
	 * Modify edition of JCheckbox, JCombobox, JTextField
	 */		
	public class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

		private static final long serialVersionUID = -8869990909839956264L;
		
		private JComponent editor;
			
		
		public MyCellEditor () {}

		@Override
		public Object getCellEditorValue() {
			if (editor != null) {
				if (editor instanceof JCheckBox) {
					return ((JCheckBox) editor).isSelected();
				} else if (editor instanceof JComboBox) {
					return ((JComboBox) editor).getSelectedItem();
				} else if (editor instanceof JTextField)
					return ((JTextField) editor).getText();
			}
			return null;
		}
		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
			int type = 100;
			
			if(value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false"))
				type = 0;
			if(value.toString().equalsIgnoreCase("value1") || value.toString().equalsIgnoreCase("value2"))
				type = 1;
		    if(value == NumberFormat.getNumberInstance()) 
		    	type = 2;

			
			switch( type ) {
			case 0:
				editor = new JCheckBox("",Boolean.parseBoolean(value.toString()));
				((AbstractButton) editor).setHorizontalAlignment(SwingConstants.CENTER);
				editor.setBackground(Color.WHITE); 
				break;
			case 1:
				editor = comboBoxInteger;
				break;
			default:
				editor = new JTextField(value.toString());
				break;
			}
			return editor;
		}
	}
	
	/**
	 * Action event<br />
 	 * For popup menu events :<br />
	 * If the user click on Add, a new row is created and added to the table <br />
	 * Else if he click on Save, the configuration is saved <br />
	 * Else if he click on Delete, the selected row is removed from the table <br />
	 * <br />
	 * @param ae The ActionEvent
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		// Auto-generated method stub
		if (ae.getSource() instanceof JMenuItem)
		{
			JMenuItem item = (JMenuItem)ae.getSource();
			if (item.getText() == "Add")
			{
				if(type.equalsIgnoreCase("integer"))
				{			
						Object data1[] = {"Signed", "false"};
						tableModel.addRow(data1);
						Object data2[] = {"Significant bit", "value1"};
						tableModel.addRow(data2);
						Object data3[] = {"Value significant bit", "1"};
						tableModel.addRow(data3);
				}
			}
			else if (item.getText() == "Save")
			{
				typeDescription.resetDatas(tableModel);
				window.queueEvent(ae, this);
			}
			else if (item.getText() == "Delete" && getSelectedRow() != -1)
			{
				int rows[] = getSelectedRows();
				Arrays.sort(rows);
				int i = rows.length;
				while (--i >= 0)
					tableModel.deleteRow(rows[i]);
			}
			else
				window.queueEvent(ae, this);
		}
	}

	/**
	 * MouseEvent<br />
	 * Show the popup menu if right click <br />
	 * 
	 * @param me the MouseEvent
	 * 
	 * @see MouseEvent
	 */
	@Override
	public void mouseClicked(MouseEvent me) {
		// Auto-generated method stub
		if (me.getClickCount() == 1 && me.getButton() == 3 && me.getSource() instanceof JTable)
			clickMenu.show(me.getComponent(), me.getX(), me.getY());
	}

	/**
	 * Not use
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// Auto-generated method stub
		
	}

	/**
	 * Not use
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// Auto-generated method stub
		
	}

	/**
	 * Not use
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		// Auto-generated method stub
		
	}

	/**
	 * Not use
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// Auto-generated method stub
		
	}
	
	@Getter
	@Override
	public TableModel getTableModel()
	{
		// Auto-generated method stub
		return tableModel;
	}
	
	@Getter
	@Override
	public JTable getJTable()
	{
		// Auto-generated method stub
		return this;
	}
}
0