public JButton add(final T key, String buttonText) { JButton editButton = new JButton(buttonText); editButton.setBackground(Color.WHITE); // editButton.setOpaque(true); editButton.setUI(new BasicButtonUI()); editButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT); editButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent event) { startEditor(key, (JButton) event.getSource()); } }); ImageIcon icon = new ImageIcon( GraphEditor.class.getResource("/edu/incense/designer/images/remove-icon.png")); JButton removeButton = new JButton(icon); removeButton.setBackground(Color.WHITE); removeButton.setUI(new BasicButtonUI()); removeButton.setSize(ICON_WIDTH, BUTTON_HEIGHT); removeButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent event) { remove(key); } }); JPanel itemPanel = new JPanel(); SpringLayout layout = new SpringLayout(); itemPanel.setLayout(layout); itemPanel.setBackground(Color.WHITE); itemPanel.setBorder(new LineBorder(Color.DARK_GRAY)); itemPanel.add(editButton); itemPanel.add(removeButton); layout.putConstraint(SpringLayout.WEST, editButton, 0, SpringLayout.WEST, itemPanel); layout.putConstraint(SpringLayout.EAST, editButton, 0, SpringLayout.WEST, removeButton); layout.putConstraint(SpringLayout.EAST, removeButton, 0, SpringLayout.EAST, itemPanel); if (items.isEmpty()) removeEmptyMessage(); items.put(key, itemPanel); add(itemPanel); setSize(getPreferredSize()); validate(); // Update container containerWindow.pack(); return editButton; }
protected void addComponents(final EditorTask task) { this.task = task; JsonSurvey jsonSurvey = new JsonSurvey(new ObjectMapper()); final Survey survey = jsonSurvey.toSurvey(task.getExtra("survey")); GroupLayout layout = new GroupLayout(this); setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); GroupLayout.Group hGroup = layout.createParallelGroup(Alignment.CENTER); GroupLayout.Group vGroup = layout.createSequentialGroup(); // First 2 fields are always the same: type and name GroupLayout.Group hLeftGroup = layout.createParallelGroup(); GroupLayout.Group hRightGroup = layout.createParallelGroup(); JLabel typeLabel = new JLabel(mxResources.get("taskType") + ":"); JLabel typeValue = new JLabel(task.getType()); vGroup.addGroup(layout.createParallelGroup().addComponent(typeLabel).addComponent(typeValue)); hLeftGroup.addComponent(typeLabel); hRightGroup.addComponent(typeValue); JLabel nameLabel = new JLabel(mxResources.get("taskName") + ":"); final JTextField nameTextField = new JTextField(10); nameTextField.setText(task.getName()); vGroup.addGroup( layout.createParallelGroup().addComponent(nameLabel).addComponent(nameTextField)); hLeftGroup.addComponent(nameLabel); hRightGroup.addComponent(nameTextField); hGroup.addGroup(layout.createSequentialGroup().addGroup(hLeftGroup).addGroup(hRightGroup)); // Survey specific data ImageIcon icon = new ImageIcon(GraphEditor.class.getResource("/edu/incense/designer/images/add-icon.png")); JButton addButton = new JButton("Add a question", icon); addButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent event) { addItem(); } }); hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(addButton)); vGroup.addComponent(addButton); questionEditor = new QuestionEditor(windowContainer); dynamicList = new DynamicList<Question>(questionEditor, windowContainer); for (Question q : survey.getQuestions()) { dynamicList.add(q.clone()); } JScrollPane scrollPane = new JScrollPane(dynamicList); Dimension d = dynamicList.getPreferredSize(8); d.setSize(d.getWidth() + 50, d.getHeight() + 5); scrollPane.setMaximumSize(d); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); hGroup.addComponent(scrollPane); vGroup.addComponent(scrollPane); JButton okButton = new JButton("Ok"); okButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { saveSurvey(nameTextField.getText(), survey); windowContainer.dispose(); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { windowContainer.dispose(); } }); JPanel buttonsPanel = new JPanel(); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); hGroup.addComponent(buttonsPanel); vGroup.addComponent(buttonsPanel); layout.setVerticalGroup(vGroup); layout.setHorizontalGroup(hGroup); }
private void addItem() { Question question = new Question(); JButton item = dynamicList.add(question); questionEditor.start(question, item); }
public void showEmptyMessage() { if (items.size() == 0) { add(emptyLabel); } }