I have to create a searching window, like a google browser window. It must have a pull down list containing similar results, which is populated from a database.
I am trying to adjust a JCombobox but this has caused me a lot of trouble. Is there a better way to do this? (Perhaps something like this already exists in Java.) If not, can anyone advise me on how to achieve my goal?
create JTextField with keyboard event to show the popup window on key realeased,
Example:
jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
showPopup(evt);
}
});
void showPopup(java.awt.event.KeyEvent evt) {
JPopupMenu popup = new JPopupMenu();
popup.setLightWeightPopupEnabled(false);
popup.setBorder(BorderFactory.createLineBorder(Color.black));
popup.setLayout(new BorderLayout());
popup.setSize(this.getPreferredSize());
popup.setPreferredSize(this.getPreferredSize());
popup.pack();
popup.setOpaque(false);
// create panel that contains the search result
popup.add(BorderLayout.CENTER, <YOUR PANEL WITH THE RESULT>);
popup.setPreferredSize(new Dimension(jTextField2.getWidth(),250));
<SEARCH PANEL>.setPreferredSize(new Dimension(jTextField2.getWidth(),250));
popup.show(jTextField2, 0, jTextField2.getHeight());
}
i have to do searching window like google browser window. It must have pull down list with similarities results that comes from database. I trying to adjust JCombobox but this made me a lot of trouble.
Maybe not true, I'd to use AutoComplete JComboBox / JTextField
Related
I have a Java application that contains a lot of features, and to make life easier for the user, I have set up numerous mnemonics and accelerators. For instance, I have a JMenuItem that allows the user to save the state of the application, witht he following code:
JMenuItem saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
This works as desired, but now I would like to give the user an option to change the hot keys. While CTRL + s would seem like a fairly obvious hot key to stick with, there are many features that use these short cuts, and simply picked Save as an example.
I have a JButton that I have arranged for testing purposes that allows the user to enter in a new shortcut when clicked. I was thinking that I would simply try and capture the keys that the user holds down (InputEvent) and presses (KeyEvent). I also though it might be smart to force the use of an InputMask to avoid complications in Text Fields and the like.
What I am wondering is: What is the best way to capture the new input that the user enters? I have looked up information regarding KeyBindings and they look right for the job, but the main issue I see is actually capturing the keys and saving them.
Sounds like you need to setup a KeyListener. When the user presses/releases a key, it triggers a KeyEvent from which you can retrieve the main key pressed (e.g. S) and the mask/modifiers (e.g. CTRL+SHIFT).
From there you can create a KeyStroke object and set this as the new accelerator of your menu.
public void keyReleased(KeyEvent e){
KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
menuItem.setAccelerator(ks);
}
The thing is you probably want this key listener to be removed right after the key released event, to avoid multiple keystrokes to be captured. So you could have this kind of logic:
JButton captureKeyButton = new JButton("Capture key");
JLabel captureText = new JLabel("");
KeyListener keyListener = new KeyAdapter(){
public void keyReleased(KeyEvent e){
KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
menuItem.setAccelerator(ks);
captureText.setText("Key captured: "+ks.toString());
captureKeyButton.removeKeyListener(this);
}
};
ActionListener buttonClicked = new ActionListener(){
public void actionPerformed(ActionEvent e){
captureKeyButton.addKeyListener(keyListener);
captureText.setText("Please type a menu shortcut");
}
};
captureKeyButton.addActionListener(buttonClicked);
I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}
When a user clicks a JButton in my Java-Swing application, a string is returned from a method and the user then needs to be able to read the string (somehow). The JButton is within a JPanel. My first thought was to create an 'alert' dialogue (thinking this would be easy), I tried to follow this example that looked easy: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExample.htm
I have not yet been able to confirm if this works because I do not know how to import the libraries into eclipse. For example import org.eclipse.swt.SWT; gives the error "... cannot be resolved".
So one possible solution is how to import in Eclipse. Another possible solution is to dynamically change the text within the JPanel somehow.
As Ben mentioned in his comment. I would set a jLabel with a blank text to start. Then, when you click your button that triggers the method, simply tack on:
label.setText(value);
Alternatively you could use another pane to popup and display the message.
If you're talking about Swing, an easy solution is to pop a message box with the string:
button.addActionListener(new ActionListener {
public void actionPerformed(ActionEvent e) {
String message = methodThatReturnsYourString();
JOptionPane.showMessageDialog(null, message);
}
}
In this link you will find the information u require in order to add the library and import it:
http://www.eclipsepluginsite.com/swt.html
as well I would not mix SWT with swing and I would stick to swing you can set the label on the event of your button
button.addActionListener(new ActionListener {
public void actionPerformed(ActionEvent e) {
String message = "Test String";
labelMessage.setText(message );
}
}
Using Swing in Java I wrote a form containing radio buttons, text fields and so on. In the very end I have a "Submit" button.
Now I want to "send" the information given by the user to the program. How do I do that? Is there a good tutorial about that?
Is it kind of similar to PHP? (I am asking just because I know how to do it in PHP. To avoid confusions I probably need to mention that I do NOT program a web application).
Processing data in Swing is way different from the typical web REQUEST/RESPONSE paradigm.
To Take something you may know, it's more in the fashion of Javascript actions in an HTML page : each time user performs an operation, one or more events are sent, and the application developper can update application content according to it.
In your case, if you register an ActionListener to the button, it will be called each time button is clicked. You'll then have the possibility to perform any operation you want.
But that's not all !
Each time a component is keyboard focused, or receives the mouse, events are sent, as well as when a key is stroked or when widget's model is updated.
I would really suggest you to read documents such as Swing tutorial (which dives in greater details than I could do in 1 month).
Not completely sure what you mean by "send to the program". You are in the program so I assume that you have a dialog that renders this form? Just pass the dialog the object that you want to use to store the data. For example, your dialog's constructor can take an argument.
public class MyDialog extends JPanel {
private UserInfo userInfo;
private JTextField name;
/**
* The main area of the dialog.
*/
protected JPanel panel;
public MyDialog(UserInfo userInfo) {
this.userInfo = userInfo;
}
public showDialog() {
// Some code to create the form which it looks like you already know how to do
// Create a name field
JLabel nameLabel = new JLabel("Name:");
panel.add( nameLabel );
JButton submit = new JButton("Submit");
submit.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent event)
{
this.userInfo.setName(name.getText().trim());
} } );
panel.add( submit );
}
}
So i have made a simple program with a basic menu at the top of the frame, Now i just need to put actions behind each JMenuItem. Im struggling to work the code out though, Here is what i thought would work:
JMenu file_Menu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit Program");
file_Menu.add(fileExit);
fileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
main_Menu.add(file_Menu);
This doesn't seem to work though, I thought that this code would create a small popup window when the menu item is clicked.
Can any spot the bug because i cant seem to.
Suggestion: Instead of adding a separate ActionListener, just use AbstractAction:
JMenuItem fileExit = new JMenuItem(new AbstractAction("Exit Program") {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
I'd also suggest, instead of setting EXIT_ON_CLOSE on the popup menu, you set it on the main frame of your application, and have the action simply call theMainFrame.dispose().
You got it working, but you have another problem.
Don't do this:
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
When you close the pop-up frame, your entire JVM terminates. Consult JFrame.setDefaultCloseOperation javadocs for a more appropriate value.
Give an instance of Action (extend from AbstractAction) to JMenuItem
Based on the code you posted it looks like it should work, but we can't see the entire context of how the menu item is being used.
Did you debug your code (with a System.out.println) to see if the ActionListener is being invoked?
If you need more help post your SSCCE that demonstrates the problem.
Fixed it.
Forgot to add the actionPerformed method.