import java.awt.
*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
public DefaultListModel<String> listModel;
public JList<String> computerList;
public JTextArea displayArea;
public JTextField newBrandField;
public Main() {
setTitle("Computer Brands");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
listModel = new DefaultListModel<>();
listModel.addElement("MSI");
listModel.addElement("ASUS");
listModel.addElement("Gigabyte");
listModel.addElement("Acer");
computerList = new JList<>(listModel);
computerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
computerList.setVisibleRowCount(5);
displayArea = new JTextArea(5, 20);
displayArea.setEditable(false);
newBrandField = new JTextField(10);
JButton addButton = new JButton("Add Brand");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBrand();
}
});
JButton displayButton = new JButton("Display Selected Brand");
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displaySelectedBrand();
}
});
JButton deleteButton = new JButton("Delete Selected Brand");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteSelectedBrand();
}
});
setLayout(new BorderLayout());
JScrollPane listScrollPane = new JScrollPane(computerList);
listScrollPane.setPreferredSize(new Dimension(150, 180));
JPanel inputPanel = new JPanel();
inputPanel.add(newBrandField);
inputPanel.add(addButton);
JPanel buttonPanel = new JPanel();
buttonPanel.add(displayButton);
buttonPanel.add(deleteButton);
add(listScrollPane, BorderLayout.WEST);
add(displayArea, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
setLocationRelativeTo(null);
setVisible(true);
}
public void addBrand() {
String newBrand = newBrandField.getText().trim();
if (!newBrand.isEmpty()) {
listModel.addElement(newBrand);
newBrandField.setText("");
}
}
public void displaySelectedBrand() {
String selectedBrand = computerList.getSelectedValue();
if (selectedBrand != null) {
displayArea.setText("Selected Brand: " + selectedBrand);
} else {
displayArea.setText("No brand selected.");
}
}
public void deleteSelectedBrand() {
int selectedIndex = computerList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
displayArea.setText("Brand deleted.");
} else {
displayArea.setText("No brand selected.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}