0% found this document useful (0 votes)
9 views

Java Gui

This Java program creates a GUI with a JList of computer brands, buttons to add, display, and delete brands. It uses a DefaultListModel to populate the JList and handle adding/removing brands. When a brand is selected, the display button shows it, and delete removes it from the list.

Uploaded by

tristannerzs0426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Gui

This Java program creates a GUI with a JList of computer brands, buttons to add, display, and delete brands. It uses a DefaultListModel to populate the JList and handle adding/removing brands. When a brand is selected, the display button shows it, and delete removes it from the list.

Uploaded by

tristannerzs0426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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();
}
});
}
}

You might also like