MyCustomizableGUI() { this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); textField = new JTextField(20); buttonPref = new JButton("Preferences"); this.add(textField); this.add(buttonPref); buttonPref.addActionListener(evt -> prefDialog.setVisible(true)); frame = new JFrame("My text editor"); frame.setContentPane(this); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // Preferences dialog box prefDialog = new JDialog(frame, "Dialog", true); prefDialog.setLayout(new BorderLayout()); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(3, 2)); JPanel p2 = new JPanel(); p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); buttonSave = new JButton("Save"); buttonCancel = new JButton("Cancel"); buttonSave.addActionListener( evtSave -> { String fontChosen; int fontSizeChosen; prefDialog.setVisible(false); if ((String) color.getSelectedItem() == "Red") { textField.setForeground(Color.red); } else if ((String) color.getSelectedItem() == "Green") { textField.setForeground(Color.green); } else if ((String) color.getSelectedItem() == "Blue") { textField.setForeground(Color.blue); } else if ((String) color.getSelectedItem() == "Cyan") { textField.setForeground(Color.cyan); } else if ((String) color.getSelectedItem() == "Magenta") { textField.setForeground(Color.magenta); } else if ((String) color.getSelectedItem() == "Yellow") { textField.setForeground(Color.yellow); } else if ((String) color.getSelectedItem() == "Black") { textField.setForeground(Color.black); } fontChosen = (String) font.getSelectedItem(); fontSizeChosen = Integer.parseInt((String) fontSize.getSelectedItem()); textField.setFont(new Font(fontChosen, Font.PLAIN, fontSizeChosen)); UserPreferences userPrefs = new UserPreferences(); userPrefs.setColor((String) color.getSelectedItem()); userPrefs.setFont(fontChosen); userPrefs.setFontSize(fontSizeChosen); try (FileOutputStream fileOut = new FileOutputStream("preferences.ser"); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut); ) { objectOut.writeObject(userPrefs); } catch (IOException ioe) { System.out.println("I/O error: " + ioe.getMessage()); } }); buttonCancel.addActionListener(evt -> prefDialog.setVisible(false)); colorLabel = new JLabel("Color:"); fontLabel = new JLabel("Font:"); fontSizeLabel = new JLabel("Font size:"); color = new JComboBox(colorList); font = new JComboBox(fontList); fontSize = new JComboBox(fontSizeList); p1.add(colorLabel); p1.add(color); p1.add(fontLabel); p1.add(font); p1.add(fontSizeLabel); p1.add(fontSize); p2.add(buttonCancel); p2.add(buttonSave); prefDialog.add(BorderLayout.NORTH, p1); prefDialog.add(BorderLayout.SOUTH, p2); prefDialog.pack(); }