构建一个swing UI来实现一个方法编辑器 [英] Building a swing UI to implement a method editor
问题描述
我建立一个方法编辑器。
一个方法( MethodModel
class),通常具有以下元素 - < br>
Im building a method editor of sorts.
A method(MethodModel
class), typically, has the following elements --
-
列表< String> inputVariableNames
-
String resultVariableName
List<String> inputVariableNames
String resultVariableName
现在我有一个类 MethodModel
,它们具有上述字段作为其成员。我还有一个类 MethodModelContainer
,其中包含一个列表(LinkedList,按照被调用的方法的顺序有意义) MethodModel
s,是当前文件/类中所有 MethodModel
的集合。
Now i have a class MethodModel
that has the aforementioned fields as its members. I also have a class MethodModelContainer
that contains a list(LinkedList, as order of Methods being called is of significance) of MethodModel
s, a collection of all the MethodModel
within the current file/class.
我试图构建一个摆动UI,借助于此UI可以使用UI编辑一个Method(或编辑一个 MethodModel
对象)。我打算通过为每个 MacroModel
类有三个按钮来实现,每个按钮用于向上和向下箭头,以将方法调用向下移动或向上移动LinkedList和第三个按钮打开一个对话框,用户可以通过该按钮编辑这个特定的 MethodModel
对象的成员。
我想过有一个JTable ,根据 MethodModelContainer
,其中每行将包含1个单元格,其中包含3个按钮用于上述用途,每行将表示 MethodModel
对象。
现在,只要 MethodModelContainer
更新,Jtable就需要更新。
我已经阅读了一些关于如何将JButtons / Jpanels添加到JTable的例子,所有这些都涉及到编写一个自定义单元格渲染器/单元格编辑器,我没有找到一个体面的阅读来理解。
Im trying to build a swing UI by virtue of which a user can edit a Method (or edit a MethodModel
object) using the UI. I plan to do this by having three buttons for each MacroModel
class, 2 buttons each for an 'Up' and 'Down' arrow to shift the method call down or up the LinkedList and a third Button that opens up a Dialog by which the user can edit the members of this particular MethodModel
object.
I had thought of having a JTable, on the basis of the MethodModelContainer
, with rows where each row would have 1 cell containing 3 buttons for the aforementioned uses and each row would signify a MethodModel
object.
Now the Jtable would need to update itself as soon as the MethodModelContainer
is updated.
I have read up some examples on how to add JButtons/Jpanels to a JTable and all of them involve writing a custom Cell Renderer/Cell Editor, something that i have not found a decent read to understand from.
有关如何使用Jtable或其他方式实现我的用例的任何指针请帮助。
Any pointers on how i could implement my use case with using a Jtable or otherwise would be of help.
推荐答案
-
列表< String>输入变量名
每行将有一个单元格包含3个按钮的行
然后myabe这样你可以...,我不是说这只是方式,你可以将JComponents中的每个JComponents分配给JTable中的分隔单元格
then myabe this way you could ..., I'm not talking that this is only way, you can to put every JComponents to the separated Cell in the JTable
import java.awt.*; import java.awt.event.*; import java.util.EventObject; import javax.swing.*; import javax.swing.table.*; public class ComponentTableTest { private JFrame frame; private JTable CompTable = null; private CompTableModel CompModel = null; private JButton addButton = null; public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ComponentTableTest().makeUI(); } }); } public void makeUI() { CompTable = CreateCompTable(); JScrollPane CompTableScrollpane = new JScrollPane(CompTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JPanel bottomPanel = CreateBottomPanel(); frame = new JFrame("Comp Table Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(CompTableScrollpane, BorderLayout.CENTER); frame.add(bottomPanel, BorderLayout.SOUTH); frame.setSize(new Dimension(800, 200)); frame.setLocation(150, 150); //frame.pack(); frame.setVisible(true); } public JTable CreateCompTable() { CompModel = new CompTableModel(); CompModel.addRow(); JTable table = new JTable(CompModel); table.setRowHeight(new CompCellPanel().getPreferredSize().height); table.setTableHeader(null); CompCellEditorRenderer compCellEditorRenderer = new CompCellEditorRenderer(); table.setDefaultRenderer(Object.class, compCellEditorRenderer); table.setDefaultEditor(Object.class, compCellEditorRenderer); return table; } public JPanel CreateBottomPanel() { addButton = new JButton("Add Comp"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source == addButton) { CompModel.addRow(); } } }); JPanel panel = new JPanel(new GridBagLayout()); panel.add(addButton); return panel; } } class CompCellEditorRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor { private static final long serialVersionUID = 1L; private CompCellPanel renderer = new CompCellPanel(); private CompCellPanel editor = new CompCellPanel(); @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { renderer.setComp((Comp) value); return renderer; } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { editor.setComp((Comp) value); return editor; } @Override public Object getCellEditorValue() { return editor.getComp(); } @Override public boolean isCellEditable(EventObject anEvent) { return true; } @Override public boolean shouldSelectCell(EventObject anEvent) { return false; } } class CompTableModel extends DefaultTableModel { private static final long serialVersionUID = 1L; @Override public int getColumnCount() { return 1; } public void addRow() { super.addRow(new Object[]{new Comp(0, 0, "", "")}); } } class Comp { int type; int relation; String lower; String upper; public Comp(int type, int relation, String lower, String upper) { this.type = type; this.relation = relation; this.lower = lower; this.upper = upper; } } class CompCellPanel extends JPanel { private static final long serialVersionUID = 1L; private JLabel labelWith = new JLabel("With "); private JComboBox typeCombo = new JComboBox(new Object[]{"height", "length", "volume"}); private JComboBox relationCombo = new JComboBox(new Object[]{"above", "below", "between"}); private JTextField lowerField = new JTextField(); private JLabel labelAnd = new JLabel(" and "); private JTextField upperField = new JTextField(); private JButton removeButton = new JButton("remove"); CompCellPanel() { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); relationCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { enableUpper(relationCombo.getSelectedIndex() == 2); } }); enableUpper(false); removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JTable table = (JTable) SwingUtilities.getAncestorOfClass(JTable.class, (Component) e.getSource()); int row = table.getEditingRow(); table.getCellEditor().stopCellEditing(); ((DefaultTableModel) table.getModel()).removeRow(row); } }); add(labelWith); add(typeCombo); add(relationCombo); add(lowerField); add(labelAnd); add(upperField); add(Box.createHorizontalStrut(100)); add(removeButton); } private void enableUpper(boolean enable) { labelAnd.setEnabled(enable); upperField.setEnabled(enable); } public void setComp(Comp Comp) { typeCombo.setSelectedIndex(Comp.type); relationCombo.setSelectedIndex(Comp.relation); lowerField.setText(Comp.lower); upperField.setText(Comp.upper); enableUpper(Comp.relation == 2); } public Comp getComp() { return new Comp(typeCombo.getSelectedIndex(), relationCombo.getSelectedIndex(), lowerField.getText(), upperField.getText()); } }
这篇关于构建一个swing UI来实现一个方法编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!