I'm a newcomer to Swing and have to create a somewhat complex frame. My MVC application gets recipes from the Internet in the service layer, whereas I need the view layer to allow the user to select the interesting recipes among those obtained, depending on whether each checkbox is selected or not. It should be noted that I've used the page-by-page iterator design pattern to obtain the recipes so the Swing frame should also use pagination (i.e. 10 recipes per page).
Taking all this into consideration, how can I create a frame with two lists (list of recipe names and list of checkboxes) and a convenient layout so that every checkbox is bound to the corresponding recipe name? Of course, data from the recipe list has to be obtained from the proper service method.
Related
I'm developing a JAVA swing application, developed using hibernate and mysql as a database.
I'm trying to bind data from a JList to appear on a JTable, when one of items in the list is clicked. For example i have different user types in my application, Supervisor, Manager, Administrator and others.
Each of the user type has users register under them. I want the application to show certain users when a certain item is clicked on the JList. Like when I click on the supervisor item then all registered supervisors must appear on the JTable. Don't know if I'm making sense, but you all allowed to reply and I will try to make you understand better. Thanks.
Like when I click on the supervisor item then all registered supervisors must appear on the JTable.
One way is to populate the table with all the data and then filter the table when you select an item from the JList. Read the section from the Swing tutorial on Sorting and Filtering.
Otherwise you would need to dynamically query you database every time a JList item is selected.
In either case you will need to add a ListSelectionListener to your JList to invoke your processing. Read the section from the Swing tutorial on How to Use Lists for an example.
So there are two different views(controllers are merged into views):
View 1: Tabular view. There is a table and a number of buttons on top of the table.
View 2: Text view. There is a text area and a number of buttons which are distinct from the buttons in Tabular view.
There is one model file for two views files to link.
I also create a main.java file to declare the main window,a tabbed pane(to switch views) and bind views to it.
As a noob java developer and MVC design pattern learner, I was wondering:
1. What is the correct way to declare buttons, the table and text area?
For example, for View 1(Tabular), are its buttons and table declared in the view or in the main.java?
2. If declared in views, how are they added in the main window? Default UpdateAllViews() doesn't seem to go through the main window in main.java.
At this point, I am only aware that model should never ever have anything like JButton declared in it as model itself should not be aware about what the window and stuff looks and feels. I can see that controllers are sort of binded to a certain view componenet, but the view itself gets me very confused.
If anyone can provide a shortcut to get deeper understanding of MVC pattern, I'd be appreciated.
Please keep in your mind that you are dealing with objects. And the Model, View and Controller are categories/collections of objects.
Your model objects are instances of classes relating to your business domain. e.g. if you are making an address book, you would have an ADDRESS class.
Your View objects provide a connection to your users. e.g. SEARCHDIALOG class and ADDRESSDIALOG class.
Your Controller provides the bindings/interface into your system (the systems API). you will have one controller that represents the system, e.g. ADDRESSBOOKAPP class.
Enjoy.
I am doing a small project for restaurant billing.In the billing form when the user enters the item ,i need the list of possible items to be displayed from which the user can select the appropriate item and all the details of the selected item should be filled in the table. ( just like a super market billing). The items are stored in the database (MySQL) and i am using java swing for user interface
Can anyone please suggest some way for achieving my requirement.Some helpful links as well. ThankYou..
You can use auto-complete package
You can use Swingx.Contains extensions to the Swing GUI toolkit, including new and enhanced components that provide functionality commonly required by rich client applications. Highlights include:
Sorting, filtering, highlighting for tables, trees, and lists
Find/search
Auto-completion
Login/authentication framework
TreeTable component
Collapsible panel component
Date picker component
Tip-of-the-Day component
in my application the user is requested to enter data in a form-like editor.
A form contains a couple of items of different data types (strings, dates,
numbers).
I am looking into Eclipse forms to build this editor, composing the form of
different input elements, one for each data type.
Since the underlying model is not static (items may be added/removed
depending on user selection),
the form view must be dynamically rebuild on model changes. Using SWT I
could have removed all components from the forms
and added the updated components again. How can this be done in SWT /
Eclipse Forms API ? I tried to dispose() the form widgets and create
new ones, but the newly created widgets won't appear.
Since I am new to SWT
this is probably not the way things are meant to be done.
Any ideas or sample code would help...?
After dispose() and creation of new form elements, try to call parent.layout() in order to position your new widgets.
I have a question about GUI design, specifically with Java Swing and creating clean separation between presentation and model.
It's a bit difficult to describe, but essentially we have lots of reference data in our system (i.e. that would correspond to lookup tables in the DB). We want people to be able to edit them all from one screen.
So, in an ideal world what we'd like is a combo box in the top-left corner with a list of 'types' of reference data (so each corresponding to one table in the DB).
Then, when selected, a list of the data is populated below, also a filter (or search box). When one of these items is selected, the panel to the right is activated which will allow the actual data to be edited.
Now, here's the problem: each type of data we need to edit is different, so it has different fields etc. We could go with a generic solution but I'm not really a fan of them - there are lots of different validation rules for each etc, even for different clients, and it would be a nightmare to manage.
We're using the Presentation Model pattern to achieve some degree of separation between GUI code and the model but I can't think of a clean way of doing this which doesn't somehow blur the line of responsibilities a bit.
What are the ways you have solved problems like this?
[Note: apologies for the long question, hope it's understandable, I can re-phrase if necessary]
You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.
So you can have something like:
IWidget widget = UIFactory.createFor(myObject.getType())
That can be invoked on the selection event to create the right widget to edit the selected element.
The IWidget could have methods such as:
validateData()
refreshData()
setDataElement(IDataElement element)
That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.
I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.
If you dont want to go down the generic path, you could have your model hold a mapping of combobox item -> panel name for use with a CardLayout. You could then create custom panels for the editing each of the reference data types. When the combo box selection is changed, you can save the current state in your model, request the panel name of the current selection, prepare your next panel for display and then have your CardLayout show it.