how to pass values from multiple jtextboxes in jframe to another class - java

I am making java application for my semster project for which I have to implement mvc. I have completed my all views using jpanels. In one panel I have created a form which contains multiple jtextfields. Now i want to pass the values of all those textfields to controller class. what is the best way of doing it?
One way of doing it, that I have figured out is to create getfunction and and save the value of those textfields in array string and return that string from the getfunction. But the probem is that this method wil work for only particular data type. If I have to store the values of some textfields as string and some as integer then the problem will occur.

Related

How can you create variable number of jpanels within a jframe or jpanel based on number of entries

Sample layout
I am attempting to build a GUI with JPanels which can display multiple projects, with multiple tasks displayed inside each table (see image). My team is using a database to hold all the project/task data. The project panels are all within a scroll frame, and each project panel can scroll as well.
I am trying to learn how to display the same number of projects in my frame that are in my database. There are not fixed numbers of projects or tasks, so I want to add a panel when it corresponds to an entry in the database. How can I only create the number of panels necessary, making the page dynamic?
Any information that can point out what I should be studying to accomplish this would be appreciated.
Based on what you're trying to do, the feat itself would not require an incredibly complex solution.
Ideally, you should create a structure or an object which contains all relevant information for your specific JPanel. From this, the objects can be stored within an ArrayList.
Looping for the number of objects present within the ArrayList, you can then simply create a new instance of your required JPanel using the information from within said object.
Hope this helps!

How can I accept a value to be passed, using a method

I am trying to select an item from the jList in one form (Home), and extract the data from the ArrayList and output the data to separate jTextFields in a different form (Details). Below is the method I'm trying to use to do this (not a lot there I know!).
public void passObjectData()
{
int i = proObjList.getSelectedIndex();
}
I know once the method is complete, I can just call it on the form load method in the next form, but I'm stuck on how to get the method correct.
I don't know what other code, if any, will be needed for your help.
I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored
in the ArrayList (name, address, tel num etc) and put this information
into jTextFields.
As I understand your question this ArrayList is the undelying data structure used to fill the ListModel and you want to get the selected index from the JList to retrieve the correct object stored in that array list. In this case you can:
Have a domain class called Person to hold the person's data (name, address, etc)
Add Person objects to the ListModel.
Provide an appropriate ListCellRenderer to display the person's name.
Use JList#getSelectedValue() to get the selected Person.
Pass this selected Person object to the text field's form and update those accordingly.
Optional: attach a ListSelectionListener to the JList in order to listen for user's selection changes and do the previous step automatically.
See the first 3 points of this approach exemplified here (note: the example is using JComboBox but the same applies to JList as well).
Suggested readings
Creating a Model
Selecting Items in a List
Writing a Custom Renderer
Side note
Not sure if by forms you mean JFrames but just in case: please note that we should avoid using multiple JFrames. See this topic: The Use of Multiple JFrames, Good/Bad Practice?

Getting a String from another form

I have a JList inside a JDialog that contains names, by double-clicking a name it should get the name somehow and insert it to a String inside another form, basically I know how to do it with when only one form is using this JDialog (Main.setSelectedName(...)), but I'm having a hard time implementing it the usage of two or more forms.
Any suggestions?
create some "holder" object and use it as "clipboard" in another forms

Custom component for JList instead of just strings

I've been trying to freshen up on my Java knowledge and I've been building a small GUI program and I've been running into a bit of a problem.
Basically, I have a JList which I'm currently populating with strings from an object from one of my classes which implement AbstractListModel which we can call my ItemList class. It contains an ArrayList of objects of the type Item which implements Serializable.
But, what I'd like to do is rather than populate my JList with a bunch of strings I'd like to populate it with some kind of string + JTextField combination so I can see one property of each Item object while also being able to update another property by changing the JTextField.
Now, what I'm looking for is the simplest possible way of doing this, and I am assuming there is a (relatively) simple way to do this since it's such a common thing to want to do in a GUI application (although I wouldn't put it past Java and Swing to make it convoluted and complicated).
So, what is the correct way of doing this?
No need to ever use String objects. Instead:
Put Item objects in the JList.
Add a ListCellRenderer to the list, to show the Item object the most user friendly way.
When the user selects an item, show the details in a different place (I'm thinking a panel with 2 columns of labels and text fields, and two rows - one for each attribute, and perhaps a button to Save)
The edit controls would best be encapsulated in a panel that can then hidden when not required, & put in a variety of places, e.g.
Below the list
In the main part of the GUI
displayed in a JOptionPane or a (modal or not) JDialog
Here is an example of placing the 'view/edit panel' (the file details) below the selection component (the table).

Search an arraylist by the contents of a JTextField - arraylist.contains(jtextfield)

Currently, I'm trying to write a system, which is focused around inputting data into lists and arraylists, and implementing search and sort functionality.
Currently on my system, I have:
A class to store data
An arraylist class which gets data from an object within the data storage class.
And finally, a swing GUI class, which contains a JList which displays the Arraylist.
What i'm trying to do is search through the arraylist with a JButton actionlistener, and then output the results on the search to the JList.
The JButton would take the contents of a JTextField, and check if the string is present in the ArrayList.
My question first of all is, how would I go about creating a search function in the arraylist class, and call the contents of a JTextField in a seperate class?
Secondly, would I need to convert the jtextfield to a string before I could call a .contains method on the arraylist?
and thirdly, once the search function is implemented, how would I go about selecting a record from the arraylist if the text searched for is present
Here is my Data storage class:
http://pastebin.com/hwyD8r1j
My arraylist class:
http://pastebin.com/d3ftLsJb
I'm not expecting you guys to write it for me, although that would be nice, haha.
But any pointers or insight on how I could go about implementing this functionality into my arraylist would be appreciated,
Oh and if you need me to post my GUI class, just ask.
Call 'getText' on the JTextField to get the string that they've entered. You'll basically do something like the following.
// Somehow you've initialized your array list
List<String> data = ...;
// within your action listener - invoked when the button is clicked. You'll need to
// make sure the textField is "final"
String selected = textfield.getText();
// Linear search through your strings for one matching your text
for (String datum : data) {
if (selected.contains(datum)) {
// do whatever you want here; you found a match
}
}

Categories