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

Java Swing MVC

This document describes a Java Swing application that implements the model-view-controller (MVC) pattern. It contains classes for the model (Model), view (View), and controller (Controller). The main method assembles these pieces by creating instances of each class and initializing the controller. The controller links user interactions with the view to methods that update the model or trigger other behavior.

Uploaded by

Natali Gamko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Java Swing MVC

This document describes a Java Swing application that implements the model-view-controller (MVC) pattern. It contains classes for the model (Model), view (View), and controller (Controller). The main method assembles these pieces by creating instances of each class and initializing the controller. The controller links user interactions with the view to methods that update the model or trigger other behavior.

Uploaded by

Natali Gamko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Centrul de Excelenta in Informatica și Tehnologii Informaționale

Java Swing MVC

Eleva:Gamco Natalia
Profesor:Cerbu Olga
public class App {
public static void main(String[] args) {
// Assemble all the pieces of the MVC
Model m = new Model("Gamco", "Natalia");
View v = new View("MVC with Swing");
Controller c = new Controller(m, v);
c.initController();
}
}import javax.swing.JOptionPane;

public class Controller {


private Model model;
private View view;
public Controller(Model m, View v) {
model = m;
view = v;
initView();
}
public void initView() {
view.getFirstnameTextfield().setText(model.getFirstname());
view.getLastnameTextfield().setText(model.getLastname());
}
public void initController() {
view.getFirstnameSaveButton().addActionListener(e -> saveFirstname());
view.getLastnameSaveButton().addActionListener(e -> saveLastname());
view.getHello().addActionListener(e -> sayHello());
view.getBye().addActionListener(e -> sayBye());
}
private void saveFirstname() {
model.setFirstname(view.getFirstnameTextfield().getText());
JOptionPane.showMessageDialog(null, "Firstname saved : " + model.getFirstname(),
"Info", JOptionPane.INFORMATION_MESSAGE);
}
private void saveLastname() {
model.setLastname(view.getLastnameTextfield().getText());
JOptionPane.showMessageDialog(null, "Lastname saved : " + model.getLastname(),
"Info", JOptionPane.INFORMATION_MESSAGE);
}
private void sayHello() {
JOptionPane.showMessageDialog(null, "Hello " + model.getFirstname() + " " +
model.getLastname(), "Info", JOptionPane.INFORMATION_MESSAGE);
}
private void sayBye() {
System.exit(0);
}
}
public class Model {
private String firstname;
private String lastname;
public Model(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}

import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class View {
// View uses Swing framework to display UI to user
private JFrame frame;
private JLabel firstnameLabel;
private JLabel lastnameLabel;
private JTextField firstnameTextfield;
private JTextField lastnameTextfield;
private JButton firstnameSaveButton;
private JButton lastnameSaveButton;
private JButton hello;
private JButton bye;
public View(String title) {
frame = new JFrame(title);
frame.getContentPane().setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Create UI elements
firstnameLabel = new JLabel("Firstname :");
lastnameLabel = new JLabel("Lastname :");
firstnameTextfield = new JTextField();
lastnameTextfield = new JTextField();
firstnameSaveButton = new JButton("Save firstname");
lastnameSaveButton = new JButton("Save lastname");
hello = new JButton("Hello!");
bye = new JButton("Bye!");
// Add UI element to frame
GroupLayout layout = new GroupLayout(frame.getContentPane());
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(firs
tnameLabel)
.addComponent(lastnameLabel))

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(firs
tnameTextfield)
.addComponent(lastnameTextfield))

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(firs
tnameSaveButton)
.addComponent(lastnameSaveButton))

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(hell
o)
.addComponent(bye)));
layout.setVerticalGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(fir
stnameLabel)

.addComponent(firstnameTextfield).addComponent(firstnameSaveButton).addComponent(hell
o))

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(las
tnameLabel)

.addComponent(lastnameTextfield).addComponent(lastnameSaveButton).addComponent(bye)))
;
layout.linkSize(SwingConstants.HORIZONTAL, firstnameSaveButton,
lastnameSaveButton);
layout.linkSize(SwingConstants.HORIZONTAL, hello, bye);
frame.getContentPane().setLayout(layout);
}
public JFrame getFrame() {
return frame;
}
public void setFrame(JFrame frame) {
this.frame = frame;
}
public JLabel getFirstnameLabel() {
return firstnameLabel;
}
public void setFirstnameLabel(JLabel firstnameLabel) {
this.firstnameLabel = firstnameLabel;
}
public JLabel getLastnameLabel() {
return lastnameLabel;
}
public void setLastnameLabel(JLabel lastnameLabel) {
this.lastnameLabel = lastnameLabel;
}
public JTextField getFirstnameTextfield() {
return firstnameTextfield;
}
public void setFirstnameTextfield(JTextField firstnameTextfield) {
this.firstnameTextfield = firstnameTextfield;
}
public JTextField getLastnameTextfield() {
return lastnameTextfield;
}
public void setLastnameTextfield(JTextField lastnameTextfield) {
this.lastnameTextfield = lastnameTextfield;
}
public JButton getFirstnameSaveButton() {
return firstnameSaveButton;
}
public void setFirstnameSaveButton(JButton firstnameSaveButton) {
this.firstnameSaveButton = firstnameSaveButton;
}
public JButton getLastnameSaveButton() {
return lastnameSaveButton;
}
public void setLastnameSaveButton(JButton lastnameSaveButton) {
this.lastnameSaveButton = lastnameSaveButton;
}
public JButton getHello() {
return hello;
}
public void setHello(JButton hello) {
this.hello = hello;
}
public JButton getBye() {
return bye;
}
public void setBye(JButton bye) {
this.bye = bye;
}
}

You might also like