Popup Menu Appears Somewhere Else on JFrame - java

I have Jframe with multiple textfields and textareas , I wanted to add copy paste functionalty to Jtextfields and Jtextareas . as you can see in the picture when I right click on Product Name field , it shows copy paste somewhere else on frame.
https://drive.google.com/file/d/0B2tIFybzjEheNTRUSTB1dTNPdEU/edit?usp=sharing
this is the event i have added to textfield
private void jTextField1MouseReleased(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger())
{
jPopupMenu1.show(this,evt.getX(),evt.getY());
}
}

MouseEvents are contextual, that is, the location specified by the MouseEvent is local to the component that generated the event...
Try using...
jPopupMenu1.show(evt.getComponent(), evt.getX(),evt.getY());
instead

JComponent.setComponentPopupMenu(meu) also can be used.

Related

Displaying a JFrame as the result of a JButton click?

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");
}
}

Text Missing On Checkbox Component for Swing After Adding Action

I'm writing a simple Swing app. I tried adding a checkbox as listed below. Once I added the actionHandler loadPickers the name Foo disappeared from where it was sitting next to the right of chckbxNewCheckBox. I tried adding a call to setHideActionText(), but now nothing displays.
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
If I change it to this it works properly. I see the text "Foo".
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
chckbxNewCheckBox.setText("Foo"); //THIS DOES NOT WORK IF IT COMES BEFORE SET ACTION
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
I've included the action here for completeness. Why does it work this way? Am I missing something here? Currently I'm using the WindowBuilder plugin for Eclipse with the Mig layout system (which I really like). Unfortunately I haven't figure out if there's a way to make WindowBuilder use the .setText() method instead of using the constructor. Any help on what I'm doing wrong, any insight on why this behavior exists like this, or a good workaround for WindowBuilder would be great.
private class LoadPickers extends AbstractAction {
public LoadPickers() {
//putValue(NAME, "SwingAction_2");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
As explained in the JavaDoc of AbstractButton.setAction:
Setting the Action results in immediately changing all the properties described in Swing Components Supporting Action. Subsequently, the button's properties are automatically updated as the Action's properties change.
So all the following properties can be impacted by setting an action:
enabled
toolTipText
actionCommand
mnemonic
text
displayedMnemonicIndex
icon (NA for JCheckBox)
accelerator (NA for JCheckBox)
selected

Pull down searching window in Java

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

Java - Icon/image is grey on a disabled JButton

I'm making minesweeper for a school project. When the player wins or loses, the mines are revealed. Their buttons are disabled, and icons of flags/mines will appear. The problem is that the icons turn grey when the buttons are disabled. Is there a way around this?
I have also tried setting the text of the JButton to something like "<html><img src=\"res\\mine.png\"/></html>" but it showed some weird image.
Update:
I tried using setDisabledIcon() but nothing's showing up. Here's some pseudo-code
The buttons I use for the minefield is a class called Field, which extends JButton
mouseReleased(mouseEvent e) {
Field fieldClicked = (Field)e.getSource();
if fieldClicked is mine {
fieldClicked.setEnabled(false);
gameTimer.stop();
setLost(true);
loop through 2D array of fields {
if field is a mine {
field.setDisabledIcon(Field.mineIcon);// public static final icon of Field. mineIcon = new ImageIcon("res\\mine.png")
field.setEnabled(false);
}
}
}
}
Figured this out in a test
For some reason
clickedButton.setDisabledIcon(mineIcon)
Alone doesn't do anything.
But:
clickedButton.setIcon(mineIcon)
clickedButton.setDisabledIcon(mineIcon)
Will show whatever icon I wanted
a JButton actually allows seven associated images: the main image (use
setIcon to specify it if not supplied in the constructor), the image
to use when the button is pressed (setPressedIcon), the image to use
when the mouse is over it (setRolloverIcon, but you need to call
setRolloverEnabled(true) first), the image to use when the button is
selected and enabled (setSelectedIcon), the image to use when the
button is disabled (setDisabledIcon), the image to use when it is
selected but disabled (setDisabledSelectedIcon), and the image to use
when the mouse is over it while it is selected
(setRolloverSelectedIcon).
- http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html
so use setDisabledIcon(ImageIcon)
The greyed image is the automatically generated one, in case you want a different icon, use setDisabledIcon()
Icon disabledIcon = new ImageIcon("youricon.gif");
button.setDisabledIcon(disabledIcon);

Setting up a Gallery of Images with NetBeans

I want to display a set of images (with associated text) on my window. I want to iterate through them using a previous and a next button. So far, I have only been able to associate the image with a JLabel. =/
How do I go about doing the rest? Should I use a different container for the complete set? Should I load the images on a data structure like an ArrayList, or is it enough to keep them on a folder? How can I add the event handling so that pushing the button displays the next or previous image?
Here is a screenshot of what I have so far.
Are you still here ?
I assume that you have found how to load the path of each of your images (if they are inside the same folder). You should store the path of the directory in a global variable, and then the name of each image into a Vector if you want to iterate through them. Just store the name of the files, not the entire images.
You also have to store the index of the current image as a global variable.
If you use a JFrame as your main window, you have to specify that it implements the class ActionListener this way:
public class MyClass extends JFrame implements ActionListener
Then you have to attach the event handler to your buttons (JButton). This must be placed inside the constructor of your window (MyClass):
nextButton.addActionListener(this);
previousButton.addActionListener(this);
Having implemented ActionListener, your class has to define the method actionPerformed. Inside it, you must change the content of the image according to the button that has been pressed.
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == nextButton)
{
currentIndex++;
if(currentIndex == vectorImages.size())
{
currentIndex = 0;
}
//Change the image in the JLabel
label.setIcon(new ImageIcon(vectorImages.get(currentIndex)));
}
else
{
//Iterate backwards
}
}
Hope this helps...

Categories

Resources