public
Created

  • Download Gist
Entry.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 
package dynamicfields;
 
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
/**
*
* @author ndunn
*/
public class Entry extends JPanel {
private JComboBox comboBox;
private JTextField textField;
private JButton plus;
private JButton minus;
private EntryList parent;
 
public Entry(JComboBox comboBox, String textFieldText, EntryList list) {
this.comboBox = comboBox;
this.parent = list;
this.plus = new JButton(new AddEntryAction());
this.minus = new JButton(new RemoveEntryAction());
this.textField = new JTextField(10);
this.textField.setText(textFieldText);
add(this.plus);
add(this.minus);
add(this.comboBox);
add(this.textField);
}
 
public JComboBox getComboBox() {
return comboBox;
}
 
public class AddEntryAction extends AbstractAction {
 
public AddEntryAction() {
super("+");
}
 
public void actionPerformed(ActionEvent e) {
parent.cloneEntry(Entry.this);
}
 
}
 
public class RemoveEntryAction extends AbstractAction {
 
public RemoveEntryAction() {
super("-");
}
 
public void actionPerformed(ActionEvent e) {
parent.removeItem(Entry.this);
}
}
 
public void enableAdd(boolean enabled) {
this.plus.setEnabled(enabled);
}
public void enableMinus(boolean enabled) {
this.minus.setEnabled(enabled);
}
}
EntryList.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 
package dynamicfields;
 
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JPanel;
 
/**
*
* @author ndunn
*/
public class EntryList extends JPanel{
private List<Entry> entries;
// Replace with your database stuff
private static final String[] comboBoxEntries = {"Test","Two","Three"};
 
 
public EntryList() {
this.entries = new ArrayList<Entry>();
 
Entry initial = new Entry(new JComboBox(comboBoxEntries), "", this);
addItem(initial);
}
 
public void cloneEntry(Entry entry) {
Object selected = entry.getComboBox().getSelectedItem();
JComboBox copy = new JComboBox(comboBoxEntries);
copy.setSelectedItem(selected);
Entry theClone = new Entry(copy, "", this);
 
addItem(theClone);
}
 
private void addItem(Entry entry) {
entries.add(entry);
 
add(entry);
refresh();
}
 
public void removeItem(Entry entry) {
entries.remove(entry);
remove(entry);
refresh();
}
 
private void refresh() {
revalidate();
 
if (entries.size() == 1) {
entries.get(0).enableMinus(false);
}
else {
for (Entry e : entries) {
e.enableMinus(true);
}
}
}
 
}
Main.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
package dynamicfields;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
/**
*
* @author ndunn
*/
public class Main {
 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
EntryList panel = new EntryList();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.