GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
Assignment2B
Browse code
Added some comments.
master
1 parent
bf46995
commit
37049ed43eaa71e7bf70b457c2c019925d0140aa
Jonathan Ström
authored
on 31 Aug 2017
Patch
Showing
1 changed file
src/MainWindow.java
Ignore Space
Show notes
View
src/MainWindow.java
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class MainWindow extends JFrame { private static final long serialVersionUID = 1L; private IList<String> elementList; private JTextField txtInputField; private JLabel lblShowText; private JList<String> lstElementsList; private JButton btnAddElement, btnRemoveElement, btnClearAllElements, btnFindElement, btnAppendList; private DefaultListModel<String> model; public MainWindow() { this.setTitle("QueueWindow"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 208); this.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); /* * Create the button listener. * Either use one or several different inner class listeners. * Also, either write the code inside the different listeners, * or write the code in different methods. Not sure what to use for this assignment. * Example: btnAddElement.addActionListener(new AddElementListener); */ ButtonListener buttonListener = new ButtonListener(); // Left panel JPanel leftPanel = new JPanel(new GridBagLayout()); // lblShowText lblShowText = new JLabel(); lblShowText.setText(" "); // To save a place in the grid for the label when it updates. c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; leftPanel.add(lblShowText, c); // txtInputField txtInputField = new JTextField(10); c.gridy++; leftPanel.add(txtInputField, c); // btnAddElement btnAddElement = new JButton("Add"); c.gridy++; leftPanel.add(btnAddElement, c); btnAddElement.addActionListener(buttonListener); // btnRemoveElement btnRemoveElement = new JButton("Remove"); c.gridy++; leftPanel.add(btnRemoveElement, c); btnRemoveElement.addActionListener(buttonListener); // btnClearAllElements btnClearAllElements = new JButton("Clear"); c.gridy++; leftPanel.add(btnClearAllElements, c); btnClearAllElements.addActionListener(buttonListener); // btnFindElement btnFindElement = new JButton("Find"); c.gridy++; leftPanel.add(btnFindElement, c); btnFindElement.addActionListener(buttonListener); /* btnAppendList */ btnAppendList = new JButton("Append List"); c.gridy++; leftPanel.add(btnAppendList, c); btnAppendList.addActionListener(buttonListener); // leftPanel c.gridx = 0; c.gridy = 0; this.add(leftPanel, c); // Right panel // lstElementsList lstElementsList = new JList<String>(); lstElementsList.setFixedCellWidth(150); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(lstElementsList); c.gridx = 1; c.gridy = 0; c.fill = GridBagConstraints.BOTH; this.add(scrollPane, c); elementList = new List<String>(); this.setVisible(true); // Show some help to run the commands. JOptionPane.showMessageDialog(this, "addElement(String): Use the text field. Use comma if you want to use (pos, elem).\n" + "removeElement(int): Use the text field to remove the specific element.\n" + "clearAllElements(): Use this to remove all elements.\n" + "findElement(String): Use this to search for something in your list, it will go there.\n" + "appendList(String[]): Use comma to separate all items to add.", "Help", JOptionPane.PLAIN_MESSAGE); } /** * This class will listen for button actions. */ private class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Add": addElement(); break; case "Remove": removeElement(); break; case "Clear": clearAllElements(); break; case "Find": findElement(); break; case "Append List": appendList(); break; } } } /** * Refresh the list on the screen. */ private void updateList() { model = new DefaultListModel<String>(); for (String s : this.elementList.getAllElementsAsStrings()) { model.addElement(s.toString()); } lstElementsList.setModel(model); } /** * Add an element to the list. */ private void addElement() { if (!txtInputField.getText().equals("")) { if (txtInputField.getText().contains(",")) { String[] contents = txtInputField.getText().split(","); elementList.addAt(Integer.parseInt(contents[0]), contents[1]); } else { elementList.addAt(0, txtInputField.getText()); } updateList(); } } /** * Remove an element from the list. */ private void removeElement() { elementList.removeAt(elementList.find(lstElementsList.getSelectedValue())); updateList(); } /** * Remove all elements from the list. */ private void clearAllElements() { elementList.clear(); updateList(); } /** * Find the first element that matches in the list. */ private void findElement() { lstElementsList.setSelectedValue(txtInputField.getText(), true);; } /** * Append a list of items separated by commas to the current list. */ private void appendList() { String[] elements = txtInputField.getText().split(","); IList<String> elementList = new List<String>(); for (String element : elements) { elementList.addAt(-1, element); } this.elementList.append(elementList); updateList(); } public static void main(String[] args) { new MainWindow(); } }
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class MainWindow extends JFrame { private static final long serialVersionUID = 1L; private IList<String> elementList; private JTextField txtInputField; private JLabel lblShowText; private JList<String> lstElementsList; private JButton btnAddElement, btnRemoveElement, btnClearAllElements, btnFindElement, btnAppendList; private DefaultListModel<String> model; public MainWindow() { this.setTitle("QueueWindow"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 208); this.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); /* * Create the button listener. * Either use one or several different inner class listeners. * Also, either write the code inside the different listeners, * or write the code in different methods. Not sure what to use for this assignment. * Example: btnAddElement.addActionListener(new AddElementListener); */ ButtonListener buttonListener = new ButtonListener(); // Left panel JPanel leftPanel = new JPanel(new GridBagLayout()); // lblShowText lblShowText = new JLabel(); lblShowText.setText(" "); // To save a place in the grid for the label when it updates. c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; leftPanel.add(lblShowText, c); // txtInputField txtInputField = new JTextField(10); c.gridy++; leftPanel.add(txtInputField, c); // btnAddElement btnAddElement = new JButton("Add"); c.gridy++; leftPanel.add(btnAddElement, c); btnAddElement.addActionListener(buttonListener); // btnRemoveElement btnRemoveElement = new JButton("Remove"); c.gridy++; leftPanel.add(btnRemoveElement, c); btnRemoveElement.addActionListener(buttonListener); // btnClearAllElements btnClearAllElements = new JButton("Clear"); c.gridy++; leftPanel.add(btnClearAllElements, c); btnClearAllElements.addActionListener(buttonListener); // btnFindElement btnFindElement = new JButton("Find"); c.gridy++; leftPanel.add(btnFindElement, c); btnFindElement.addActionListener(buttonListener); /* btnAppendList */ btnAppendList = new JButton("Append List"); c.gridy++; leftPanel.add(btnAppendList, c); btnAppendList.addActionListener(buttonListener); // leftPanel c.gridx = 0; c.gridy = 0; this.add(leftPanel, c); // Right panel // lstElementsList lstElementsList = new JList<String>(); lstElementsList.setFixedCellWidth(150); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(lstElementsList); c.gridx = 1; c.gridy = 0; c.fill = GridBagConstraints.BOTH; this.add(scrollPane, c); elementList = new List<String>(); this.setVisible(true); // Show some help to run the commands. JOptionPane.showMessageDialog(this, "addElement(String): Use the text field. Use comma if you want to use (pos, elem).\n" + "removeElement(int): Use the text field to remove the specific element.\n" + "clearAllElements(): Use this to remove all elements.\n" + "findElement(String): Use this to search for something in your list, it will go there.\n" + "appendList(String[]): Use comma to separate all items to add.", "Help", JOptionPane.PLAIN_MESSAGE); } private class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Add": addElement(); break; case "Remove": removeElement(); break; case "Clear": clearAllElements(); break; case "Find": findElement(); break; case "Append List": appendList(); break; } } } private void updateList() { model = new DefaultListModel<String>(); for (String s : this.elementList.getAllElementsAsStrings()) { model.addElement(s.toString()); } lstElementsList.setModel(model); } private void addElement() { if (!txtInputField.getText().equals("")) { if (txtInputField.getText().contains(",")) { String[] contents = txtInputField.getText().split(","); elementList.addAt(Integer.parseInt(contents[0]), contents[1]); } else { elementList.addAt(0, txtInputField.getText()); } updateList(); } } private void removeElement() { elementList.removeAt(elementList.find(lstElementsList.getSelectedValue())); updateList(); } private void clearAllElements() { elementList.clear(); updateList(); } private void findElement() { lstElementsList.setSelectedValue(txtInputField.getText(), true);; } private void appendList() { String[] elements = txtInputField.getText().split(","); IList<String> elementList = new List<String>(); for (String element : elements) { elementList.addAt(-1, element); } this.elementList.append(elementList); updateList(); } public static void main(String[] args) { new MainWindow(); } }
Show line notes below