Java Swing-在JTable中嵌入Jbutton的Jpanel [英] Java Swing -- Jpanel with Jbuttons embedded within a JTable

查看:504
本文介绍了Java Swing-在JTable中嵌入Jbutton的Jpanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swing中构建一个Ui,其中我的要求是在JTable中包含JPanes.这些JPanes中将包含JButton.
我的用例如下-
我正在编写一个MethodEditor,其中我提供了一个UI来在提供的文件中存储方法.用户界面还允许在单击按钮时编辑传递给方法的参数.
每个单一方法都具有如下UI表示形式-

Im building a Ui in Swing wherein my requirement is to have JPanes within a JTable. These JPanes will have JButtons within them.
My use case is as follows --
Im writing a MethodEditor wherein im providing a UI to store the methods within a supplied file. The UI would also allow editing the parameters being passed to the method on the click of a button.
Every single method would have a UI representation as follows --

我对Method类的基本表示如下-

My basic representation of the Method class is as follows --

public Class Method {

String methodName;
List<String> InputVariableNames;
String OutputVariableName;
}

现在我有一个Method对象列表,List<Method> methodList是我要基于JTable的对象. 此List包含在MethodModel类中,如下所示-

Now i have a list of Method objects, List<Method> methodList on which i want to base my JTable. This List is contained in a MethodModel class as follows --

public class MethodModel {
   List<Method> methodModel;
}

我问了一个问题之前,我的代码基于此处提供的答案.
但是我的代码似乎不起作用.我的代码如下-

I had asked a question earlier and have based my code on the answer provided there.
My code however does not seem to be working. My code is as follows --

public class MethodEditor extends JTable {


    private static final long serialVersionUID = 1L;

    private MethodEditorModel model ;
    private MethodCellRenderer cellRenderer;

    public MethodEditor(MethodModel bean) {
        setRowHeight(25);
        this.setPreferredSize(new Dimension(500, 500));
        model = new MethodEditorModel(bean);
        this.setModel(model);
        setupComponent();
    }

    private void setupComponent() {
        cellRenderer = new MethodCellRenderer();
        this.setDefaultRenderer(Object.class,cellRenderer);
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }


    private static class MethodEditorModel extends DefaultTableModel implements PropertyChangeListener {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;    
        private MethodModel bean;

        public MethodEditorModel(MethodModel bean) {
            this.bean = bean;
            bean.addPropertyChangeListener(this);
        }


        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            fireTableDataChanged();

        }

    }

    private static class MethodCellRenderer implements TableCellRenderer {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private MethodEditorCellPanel renderer = new MethodEditorCellPanel();


        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            MethodModel methodModel = (MethodModel)value;
            for(Method method : methodModel.getMethodList()) {
                renderer.setComponents((Method) method);
            }
            return renderer;
        }

    }

    private static class MethodEditorCellPanel extends JPanel implements ActionListener {

        private static final long serialVersionUID = 1L;
        private JButton upButton;
        private JButton downButton;
        private JButton methodDetailsButton;
        private Method method;

        public MethodEditorCellPanel() {
            upButton = new JButton("Up");
            downButton = new JButton("Down");
        }

        public void setComponents(Method method)
        {
            this.method = method;
            methodDetailsButton = new JButton(method.getMethodName());

            upButton.addActionListener(this);
            downButton.addActionListener(this);
            methodDetailsButton.addActionListener(this);

            Box verticalBar =  Box.createHorizontalBox();
            verticalBar.add(upButton);
            verticalBar.add(Box.createHorizontalStrut(15));
            verticalBar.add(methodDetailsButton);
            verticalBar.add(Box.createHorizontalStrut(15));
            verticalBar.add(downButton);
            add(verticalBar);
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            if(evt.getSource().equals(downButton)) {

            }

            if(evt.getSource().equals(upButton)) {

            }

            if(evt.getSource().equals(methodDetailsButton)) {


            }
        }

    }

}

代码可以编译,但是JTable没有显示. 任何有关我可能在做错事情的指示都会有很大帮助.

The code compiles but the JTable does not show up. Any pointers on what i may be doing wrong would be of great help.

推荐答案

不要在JTable中包含其他组件.更不用说组件和其他多个组件了.原因是JTable 不会将鼠标事件传递到其单元格.因此,即使您在JTable中有按钮,也必须注意通过以下方式自行按下按钮:

Don't include another components to JTable. Let alone components with multiple other components. The reason is that JTable won't pass mouse events to its cells. So even when you have buttons inside JTable, then you would have to take care about pressing them by yourself, by:

  • 获取被点击的单元格
  • 获取准确的坐标
  • 将这些坐标外推到内部组件
  • 手动调用相应按钮.

即使那样,您也不会获得按钮动画和内容.

And even then you won't get button animation and stuff.

如果需要将组件排列到表中,请将JPanel与GridLayout或GridBagLayout一起使用.

If you need to arrange components into a table, use JPanel with GridLayout or GridBagLayout.

这篇关于Java Swing-在JTable中嵌入Jbutton的Jpanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆