Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating a program using MVC pattern in java... However Im very confused on how to properly perform file input.
For example, should I perform all file input in MAIN? ..I tried but its getting messy, and Im not sure if there is a "uniform" way of performing file input for MVC pattern. This is for an OOP assignment btw. Thank you.
For a theoretical question, the answer would be theoretical.
MVC means Model View Controller
Model - Model represents the layer that is closely related to data. Say if you want to persist a Employee object, you have a Employee POJO carrying the Employee data.
View - View represents the visualization of the data that model contains.
Controller - Controller acts on both Model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps View and Model separate.
In your case, File Input process - what do you mean by that? A File is send as input and you need to persist the file in your database?
This is not a good use case for showing MVC pattern. yet..for your understanding
View - you can have a File Upload page
Controller - The class that route the control from view to a Java class that performs File Read, and performs business logic on the input file
Model - A class that has database connection setup, and has a method that accepts the final formatted File and store to the database.
More experienced folks, correct me if you feel that way
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm newbie programming in java.
I'm doing some forms and in one of them I put some fields, a button and a JTextArea. The idea is when I click in button makes a connection to external database and in JTextArea shows me the connection return (if is okay or if is failed and the error).
So I've created a class with the UI and a class(connection name) with the attributes database, port, username, password ... that make connection to external database.
I don't know if these organization mode is the best way to make it. I should include class connection in the same class of the UI,? How should I pass values of the form to the class connection, by a method?
Could you suggest me how to make it?
As lealceldeiro commented, this is opinion based, but there are some structures that it is good for these types of applications. What I've been taught and have been using is MVC(Model, View, Controller) structure.
The model is responsible for managing the data of the application. It receives user input from the controller.
The view means presentation of the model in a particular format.
The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
I usually make packages/modules called controllers, views and models. So I think you're on the right track here.
Let's say you have one class for your GUI, where the button onClickListener is. That would be your View.
Your models would be the class that you use to connect to the database, and maybe some classes/models in which you store the data from your database queries.
For you I would suggest to make another class that will be your controller. This class would contain your database object, and you can use this to make queries to the database and update your models. One of these could be connectToDatabase(), and you could call this method from your GUI when the button is clicked.
I hope I explained this well enough, and good luck!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to GUI development, may be this is very easy to do, however, any help will be appreciated.
Considering that I have a GUI with 7-8 tabs, and each tab contains text boxes, check boxes and radio buttons, list-boxes and so on.
The idea is to after filling the tabs, the user can save all the data (state of the components) in a file.
And later on by loading the corresponding file, the user can retrieve all the data.
How can I able to do it without manually save all the data?
Update:
Please consider, I have the following GUI application that collect student information:
Sample application Tab # 1
As there are several tabs, the user may stop anytime ans save the information in a file, such as: Saving information
The idea is that the user can load the file later and continue his/her task.
I would like to know how I can save the state of all the components in the application and load later on.
Thanks.
You are mixing up two things here: UI and data.
The data that you save to a file should represent the "mental model" that you build your whole application around.
Example: you seem to think of a "Person" as a collection of text fields and checkboxes ... and that is wrong: a Person is a distinct class, with a set of fields respectively methods that represent behavior.
And a Person object can somehow be displayed within your UI. But what you persist are Person objects, not text fields and checkboxes!
In other words: you should first understand the object model that your application is dealing with. You learn the different ways to persist that data. And based on that, you look into providing a good graphical user experience. And when you are there, your first stop should be models - the Swing concept to "map" data to UI elements.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
For a group project for school we have to make a program that implements the MVC pattern.
The difficulty I have is understanding how the MVC pattern is implemente in java with SWING, does the view make the SWING components?
Another thing I have a hard time understanding is what updates what. For instance one article says the view 'executes' the controller, then the controller updates the model accordingly, the model the notifies the view and and the view then pulls the data from the model.
While what i've learned about the MVC Pattern is that the controller has an instance of the model and the view so it can interact with both. but then if I push a button in the view how does the controller know to do something with the model? And for this example which clas(ses) is/are the Observer(s) and which clas(ses) are the Observable(s)?
Another idea I had is that if we use the MVC Pattern as described above the view is the Observer and the model is the Observable, but that still leaves the question how does the controller know when to update the model?
If it seems vague please ask for clarification in the comments and I will answer.
The View communicates with the controller, the controller communicates with the model and vice-versa.
Say you have a model class Graph that contains the points of a graph. Your views purpose is to display that graph. The Graph class would be an Observable that the controller observes. If the controller observes change, it will tell the view update/change accordingly. If you press a button on the view, say to display a different graph object, your button has an actionListener that asks the controller to change the graph.
And yes, your view classes would create the Swing components.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a school project where we must apply some design patterns in java code. I have to make a functionality where data is obtained by JPA/Hibernate and present it in a pdf or excel. I need to make it so that multiple file extensions can be added in the future.
My question is, what design pattern is best used for a pdf/excel report builder like this.
The design patterns I've already looked at is the strategy pattern and the factory pattern(standard, method and abstract).
As the question is stated, I suppose you should consider some of the creational patterns. Factory would probably be the best way to implement the specific functionality.
Take a look at the MVC Pattern
It might be suited to your requirements, I would have a different view for PDF and EXCEL files. As you add new file types you could add a new view to handle each of them.
The Controller is the link between your user input and the system, it might take a selection from you for the view you wish to see.
The Model is essentially the data you wish to present in your reports. This should be the same for the pdf or excel report.
The View will be either an excel view which creates the excel file, or a pdf view which creates the pdf file. Both use the model as input.
Assumption you have a user interface.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So, I'll be looping through a data base and there will be a bunch of campaigns. Each campaign will have some demos and some sites where certain conditions are satisfied. I want to plot some graphs for the data corresponding to all the campaigns, sites and demos. I was thinking of using java, first getting the campaign, site and demo combinations where the conditions are satisfied and then looping through all of them, running individual queries based on their values and plotting the graphs using maybe, GNU plot. My questions are -
Is there a better way to achieve this (with minimal queries).
If I do do it this way, I first have to store the information.
I was thinking of storing the campaign ids in an ArrayList of Integers, the demos for each campaign in
ArrayList<ArrayList<Integer>>
and the sites for each campaign in
ArrayList<ArrayList<Integer>>
Is there a more efficient way of storing this information?
I'd recommend creating a new class to hold your campaign data and storing references to each object within an ArrayList if you need to keep a handle to them in memory (may not be necessary).
From a purist point of view, the class should be backed by a Data Access Object (DAO) and Plain Old Java Object (POJO) to manage database access and storage in memory but if this is a simple prototype then I wouldn't worry too much. I'd also recommend a utility class to convert/write your chart data - all accessible from your Campaign class.
The Campaign class should also be able to work out whether your conditions are satisfied - and if it's worth generating those charts.