啊,没有人回答,难吗?我已经把JButton加上去了,但不知道如何加这个组合键。诶,自己顶一下!!!

解决方案 »

  1.   

    你可以自己定义一个控件,
    然后用这个控件作TableCellRender
    应该能够完成你的功能吧
      

  2.   

    自己定义很麻烦吧 感觉要继承JComponent的样子
    用GUI设计画一个看能吾能行
      

  3.   

    我个人还是感觉自己定义一个为好,因为如果是JComboBox和JButton的组合件也能画出来吗?
    至于定义,那就肯定要继承JComponent,呵呵,感觉难度也是有的,不然也不用问了?哈哈哈大家帮帮小弟哦,一起加油!
      

  4.   

    ding 
    up
    强烈支持!
      

  5.   

    不用什么组合控件的,直接用JComboBox就可以了,将JComboBox设成允许编辑
      

  6.   

    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);
      

  7.   

    什么?这个样子只能是对JComboBox 可编辑而已,我现在希望的是点击单元格中右边的按钮可以弹出一个对话框在这个里面可以进行选择,同时左边又是可以进行直接输入的文本框。这样举一个例子。比如进行对颜色的选择。我既可以直接输入(例000000)同时也可以点击右边的按钮,弹出一个JColorChooser的Dialog,进行选择。所以上一位仁兄可能误会我的意思了。呵呵呵
      

  8.   


    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);
                }
            }
        }
    }
      

  9.   

    package com.ghca.swing;...public class DialogEditor extends JComponent {    private TextEditor textEditor;    private JButton button;    private AbstractEditorDialog dialog;    public DialogEditor() {
            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;
            }
        }
    }
      

  10.   

    不用这么麻烦吧,你对jcombobox的按钮进行事件监听,然后在事件处理里面弹出窗口就行了啊。
    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();//这个方法里面进行弹出窗口之类的处理,就行了
       }
    }
      

  11.   

    to: Acylas(Acylas)  你说的非常有道理!我也试着作了一下,但这个还有一点问题:1.这个是最主要的,就是在我点击按钮时,他的下啦框也同时出现了,我现在是只要一个JTextField,问题就是如何屏蔽掉那个下啦框!2。是关于外观设置,也就是要把按钮上的下啦框的标志该为“”,这个能实现吗?
    最后不管怎么样都非常感谢楼上各位的仁兄的帮忙,谢谢哦,呵呵呵
      

  12.   

    我重载了一下setNaximumRowCount方法,可是还是没有用,有谁能够提示一下本人,不胜感激哦!
      

  13.   

    做一个panel,里面包括一个JTextField和一个图像Jbutton,button的样子就像下拉框,在button的事件处理里弹出对话框就可以了,这是我做时间控件的方法
      

  14.   

    上楼说的就失去意义了,因为我的最终目的是为了嵌入JTable中,现在的问题是把JComboBox 的多选项去掉就行,呵呵可怜我不知道诶。
      

  15.   

    其实jcobobox就是jlist jpopupmenu,jbutton这几样东西组合而成的,
    你看看jdk里面的BasicComboBoxUI,BasicComboPopup,
    你想要将他变成什么某样都行
      

  16.   

    提示给你了还不行啊。
    象下面这样就可以不用弹出东西并且加...
    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();
                }
            }
    }
      

  17.   

    btnArrow.addActionListener();public void actionPerformed(java.awt.event.ActionEvent event) {
       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.........
                }
            }
      

  18.   

    非常感谢!!!哈哈哈学了不少东西!以后得继续努力了。呵呵呵上楼有msn吗?me 的[email protected],呵呵想今后多跟你多多学习诶!