啊,没有人回答,难吗?我已经把JButton加上去了,但不知道如何加这个组合键。诶,自己顶一下!!!
解决方案 »
- 删除时Entity实例时报javax.ejb.EJBTransactionRolledbackException: Removing a detached in
- 怎样将hibernate的sql查询结果集转换为pojo对象结果集
- 分页使用displaytag影响网页速度吗?
- JfreeChart曲线时序图,图例字体调整
- 请大家帮我解决一个小BUG ^_^
- WebService 服务端接收客户端传输的附件不完整,急!!!!!!!!在线等......................
- 初学jsp和servlet有什么好书推荐么??????
- -- 关于Struts页面验证的小问题 ---
- 【ligerui】使用$("form").ligerForm()后select中的内容不显示
- 项目导入后发布tomcat报错
- 关于struts多模块的问题,来者有分,欢迎讨论,解决者另外加分!!!
- ~~~~~~~~100分问两个调用EJB的小问题。希望有经验的兄弟帮我看一下~~~~~~~~~~~~~~~~~~~~~~~~~~~~
然后用这个控件作TableCellRender
应该能够完成你的功能吧
用GUI设计画一个看能吾能行
至于定义,那就肯定要继承JComponent,呵呵,感觉难度也是有的,不然也不用问了?哈哈哈大家帮帮小弟哦,一起加油!
up
强烈支持!
TableColumn column = dcm.getColumn(7);
JComboBox jcmb = new JComboBox();
jcmb.setEditable(true);
DefaultCellEditor editor = new DefaultCellEditor(jcmb);
column.setCellEditor(editor);
import java.awt.Component;
import java.util.EventObject;import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;import com.ghca.swing.DialogEditor;public class DialogTableCellEditor implements TableCellEditor { protected transient ChangeEvent changeEvent; protected EventListenerList listenerList = new EventListenerList(); private JTable table; private DialogEditor dialogEditor; public DialogTableCellEditor(DialogEditor dialogEditor) { if (dialogEditor == null) {
dialogEditor = new DialogEditor();
}
this.dialogEditor = dialogEditor; if (dialogEditor.getDialog() != null) {
dialogEditor.getDialog().addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent evt) {
DialogTableCellEditor.this.setCellEditorValue(
DialogTableCellEditor.this.dialogEditor.getDialog().getValue());
DialogTableCellEditor.this.stopCellEditing();
}
});
}
} public void addCellEditorListener(CellEditorListener listener) {
listenerList.add(CellEditorListener.class, listener);
} public void removeCellEditorListener(CellEditorListener listener) {
listenerList.remove(CellEditorListener.class, listener);
} public Object getCellEditorValue() {
return dialogEditor.getEditor().getText();
} public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
this.table = table; setCellEditorValue(value); return dialogEditor;
} public boolean stopCellEditing() { fireEditingStopped(); if (table != null) {
table.requestFocus();
}
return true;
} public void cancelCellEditing() {
fireEditingCanceled();
} public boolean isCellEditable(EventObject anEvent) {
return true;
} public boolean shouldSelectCell(EventObject anEvent) {
return true;
} public void setCellEditorValue(Object value) {
if (value != null) {
this.dialogEditor.getEditor().setText(value.toString());
} else {
this.dialogEditor.getEditor().setText("");
}
} protected void fireEditingCanceled() {
Object[] listeners = listenerList.getListenerList(); for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i]==CellEditorListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
}
}
} protected void fireEditingStopped() {
Object[] listeners = listenerList.getListenerList(); for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i]==CellEditorListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener)listeners[i + 1]).editingStopped(changeEvent);
}
}
}
}
this(null);
} public DialogEditor(AbstractEditorDialog dialog) { this.textEditor = new TextEditor();
this.textEditor.setBackground(Color.white);
this.textEditor.setBorder(new EditorBorder());
this.textEditor.setAutoscrolls(true);
this.textEditor.setHorizontalAlignment(TextEditor.LEFT);
this.textEditor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
case KeyEvent.VK_HOME:
case KeyEvent.VK_END:
case KeyEvent.VK_ENTER:
case KeyEvent.VK_PAGE_UP:
case KeyEvent.VK_PAGE_DOWN:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_TAB:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
break;
default:
DialogEditor.this.showDialog();
break;
}
}
}); this.button = new JButton();
this.button.setBorder(BorderFactory.createEtchedBorder(1));
this.button.setFocusPainted(false);
this.button.setFocusable(false);
this.button.setIcon(new ImageIcon(
getClass().getResource("icons/Browse.gif"))); this.setLayout(new BorderLayout());
this.add(this.textEditor, BorderLayout.CENTER);
this.add(this.button, BorderLayout.EAST);
this.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_LEFT :
case KeyEvent.VK_RIGHT :
break;
case KeyEvent.VK_ALT : // ?
case KeyEvent.VK_F4 : // ?
case KeyEvent.VK_ESCAPE :
case KeyEvent.VK_HOME :
case KeyEvent.VK_END :
case KeyEvent.VK_ENTER :
case KeyEvent.VK_PAGE_UP :
case KeyEvent.VK_PAGE_DOWN :
case KeyEvent.VK_UP :
case KeyEvent.VK_DOWN :
case KeyEvent.VK_TAB :
if (evt.getSource() == textEditor) {
textEditor.processKeyEvent(evt);
} else {
processKeyEvent(evt);
}
break;
default:
DialogEditor.this.showDialog();
break;
}
}
}); setDialog(dialog);
} public AbstractEditorDialog getDialog() {
return dialog;
} public JButton getButton() {
return button;
} public JTextField getEditor() {
return (JTextField) textEditor;
} public void setDialog(AbstractEditorDialog dialog) {
if (dialog == null) {
return;
}
this.dialog = dialog; this.button.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
if (!DialogEditor.this.dialog.isShowing()) {
DialogEditor.this.showDialog();
}
}
}); this.dialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent evt) {
String buffer = "";
if (DialogEditor.this.dialog.getValue() != null) {
buffer = DialogEditor.this.dialog.getValue().toString();
}
DialogEditor.this.textEditor.setText(buffer);
DialogEditor.this.textEditor.requestFocus();
}
});
} private void showDialog() {
if (dialog == null) {
return;
}
double screenWidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double screenHeight = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int xoffset = new Double(this.getLocationOnScreen().getX()).intValue();
int yoffset = new Double(this.getLocationOnScreen().getY()).intValue();
int width = this.getWidth();
int height = this.getHeight();
int x = xoffset;
int y = yoffset + height;
if (xoffset + dialog.getWidth() > screenWidth) {
x = xoffset + width - dialog.getWidth();
}
if (y + dialog.getHeight() > screenHeight) {
y = yoffset - dialog.getHeight();
}
dialog.setLocation(x, y);
dialog.show();
} public static abstract class AbstractEditorDialog extends JDialog {
public AbstractEditorDialog(JFrame frame) {
super(frame);
}
public AbstractEditorDialog(JFrame frame, String title) {
super(frame, title);
}
public abstract Object getValue();
} protected class TextEditor extends JTextField {
public TextEditor() {
super("", 9);
}
public void processKeyEvent(KeyEvent evt) {
switch (evt.getKeyCode()) {
// 以下是不被响应的键盘事件
case KeyEvent.VK_ALT : // ?
case KeyEvent.VK_F4 : // ?
case KeyEvent.VK_BACK_SPACE :
case KeyEvent.VK_DELETE :
break;
// 以下是以通常的方式响应的键盘事件
case KeyEvent.VK_HOME :
case KeyEvent.VK_END :
case KeyEvent.VK_PAGE_UP :
case KeyEvent.VK_PAGE_DOWN :
case KeyEvent.VK_UP :
case KeyEvent.VK_LEFT :
case KeyEvent.VK_RIGHT :
case KeyEvent.VK_DOWN :
case KeyEvent.VK_ENTER :
case KeyEvent.VK_TAB :
super.processKeyEvent(evt);
break;
// 以下是以弹出对话框的方式响应的键盘事件
default:
evt.consume();
super.processKeyEvent(evt);
break;
}
}
} protected class EditorBorder extends AbstractBorder {
protected Insets editorBorderInsets = new Insets(4, 2, 4, 0);
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
g.translate(x, y);
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(0, 0, w-1, 0 );
g.drawLine(0, 0, 0, h-2 );
g.drawLine(0, h-2, w-1, h-2 );
g.setColor(MetalLookAndFeel.getControlHighlight());
g.drawLine(1, 1, w-1, 1 );
g.drawLine(1, 1, 1, h-1 );
g.drawLine(1, h-1, w-1, h-1 );
g.setColor(MetalLookAndFeel.getControl());
g.drawLine(1, h-2, 1, h-2 );
g.translate(-x, -y );
}
public Insets getBorderInsets(Component c) {
return editorBorderInsets;
}
}
}
DefaultTableColumnModel dcm = (DefaultTableColumnModel)table.getColumnModel();
TableColumn column = dcm.getColumn(7);
JComboBox jcmb = new JComboBox();
jcmb.setEditable(true);
DefaultCellEditor editor = new DefaultCellEditor(jcmb);
column.setCellEditor(editor);
JButton btnArrow=(JButton)jcmb.getComponent(0);
btnArrow.addActionListener();
然后在事件处理里面
public void actionPerformed(java.awt.event.ActionEvent event) {
Object ojbect = event.getSource();
if (object == btnArrow) {
showDialog();//这个方法里面进行弹出窗口之类的处理,就行了
}
}
最后不管怎么样都非常感谢楼上各位的仁兄的帮忙,谢谢哦,呵呵呵
你看看jdk里面的BasicComboBoxUI,BasicComboPopup,
你想要将他变成什么某样都行
象下面这样就可以不用弹出东西并且加...
DefaultTableColumnModel dcm = (DefaultTableColumnModel)table.getColumnModel();
TableColumn column = dcm.getColumn(7);
JComboBox jcmb = new JComboBox();
jcmb.setUI(new UI());
jcmb.setEditable(true);
DefaultCellEditor editor = new DefaultCellEditor(jcmb);
column.setCellEditor(editor);
JButton btnArrow=(JButton)jcmb.getComponent(0);
btnArrow.addActionListener();然后在事件处理里面
public void actionPerformed(java.awt.event.ActionEvent event) {
Object ojbect = event.getSource();
if (object == btnArrow) {
showDialog();//这个方法里面进行弹出窗口之类的处理,就行了
}
}
//
class UI extends javax.swing.plaf.basic.BasicComboBoxUI {
protected JButton createArrowButton() {
return new JButton("...");
} public void configureArrowButton() {
if ( arrowButton != null ) {
arrowButton.setEnabled( comboBox.isEnabled() );
arrowButton.setRequestFocusEnabled(false);
arrowButton.resetKeyboardActions();
}
}
}
Object ojbect = event.getSource();
if (object == btnArrow) {
showDialog();//这个方法里面进行弹出窗口之类的处理,就行了
}
}
上面的事件处理可以改在UI里面实现
public void configureArrowButton() {
if ( arrowButton != null ) {
arrowButton.setEnabled( comboBox.isEnabled() );
arrowButton.setRequestFocusEnabled(false);
arrowButton.resetKeyboardActions();
arrowButton.addAction.........
}
}