The Artima Developer Community
Sponsored Link

Java Answers Forum
Custom Tree Editor and Tab control problem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Kate Lapan Fox

Posts: 2
Nickname: kfox
Registered: Aug, 2002

Custom Tree Editor and Tab control problem Posted: Sep 27, 2002 4:26 PM
Reply to this message Reply
Advertisement
I have a custom tree editor that contains two jtextfields. When I
edit the first text field and press tab, the cell looses focus. The
desired behavior is to have the focus shift to the second text field
in the cell. I tried adding a key listener and set the focus on the
second cell, but that doesn't work.

Code is attached below:
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
 
/**
 * <p>Title: Trigger Reset Editor</p>
 * <p>Description: Custom editor for specifying the trigger and the
reset.</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Peakstone Corporation</p>
 * @author Kate Lapan Fox
 * @version 2.0
 * @todo tab moves from trigger to reset to stopping editing
 * @todo validate the trigger and reset values (prevent wrong input)
 */
public class TriggerResetEditor extends AbstractCellEditor implements
TableCellEditor {
 
	private Editor editor = new Editor();
 
	/**
	 * Create the trigger reset editor
	 */
	public TriggerResetEditor() {
	}
 
	/**
	 * Get the value from the cell editor
	 *
	 * @return value
	 */
	public Object getCellEditorValue() {
		return editor.getValue();
	}
 
	/**
	 *  Sets an initial <code>value</code> for the editor.  
	 * @param	isSelected	true if the cell is to be rendered with
	 *				highlighting
	 * @param	row     	the row of the cell being edited
	 * @param	column  	the column of the cell being edited
	 * @return	the component for editing
	 */
	public Component getTableCellEditorComponent(JTable table, Object
value,
		boolean isSelected, int row, int column) {
		editor.setValue((String)value);
		return editor;
	}
 
	/**
	 * <p>Title: Editor</p>
	 * <p>Description: Displays two text fields separated by "/".</p>
	 * <p>Copyright: Copyright (c) 2002</p>
	 * <p>Company: Peakstone Corporation</p>
	 * @author Kate Lapan Fox
	 * @version 2.0
	 */
	class Editor extends JPanel {
		JTextField trigger=new JTextField(3), reset=new JTextField(3);
		private static final String SEPARATOR = "/";
		private static final String DISPLAYSEPARATOR = " / ";
		private final TabListener tabListener = new TabListener();
 
		/**
		 * Create a new editor
		 */
		public Editor() {
			setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
			setBackground(Color.WHITE);
			trigger.setToolTipText("Trigger");
			trigger.addKeyListener(tabListener);
			reset.setToolTipText("Reset");
			add(trigger);
			add(new JLabel(DISPLAYSEPARATOR));
			add(reset);
		}
 
		/**
		 * Get the editor value
		 *
		 * @return value
		 */
		public String getValue() {
			return trigger.getText() + SEPARATOR + reset.getText();
		}
 
		/**
		 * Set the editor value
		 *
		 * @param value
		 */
		public void setValue(String value) {
			String triggerValue = "";
			String resetValue = "";
			if (value != null && value.length() != 0) {
				triggerValue = value.substring(0, value.indexOf(SEPARATOR));
				resetValue = value.substring(value.indexOf(SEPARATOR)+1);
			}
			trigger.setText(triggerValue);
			reset.setText(resetValue);
		}
 
		class TabListener extends KeyAdapter {
 
			/**
			 * Invoked when a key has been pressed.
			 *
			 * @param e
			 */
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar() == KeyEvent.VK_TAB) {
					System.out.println("tabpressed");
					trigger.grabFocus();
				}
			}
		}
	}
}

Topic: JSP Previous Topic   Next Topic Topic: browser applet in the browser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use