How to do the best lookup textfield for JAVA Swing [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to do the best lookup textfield for JAVA Swing like below:

This is actually not very easy to achieve, not if you want a flexible and re-usable solution at the end of the process.
You need a JTextField (well, okay, that was obvious), a JList and a JPopupMenu or JWindow.
You need some kind of filtering mechanism that can take a String and produce a List of matches, which can then be displayed within the JList. This will require you to provide a ListCellRenderer for you particular implementation.
You will need to attach a DocumentListener to the JTextField. When the document is updated/changed, you will need to pass the text of the text field to your filtering engine to find all the possible matches and display them within the JList.
If the popup is not visible, you will need to make it visible, taking into account that the list might not fit on the screen based on where the text field is. If it is visible, you may need to re-pack the window to better accomidate the available options (if any)
If the user leaves the field, you will need to decide if you want to auto complete the text based on the first match, invalid the field or leave the text as is and close the popup if it's visible.
If the user clicks on a value, you will need to extract the String representation of the item and apply it to the text field and close the popup.
You will need to add key bindings to the text field to accommodate the down arrow which could open the popup window and/or move to the first item in the list. This will trigger a lose of focus, so you need to be prepared for it.
You will probably also want to provide a Escape keyboard binding for the popup/field so the user can dismiss the popup

Related

Can I use JDialog for this? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm pretty new to Java and I'm currently attempting to make a simple work scheduling program. I'm trying to implement a way to add employee's and their info into but I'm a little stuck.
What I wanted to do was have a button that opens up a new window that will let me input their name (string), total hours to work per week (int), and their availability (array, check boxes that will translate to an array). Is it possible to customize a JDialog to do this or is there a better way to go about doing this? I tried reading tutorials on JDialogs but none of it explains how to implement multiple inputs.
I currently have it to where I'm opening up a new JFrame but I've read from multiple sources that I shouldn't do that.
Thanks for any help.
I tried reading tutorials on JDialogs but none of it explains how to implement multiple inputs.
This is no different than adding multiple components such as JTextFields, JRadioButtons, JComboBoxes in a JFrame. For both you'd create a main JPanel to hold the GUI, and then give it components and or other JPanels each using its own layout manager. Then create your JDialog or JFrame (using the API to see which constructor to use), add your main JPanel to the top level window (actually to its contentPane) by calling add(myMainPanel), pack the top level window by calling pack(), and display it via setVisible(true).
The key issue for a dialog window is often when to query its contents. If it's a modal dialog, then that's easy -- you query the contents (the state of its fields) after the call to display the dialog, since that code flow will resume once the dialog is no longer visible. For a non-modal dialog, then you'd need to add a WindowListener to notify you when the dialog is no longer visible.
For more specific help, you need to ask a more specific question and show code.

How to pass data between 2 or more ACTIVATED JFrames? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In C# this is done by using delegates and event, here is the link: Passing Data between Windows Forms.
My requirement is like this. User have to search the item code by loading the whole items from the database to a new form's JTable, this new form is shown by clicking a button in the main form (or any other form). After that appears, user Double click on specific item code in the table and that item code is passed to the main form's (or any other form's) text field and closed the current form.
here is the output from c# https://www.youtube.com/watch?v=_lPkc1YV2vQ&feature=youtu.be
The second window should not be a JFrame, but rather a JDialog, possibly modal. If it is modal, then the calling window will know when the 2nd dialog window closes, since program flow stops at the calling code immediately from when the dialog is displayed (think of how JOptionPanes work) and does not resume until the 2nd dialog window closes. It is right then that you would extract pertinent data from the objects associated with the dialog window, and this information can be obtained by simple means such as by calling appropriate getter methods.
e.g.,
JDialog someDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
someDialog.add(myContentPaneWithMyGui);
someDialog.pack();
someDialog.setVisible(true);
// here code flow stops until the dialog is no longer visible.
// now call my getter to extract data
SomeType someData = myContentPaneWithMyGui.getSomeData();
If you want to stick with your event/delegate way of doing, you could consider using BeansBinding.
It requires your model to fire events when their data changes, and add your Swing GUI elements registers as propertyListeners on those models.

Set mouse click event on text? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a project where some random text is displayed in a JScrollPane. This text is coded in and is not editable. Is there a way to create a mouse click event on each line of text and have that event take the text clicked on and have it display in a textField?
The part that stumps me is how to act on a line of text versus a clickevent on a button per se. Below is a rendering of the project and the areas containing the text.
Presumably your text is displayed in a JTextArea or JTextPane, so you would add a MouseListener to the component. Then when the MouseEvent is generated you can get the caret position. Using the caret position you can then use the Utilities class. It has methods like:
getRowStart(...)
getRowEnd(...)
Using these values you can then get the text from the Document using the getText(...) method.

Setting TextField limit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created a swing application which has a string named textField, and I need to append in a string named textArea. My Problem is; when it appends, it doesn't align, since the names are different .
I want to make it accept 25 characters only, and if less than 25 were entered they are filled with spaces.
Is there any way this can be done? I Have been on it since morning without making any headway.
Any suggestion would be greatly appreciated, thanks in advance.
Every Text Component in java associates with a model to maintain it's text content known as Document. A Document has: DocumentListener to listen to changes made to it which generates event on it's content update. you can also use DocumentFilter with Document to change how the text component's data is set. You can implement certain customizations either by installing a document filter or by replacing a text component's document with one of your own.
Check out the example and tutorial:
How to write DocumentListener
Implementing a Document Filter
DocumentSizeFilter

Moving a JLabel in a circular path [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am.working. on a Netbeans Java project in JFrame form (a GUI application) where I want to move a JLabel into circular path. Can any tell or help to how to do that?
It sounds like you haven't done anything yet, and when that happens and you need to get started, then you should try to break the main problem into little steps, and then try to solve each little step one at a time, including looking at references for each of these steps such as using a Timer, doing animation, positioning components, etc...
So general recommendations:
Look up using a Swing Timer (or just click on link)
Use the Timer to drive your animation.
You can move a JLabel if the layout is null, but this is generally to be avoided.
Consider instead using a custom layout if you absolutely need to move a JComponent (the JLabel) along a prescribed path.
Or if you just want to move an image, then draw the image inside of a JPanel's paintComponent(...) method, setting its position with two int fields that are changed by the Timer. This JPanel of course will need to be displayed in your GUI. There are lots of examples on how to do this on this site, some written by me (for example), that simple searching can help you find.
But most important, take the first steps, do something, anything, that moves you forward with this project.
Then when you try this if it doesn't work, show your code and we'll be much better able to help.

Categories

Resources