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
Related
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.
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?
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).
Is there a way to iterate through the objects on a form and find all the types. In VBNet you would do something like:
For each control on form
print typename(control)
What I mean by a form is the UI that the user can see, I guess that is the Context? If I have an XML that say has 2 buttons and a textview, is there a way from the java code to see what all of the object are that has been loaded from the XML.
I am trying to make a generic class that I can use on all UI classes that will look through every object and if its a button then change the colors on that button.
Alternately I could do this manually for every UI and object.
a normal "java for" :
for( Control c : ArrayList<Control> controlList){
System.out.println(c.getTypeName();
}
hope it helps.
I writing a java program that will read the content of JFrame and based on the value of that an xml file will be created which will contain the attributes and its corresponding value.
I have two classes one having java swing and another creating an xml file.
On my frame i have few check boxes and few combo boxes and I want to create the xml containing attribute and value of these.
I am not able to set the value of these attributes at all in my CreateXML.java file.
If anybody has some solution please help me out.
Regards,
SHK
AlexR is basically right.
However, a far simpler solution to the larger problem is to look into the usage of XMLEncoder, which should handle all of this for you.
JFrame is an indirect subclass of Container that implements method public Component[] getComponents(). Once you have reference to you JFrame you can iterate over its components and create XML if you need. Pay attention that if one of the components is instance of Container you should go inside recursively.
I believe that this is what you should implement into your CreateXML class.