Save and load state of GUI component [closed] - java

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.

Related

Better put code in same class or in different class? [closed]

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!

Making the user follow specific steps [closed]

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 7 years ago.
Improve this question
Am not sure if the title describes what I want. Am developing an App on Android where kids start a new empty drawing project and finish with a full drawing at the end by dragging different shapes from a list of shapes that I created for the App. My problem is that I dont know how am going to follow the kid steps.
The kid is supposed to read a message in each step and try to apply it and then click on a next button to view the next step.
I want to make sure that he do exactly what the message is saying in order to view the next step.
Theoretically, how is this applied in Java ? If for example, there would be 20 steps how can I make the app follow up with him and make sure he is doing the right thing
You are looking to create a wizard. In Java use a MVC libray Spring-MVC or if its android you can use similar approach. Basically you want steps and views while saving the state - typical MVC pattern. Have a model object that is passed along to the views. Each view will have portions of the model that it can read/write to.
Android -refer to WizarDroid.
Spring - AbstractWizardFormController will help

Creating a dialogue in Java with libGDX [closed]

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 want to make a dialogue tree (conversation tree) in Java using libGDX. Should I use lots of conditionals (if,else etc...) and move on to the next dialogue or is there a better way to read a file such as XML that already have the dialogues inside? Also, I want the solution that would consume the least possible memory amount because I am going to write it for Android.
Example of the dialogue tree:
(Q: Question, A: Answer , C:Choice ,AC:Action)
Q:Hi is there anyway that i can help you?
A:You own me 5 dollars!
C1:Ask politely to return them to you, C2:Τhreaten her , C3:Draw your gun
A1:No way get out of here , A2:Call the security , A3:Call the cops
AC1-2:Exit the building //end of choices 1-2
C3.1:Draw your gun and shoot the cops , C3.2:Jump from the window
AC3.1:Arrested , AC3.2:Dead
If your game is going to have little dialogue, I would use Strings for it, but if you base it around the dialogues, I would use a SQLite or similar database to store them in it. I don't know whether it will be the most efficient way to do that, but that what's occurred to me while reading your question:
You could use e.g. column 1 for the question, and columns 2, 3, 4, 5 for the possible answers. You can get information about using SQLite in libGDX here
You could make into some method actor, whom he pass an id to access the SQLite for the question and the answers to that question, then assign some variables, and use a switch statement, if you don't want to have much if-elseif etc.
Note: I think that SQLite is mostly used when you want data to be saved and used in the future; if the data for example change every 10 minutes, I think it would be better to use JSON, because in SQLite making connections to the database every 10 minutes may take some time. I think this is not the case; in my opinion the purpose of JSON and SQLite is completely different. I would use for example:
JSON = I would use it to send or/and receive data betwen server & client or configuration files etc.
SQLite = I would use it to store data.
This is only my opinion, and I not say that SQLite is better or worse than JSON.
PS 1: the photo is taken from the Internet
PS 2: I also believe that you should read https://stackoverflow.com/tour

Java MVC with File Input/Output [closed]

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

Best data structure to store peculiarly structured data [closed]

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.

Categories