I've been playing about with mouse listeners etc on my tabbedpane but cant seem to get anything going. Trying to make a little menu appears when you right-click a tab which will give you the option to close that tab. Could someone point me in the right direction please
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
System.out.print(tabbedPane.getSelectedIndex());
}
}
});
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
JPopupMenu menu = new JPopupMenu();
JMenuItem closer = new JMenuItem(new AbstractAction("Close") {
#Override
public void actionPerformed(ActionEvent e) {
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
});
menu.add(closer);
menu.show(tabbedPane, e.getX(), e.getY());
}
}
});
It might be better to install the menu on the tab component which can be accessed via tabbedPane.getTabComponentAt. The tab component is the component that renders the text tag for the tab. If you wanted to add an X button to the tab, that is where you put it.
Related
I am trying to build a simple planner app using JavaFX. My current goal is to be able to:
click on a panel of the calendar (already implemented)
type in a task, hit enter and have it show up as a Label (already implemented)
click on the currently placed labels and remove them from the calendar. (issue)
Step 3 is where I am having most trouble. I am confident that I am setting up my mouse event for the label correctly but when I click on one of the labels it runs the mouse event for the panel. I need a way to override the pane's mouse event so I can use the labels mouse event, but I'm not too sure how to go about that. Any feedback would be great!
this.setOnMouseClicked(e ->
{
TextField field = new TextField();
this.getChildren().add(field);
//sets field as a label
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent key) {
KeyCode k = key.getCode();
if ((k.equals(KeyCode.ENTER))) {
Label lab = new Label(field.getText());
getChildren().add(lab);
getChildren().remove(field);
}
}
});
//removes textfield and label
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
KeyCode kc = ke.getCode();
if ((kc.equals(KeyCode.ESCAPE))) {
getChildren().remove(field);
}
}
});
});
if(lab != null)
{
lab.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
setStyle("-fx-background-color: #00FF00;");
}
});
}
I'm trying to create a JPopupMenu, but for some reason, it doesn't show the text I've set on the JMenuItems. The menu itself works, there are menuitems in it and they are responsive, but the text is not showing. I'm creating the menu like this:
private void createPopupMenu() {
this.popupMenu = new JPopupMenu();
this.addMouseListener(new PopupListener(this));
JMenuItem addPlaceMenuItem = new JMenuItem(SketchPad.ADD_PLACE_POPUP_TEXT);
addPlaceMenuItem.setAction(new PopupAction(ActionType.AddPlace));
this.popupMenu.add(addPlaceMenuItem);
JMenuItem addTransitionMenuItem = new JMenuItem(SketchPad.ADD_TRANSITION_POPUP_TEXT);
addTransitionMenuItem.setAction(new PopupAction(ActionType.AddTransition));
this.popupMenu.add(addTransitionMenuItem);
}
In case it matters, here is the PopupListener:
class PopupListener extends MouseAdapter {
SketchPad pad;
public PopupListener(SketchPad pad)
{
this.pad = pad;
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
{
this.pad.getController().deselectAllNodes();
}
else
{
maybeShowPopup(e);
}
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
pad.popupPosition = new Point(e.getX(), e.getY());
pad.popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
What am I missing here?
but for some reason, it doesn't show the text I've set on the JMenuItems.
addPlaceMenuItem.setAction(new PopupAction(ActionType.AddPlace));
The setAction(...) method reset the properties of the menu item with the properties of the Action. So you need to make sure you set the NAME property of the Action to set the text of the menu item.
So in your case it looks like the value of the NAME property should be:
SketchPad.ADD_PLACE_POPUP_TEXT
Or the other approach is to reset the text of the menu item after you set the Action
JMenuItem addPlaceMenuItem = new JMenuItem( new PopupAction(ActionType.AddPlace) );
addPlaceMenuItem.setText(SketchPad.ADD_PLACE_POPUP_TEXT);
The effect is platform specific. In particular, "In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled." Your implementation of mouseReleased() precludes even checking isPopupTrigger(). Instead, handle the selection and check the trigger. A similar approach is shown in GraphPanel in order to handle multiple selection and a context menu.
I have a popupmenu like this
final JPopupMenu contextMenu = new JPopupMenu();
final JMenuItem addTask = new JMenuItem("Add Task");
Then i add a MouseListener:
component.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger()) {
contextMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
In my Actionlistener for the MenuItem i would like to access the x/y-data from my MouseEvent. Is this possible without saving them in an extra variable? i would like to get something like this:
addTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//System.out.println(Mouse.getX()+", "+Mouse.getY());
}
});
If you want to get the mouse coordinate of event which has trggered the popup menu - no it's impossible without to save it. If you want to get the mouse event whcih has triggered the menu item action - yes it's possible: EventQueue.getCurrentEvent(); will return the event (you should check whether this event is a mouse event and if yes - cast it, because the action can also be triggered with key event).
public void actionPerformed(ActionEvent arg0) {
AWTEvent evt = EventQueue.getCurrentEvent();
if (evt instanceof MouseEvent) {
MouseEvent me = (MouseEvent) evt;
}
}
Is it possible to listen to events from browser with Java?
The main task is to add command "copy to file" to pop-up menu of right click of the mouse. This command must add selected text in browser, in Notepad, in winword (any selectable text) to specific text file.
I've just tried code which adds icon to tray but I do not know whether it can it be developed for solving my task.
import java.awt.*;
import java.awt.event.*;
public class SystemTrayTest
{
public SystemTrayTest()
{
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Peformed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
// Depending on which Mustang build you have, you may need to uncomment
// out the following code to check for an AWTException when you add
// an image to the system tray.
// try {
tray.add(trayIcon);
// } catch (AWTException e) {
// System.err.println("TrayIcon could not be added.");
// }
} else {
System.err.println("System tray is currently not supported.");
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
SystemTrayTest main = new SystemTrayTest();
}
}
You are talking about accessing clipboard events. this may helps you. How do we get notified about system clipboard events?
I have two JLists, jltCategories and jltSubcategories, belonging to the same JPanel. Double clicking on the jltCategories causes the jltSubcategories to be populated with the corresponding subcategories, and jltSubcategories is removed from the JPanel, added back and revalidated.
Double clicking the jltSubcategories AFTER it has been removed/added back does not fire anything. Yet, If I open the program and double click on the jltSubcategories, it will fire its mouse event: It will fire if it hasn't been removed/added back, but it will not fire if it has been removed/added back. Same for jltCategories: if I cause it to be removed/added, it will stop firing. Why is this so? Thank you!
jltCategories.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
jbtNavigate.doClick();
}
}
});
jltSubcategories.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
jbtLoad.doClick();
}
}
});
jbtNavigate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String catName = jltCategories.getSelectedValue();
try {
jpLists.remove(jltSubcategories);
jltSubcategories = new JList<String>(SQL.populateSubcategories(catName));
jpLists.add(jltSubcategories);
jpLists.revalidate();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
jbtLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Testing Testing 213");
}
});
It is not enough to revalidate() the view; you must also let the model notify the view that new data is available.
DefaultListModel model = (DefaultListModel ) jltSubcategories.getModel();
model.fireContentsChanged(0, model.getSize());
If this is ineffective, please edit your question to include an sscce that exhibits the problem you describe.
Addendum: It's not clear why you use a MouseListener to effect the update; use a ListSelectionListener, shown here.