Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.
Related
Iam trying to make normal JList and when the user select item the button change, i guess if there is any action listener or something i could add? i don't actually know
I guess you could use a ListSelectionListener. It will generate an event every time the selection changes.
Read the section from the Swing tutorial on How to Write a ListSelectionListener for more information and working examples.
I have been trying to do this a while back but failed to find a way.
My Java program contains four JFrames. JFrame1 contains a table and a button that on clicking opens JFrame2. In JFrame2 there is a table and two buttons that on clicking opens JFrame3 and JFrame4 which contain a table and Save button each.
Now what I want to do is that when a user enters data in JFrame3 and clicks the save button the data is saved in the table of JFrame2 and JFrame1 repectively. Same thing for JFrame4 but the data should be appended not deleted.
The data that is saved in the JFrame1 from JFrame3 and JFrame4 is to be used to insert into the database.
These are my requirements. Can anyone help me out with this problem? Thanks in advance.
What you are doing is usually achieved through two approaches:
You can use the Card Layout to avoid creating a new JFrame each time, but rather have one JFrame and replace its components. This would allow you to have one class which stores this data and then the logic to show it on your different screens.
If (1) does not do it for you, the other option would be to pass on a reference of the parent frame to the child. Thus, when you create an instance of JFrame2, you also pass it along an instance of JFrame1. Each JFrame class will have methods which are responsible for accepting information and displaying it according to your requirements.
Option 1 is usually the most recommended.
You can use parameterized constructor or public variables or even public methods to pass values from 1 JFrame to another also using public static variables(not preferred) will be okay.
When I double clicked the table of the specific person details, the dialog frame pop out, but its details could not be included.
What shall I do? I need the details inside the table to also display in the new dialog frame that pops out.
Since there isn't any code its hard to tell. But you can use MouseClick event to your table modal and get all the data into public arrays,struct or some variables. Then you can set them into the text fields of your new pop up window by setText method.Thats the simple and the easiest way possible. Or if you don't want new pop up window you can simply set the details in table into text fields or whatever you want by inserting MouseClick event.
Hello I have a JTable and there a 3 radio buttons at the bottom when the user clicks the buttons I want the JTable to only show certain rows for that button. I've tried deleting the rows when the button is pressed but I get an error when i click the other button that re-adds them. Is there anyway I can just hide the rows when the button is pressed and show them when the other button is pushed?
Is there anyway I can just hide the rows when the button is pressed and show them when the other button is pushed?
Use a RowFilter for your table. So you need some data in the table that you will be able to specify a filter for. Every time you click the button you will need to change the filter for the new requirements.
Read the section from the Swing tutorial on Sorting and Filtering for more information and working examples.
I think the way to go is implementing your own TableModel (preferably by extending AbstractTableModel)
I'm developing a SWT app and in one particular form there are 14 pairs of Yes-No radio buttons. Each of these pairs have a text box associated with them. So if a user selects Yes, the associated textbox should be editable else uneditable. I find writing 28 listeners for the radio buttons really daunting. Since the radio buttons have nothing much to do than just rendering the textbox editable/uneditable I was hoping if there were some generic type of listeners in SWT that would be applicable to a set of radio buttons specified in an array or like that. Are there any frameworks or shall I have to write individual listeners?
Edit
I'm trying to fire an event only when the radio button is selected
rdoExperience.addListener(SWT.CHECK, new RadioButtonSelection(
txtExperience));
but SWT.CHECK is causing the event to be fired on mouse hover over radio button too. I've tried using SWT.SELECTED too but it's not working either and I can't find other suitable SWT constants. W;hat should I use?
Good point. Sorry I don't know such thing.
However, you create one yourself: Instead of writing an anonymous listener for every button, you could write one - say MyButtonListener - and give it the button text box as an argument. Than you instantiate MyButtonListener with the appropriate text box as an argument. Than in the Listeners appropriate callback method you enable or disable the text box.
Edit: My bad. Of course I meant you could give it your text box like radioBtn.addListener(SWT.SELECTED, new MyButtonListener(textfield1));
You could create one SelectionListener and add it to each of the radio buttons. Then you can ascertain which button was pressed from the selection event and map that to a text box. For the mapping you could use an array or hashtable.