Java Programming Language Event Mouseclicked - java

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.

Related

create dialog from listener

Faced the following situation. I have a list of projects in JCombobox. Changing the line in the combo box causes my itemStateChanged method to be called from the ItemListener interface. And it is right. But here I need to ask the user if he/she is sure of this, because he/she changed something in the old project, and it would be nice to save these changes (or not, since this is an accidental error, or not leave the old project at all). I went beyond a simple JOptionPane.showQuestionMessage and built my own complex dialog. With different fields, trees, tables and others, allowing the user to make an informed choice between "Yes", "No" and "Cancel". And here is an ambush!
The dialog after pack() and setVisible(true) opens but doesn't get focus! The focus is still on the combo box, in which the current line was changed. Probably, this is also correct, since after confirming and possibly saving the changes, the user should be given the opportunity to continue clicking projects.
Is it possible to call modal dialog window without affecting the drop-down list?
Even if we accept the loss of focus in the combo box and the closing of the drop-down list in it, then how transfer focus to the dialog after setVisible(true)? Using dialog.requestFocus before dialog.setVisible (true) doesn't help. Now the "Yes" button must be clicked twice. The first click is to transfer focus, the second is to press the button itself.
Or am I missing something simple? I would be grateful for any suggestions!
Following the #camickr 's advice the solution is:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
appData.askAndSaveCurProject(project);
}
});

How to create JList and Gray Button When user select item from the list the button color change to black?

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.

Java: JButton that creates JTextField everytime it is pressed

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.

Design for a component with 2 listeners and 2 "setters" (Swing application)

I'm making a map editor using java swing for my tile based java game. the swing application has two major components, the "upper" component is the game map preview, and the "lower" component is modifyable properties of the map, like its height and width.
Currently the user types in to a jtextfield for the map width, then I use a change listener to set that value to the GameMap object. The GameMap object when changed fires a notification event to GameMapListeners, the primary listener it has is the preview display of the map inside the swing application.
This lets the user change the map width and instnatly see the results in a preview pane.
Now I want to go to the other way. I want the user to be able to click and drag the edges of the map in the preview pane, but then the results need to then be sent to the properties panel so it shows the updated width value.
This is where the problem is, if I update the jtextfield it'll fire a change event, which would update the GameMap and update the preview display, and then that would fire an event that changes the jtextfield again (so on and on until the program crashes due to stack overflow)
Are there any kind of design patterns i could use instead, or is there some common way to solve this issue?
In this type of case, you have at least two choices...
You Could
Remove the listener to the other component when you want to trigger a change, adding it back after you've raised the event...
You Could
Change the state of a flag to indicate that you should ignore any changes that might come in, resetting after you're raised the event...
Which one you choose will depend on how much code you want to add and how readily available the reference to the listeners in question are (ie, if you don't have a reference to the listener you want to remove, it's kind of hard to implement)
If I update the jtextfield it'll fire a change event, which would update the GameMap and update the preview display, and then that would fire an event that changes the jtextfield again (so on and on until the program crashes due to stack overflow).
When you have a situation like this, you can temporarily remove event listeners, fire the change event, and add the event listeners back. Yes, this is as much of a pain as it sounds, but it's a good way to prevent the stack overflow.
You can see a detailed explanation as well as a working example of managing event listeners in my Sudoku Solver Swing GUI article.
You can use action events for a JTextField. Action events don't trigger when you change the component programmatically.

generic listeners for swt radio buttons

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.

Categories

Resources