I have a class that extends BasicTabbedPaneUI and does some paint component overriding.
I want to be able to add a addMouseListener to the class I use it in to check when the user selects a tab the current tab index and the previous tab index.
NOTE: The user is able to navigate to tabs via the keyboard and not just clicking on a tab and I want to be able to make sure the previous index tracks this. So in the example below preIndex would equal the previous index regardless to whether the user navigated to it via the keyboard or mouse.
Any ideas please?
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.indexAtLocation(e.getX(), e.getY());
int prevIndex = ?????
}
});
Many thanks!!!!
I would use the change listener instead of mouse listener (it's called in both cases: for mouse and key event triggered tab change). If you cannot determine previously selected tab you can use following approach: save currently selected tab index as client property of the tabbed pane.
private static final String OLD_TAB_INDEX_PROPERTY = "oldTabIdx";
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.getSelectedIndex();
int oldIdx = 0;
Object old = tabP.getClientProperty(OLD_TAB_INDEX_PROPERTY);
if (old instanceof Integer) {
oldIdx = (Integer) old;
}
tabP.putClientProperty(OLD_TAB_INDEX_PROPERTY, currIndex);
// now we can use old and current index
}
});
Related
When I receive a ChangeEvent from my JSpinner, I'd like to detect if user used the arrows to increase/decrease the number value, or directly typed a new number.
What would be the best approach to do this ?
EDIT: for now my non-reliable solution is just to save the last JSpinner changed value, and if new change value is +/- equals to the step value, then I assume user clicked on an arrow. It works except if user typed a value which is equals to (oldValue +/- step).
EDIT: why ?
I want to reproduce the behavior found in Midi editors of several famous DAWs. The JSpinner represents the velocity (0-127) of selected notes. It shows the velocity of the first selected note. Usually notes velocity differ. When you increase with arrow, you want to increase all selected notes by the same amount. When you type in a new value, you want all velocities to be reset to this value.
Distinguishing the trigger of a value change is not supported - the only value-related event fired by JSpinner is a ChangeEvent which carries no state except the source. We need another type of listener or a combination of listeners.
First thought: listen to changes of the editor's textField, f.i. an actionListener or a propertyChangeListener on the value property. This doesn't work, mainly because
both change- and propertyChangeListener are fired always (change before property)
actionListener is not fired on focusLost
Second thought: go dirty and dig into implementation details to hook into the action fired by the arrow buttons.
The idea:
look up the action for increment/decrement from the spinner's actionMap: this is the same as the arrows' actions and also used by up/down keys (which I assume not counting a "editing")
for each, create a wrapper that sets a flag before delegating to super
put that wrapper into the spinner's actionMap
look up the arrow buttons in the spinner's children and replace their respective actionListener with the wrapper
Client code would change the tweak's changeListener to acts according to the flag as appropriate.
Some code doing the tweaking (beware: not formally tested!):
public static class SpinnerTweaker {
private JSpinner spinner;
private boolean wasButtonAction;
private Object oldValue;
public SpinnerTweaker(JSpinner spinner) {
this.spinner = spinner;
AbstractAction incrementDelegate = createDelegate("increment");
spinner.getActionMap().put("increment", incrementDelegate);
AbstractAction decrementDelegate = createDelegate("decrement");
spinner.getActionMap().put("decrement", decrementDelegate);
// replacing arrow button's action
Component[] components = spinner.getComponents();
for (Component component : components) {
if (component instanceof JButton) {
if (component.getName() == "Spinner.nextButton") {
ActionListener[] actions = ((JButton) component).getActionListeners();
ActionListener uiAction = actions[0];
((JButton) component).removeActionListener(uiAction);
((JButton) component).addActionListener(incrementDelegate);
}
if (component.getName() == "Spinner.previousButton") {
ActionListener[] actions = ((JButton) component).getActionListeners();
ActionListener uiAction = actions[0];
((JButton) component).removeActionListener(uiAction);
((JButton) component).addActionListener(decrementDelegate);
}
}
}
spinner.addChangeListener(e -> {
if (wasButtonAction) {
System.out.println("value changed by button: " + spinner.getValue());
} else {
System.out.println("value changed by editing: " + spinner.getValue());
}
wasButtonAction = false;
});
}
protected AbstractAction createDelegate(String actionCommand) {
// hooking into original button action to set button flag
AbstractAction action = (AbstractAction) spinner.getActionMap().get(actionCommand);
AbstractAction delegate = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
oldValue = spinner.getValue();
wasButtonAction = true;
action.actionPerformed(e);
// hit min/max - TBD: needs testing!
if (oldValue.equals(spinner.getValue())) {
wasButtonAction = false;
}
}
};
return delegate;
}
}
#kleopatra solution works but I found a simpler solution.
The trick is that commitEdit() is only called internally by JSpinner when change results from the increment or decrement action.
public class Spinner2 extends JSpinner
{
// To be checked by ChangeListener after receiving the ChangeEvent
public boolean wasManualEdit=true;
#Override
public void commitEdit() throws ParseException
{
wasManualEdit = false;
super.commitEdit();
}
#Override
protected void fireStateChanged()
{
super.fireStateChanged();
wasManualEdit = true;
}
}
private void createEvents()
{
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
////// Events on tree selection
jtStoryViewer.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent arg0)
{
DefaultMutableTreeNode selection = (DefaultMutableTreeNode) jtStoryViewer.getLastSelectedPathComponent();
Object nodeObject = selection.getUserObject();
////// Checks if selected node is a String (only story title is a string)
if(selection.getUserObject().getClass().getName() == "java.lang.String" )
{
tfTitle.setText(nodeObject.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
selection.setUserObject(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
///// checks if the object is a chapter object
if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter")
{
Chapter chapter = (Chapter) selection.getUserObject();
tfTitle.setText(chapter.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
chapter.setTitle(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
}
});
}
I am using JTree to display and modify some objects. I added a TreeSelectionListener to get the object data on selection. For now I want to be able to change the title of an object, it works fine on first selection on the tree , I change the value in the text box and the "Change" button works just fine, but when I move on to next objects, the change button also modifies the value of all previously selected objects.
I guess it is caused due to my improper usage of the ActionListeners but I can't tell for sure and at this point I'm stuck.
Will be grateful for any hints.
Don't keep adding an ActionListener to the btnChange JButton within the TreeSelectionListener#valueChanged method.
This will cause the button to call EVERY ActionListener you have previously
Instead, give the btnChange a single ActionListener, when clicked, can act on the currently selected node (by checking the JTree it self). You could have the TreeSelectionListener#valueChanged method enable or disable the btnChange based on the validity of the selection
Also, if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter") isn't how String comparison is done in Java, instead you should use something more like if("ISW.common.Chapter".equals(selection.getUserObject().getClass().getName()))
I am showing some results in a JTable that consists of 2 columns.
File - Result
I implemented a JPopupMenu which displays a copy entry, and I try to copy the value of the cell, where I right-clicked.
filelistTable.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{
TablePopupMenu popup = new TablePopupMenu(filelistTable, e.getPoint());
filelistTable.setComponentPopupMenu(popup);
}
}
});
--
public TablePopupMenu(JTable table, Point p) {
this.table = table;
this.p = p;
JMenuItem mntmKopieren = new JMenuItem("Kopieren");
mntmKopieren.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
copyCellToClipboard();
}
});
add(mntmKopieren);
}
public void copyCellToClipboard()
{
int r = table.rowAtPoint(p);
int c = table.columnAtPoint(p);
System.out.println(table.getValueAt(table.convertRowIndexToView(r),
table.convertRowIndexToView(c)));
StringSelection entry = new StringSelection(table.getValueAt(table.convertRowIndexToView(r),
table.convertRowIndexToView(c)).toString());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( entry, this );
}
Anyhow, this only works for a small number of tests.
Did I do something wrong or something missing? It looks to me, as if the cell will not even get choosen correctly.
Two thingies are slightly off:
setting the componentPopup in the clicked is too late in the sequence of mouseEvents (popups are typically triggered on pressed or released which happen before the click)
the value is taken from the incorrect cell: all coordinates in a JTable are in view coordinate system, converting them to view coordinates will be completely off
That said: getting cell-coordinate related context is poorly supported. Often, the best bet is to (code snippet below)
override getPopupLocation(MouseEvent) and store the location somewhere
implement a popup/action to access the location
Fails if (as should be done in a well-behaved application), the popup could be triggered by keyboard: if that's the case, you'll need to provide some other marker (f.i. the focused cell) to act on.
final String popupLocation = "table.popupLocation";
final JTable table = new JXTable(new AncientSwingTeam()) {
#Override
public Point getPopupLocation(MouseEvent event) {
// event may be null if triggered by keyboard, f.i.
// thanks to #Mad for the heads up!
((JComponent) event.getComponent()).putClientProperty(
popupLocation, event != null ? event.getPoint() : null);
return super.getPopupLocation(event);
}
};
JPopupMenu popup = new JPopupMenu();
Action printLocation = new AbstractAction("print cell") {
#Override
public void actionPerformed(ActionEvent e) {
Point p = (Point) table.getClientProperty(popupLocation);
if (p != null) { // popup triggered by mouse
int row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
LOG.info("" + table.getValueAt(row, column));
} else { // popup triggered otherwise
// could choose f.i. by leadRow/ColumnSelection
...
}
}
};
popup.add(printLocation);
table.setComponentPopupMenu(popup);
Edit (triggered by Mad's comment):
You should be checking MouseEvent.isPopupTrigger as the trigger point is platform dependent. This does mean you need to monitor mousePressed, mouseReleased and mouseClicked
No, that's not needed (just checked :-): the mechanism that shows the componentPopup in response to a mouseEvent - happens in BasicLookAndFeel.AWTEventHelper - only does so if it is a popupTrigger.
By reading the api doc (should have done yesterday ;-) again, it turns out that the method is called always before showing the componentPopup, that is also if triggered by other means, f.i. keyboard. In that case the event param is null - and the original code would blow. On the bright side, with that guarantee, all the logic of finding the target cell/s could be moved into that method. Didn't try though, so it might not be feasable (f.i. if then the location should be based on the leadRow/ColumnSelection that might not yet be fully handled at that time)
I'm working with Netbeans IDE in Java.
I've a form with one JPanel.
Each JPanel has a gridLayout 3x3 and in each place there is an image representing a number[0,1,2,3,4,5,6,7,8](the image is created used a custom class,not just fitting the image in a lab).
I want to be able to exchange two images in the panel when the user click them (First click: no action , second click: switch the two images fitted in the jPanel Components).
I already created a function exchangeComponents and with a test code (like:
exchangeComponents (0,8,jPanel1)
it exchanges correctly the images located in position1 (1st row,1st column) and in position2 (3rd row,3rd column).
The function a creted is the following:
public void exchangeComponents(int component1,int component2,JPanel jpanel){
try{
Component aux1 = jpanel.getComponent(component1);
Point aux1Loc = aux1.getLocation();
Component aux2 = jpanel.getComponent(component2);
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
}
catch (java.lang.ArrayIndexOutOfBoundsException ex){ /* error! bad input to the function*/
System.exit(1);
}
}
I suppose I neeed to have an event that call the function exchangeComponents() when the user click on one of the images on the jPanel1 but how should I do it? and how to check what components (images) the user has selected?
I just know that when I create a Button if a click on it (from the IDE) an event like
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// some code..
}
is created and the code I fill in is executed.
Thank you in advance for any hint.
You need to add the same mouse listener to all you JLabels or whatever container you have for your images, like:
img1.addMouseListener(this);
img2.addMouseListener(this);
etc., then detect which Jlabel you clicked with MouseEvent.getSource(); , like this
boolean hasclicked1=false;
JLabel click1label=null;
public void mouseClicked(MouseEvent me){
if(!hasclicked1){ //clicked first pic
hasclicked1 = true;
click1label = (JLabel) me.getSource();
} else { //clicked second pic
hasclicked1 = false;
exchangeComponents(click1label, (JLabel) me.getSource(), /*your jpanel here*/);
}
//now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
try{
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
Component aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
System.exit(1);
}
}
If you are not using JLabels for the images though, replace JLabel in the code with whatever you are using...
EDIT: Sorry, I don't think I made this unclear, but your class with the method exchangeComponents has to implement MouseListener. Then, in the mouseClicked event put the code I gave for it. Make sure to include the variables hasclicked1 and click1label in your class. Make you class something like this
public class ComponentExchanger implements MouseListener {
boolean hasclicked1=false;
JLabel click1label=null;
JPanel mainPanel;
public ComponentExchanger(){
//create JFrame, JPanel, etc.
JFrame f=new JFrame();
//etc.
mainPanel=new JPanel();
f.add(mainPanel);
//set layout of panel, etc.
for(int i=0;i<9;i++){
JLabel l=new JLabel(/*label image here*/);
Point loc=new Point(/*coordinates here*/);
l.setLocation(loc);
mainPanel.add(l);
/*more code*/
f.setVisible(true);
}
}
public static void main(String args[]){
new ComponentExchanger();
}
public void mouseClicked(MouseEvent me){
if(!hasclicked1){ //clicked first pic
hasclicked1 = true;
click1label = (JLabel) me.getSource();
} else { //clicked second pic
hasclicked1 = false;
exchangeComponents(click1label, (JLabel) me.getSource(), mainPanel);
}
//now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
try{
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
Component aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
System.exit(1);
}
}
//Also, you will need to include the other mouselistener implemented methods, just
//leave them empty
}
First of all, to be technical it's methods not functions.
There are a couple of ways you could do this. You could go ahead with actionListener, but then you would probably need buttons or something.
Or you could use MouseListener, and detect clicks over a certain region of the panel.
For the switching algorithm, perhaps an array of 2 images. There is a variable that increases by 1 every click. When the variable is 2, it resets back to 0.
clicks++; //every time the mouse is clicked; clicks starts out at 0
if(clicks == 2){
clicks = 0; //at the end of the listener method
}
On the first click the clicked image goes into the first array slot, because the user has clicked once.
clickImage = imageArray[clicks];
On the second click, the other clicked image goes to the second array slot, because 2 clicks have been detected. In this case, your exchangeComponents method would go at the end of the listener method, with the arguments being imageArray[1], imageArray[2], .
You can apply this to ints or whatever, just save the value in an array and use an incrementing and resetting variable.
here is my problem:
I have a jList and a popup menu. When I right click the jList, the popup menu shows. The problem is that the jList item which the mouse is pointing at won't select.
And I want it to do that. When I point my cursor at an item in the list and press the right button, I want two things to happen. Select the item on which I clicked and show the popup menu.
I tried this:
jLists.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
jList.setSelectedIndex(jList.locationToIndex(e.getPoint()));
}
});
jList.setComponentPopupMenu(jPopupMenu);
But it only shows the popup menu.
If I delete this line:
jList.setComponentPopupMenu(jPopupMenu);
then the right-click select works (but the popup menu doesn't show).
So, what do you think is the best way to make these two functions (both) work ?
Thanks and sorry for my english.
Don't do setComponentPopupMenu. In the MouseAdapter do the following:
public void mousePressed(MouseEvent e) {check(e);}
public void mouseReleased(MouseEvent e) {check(e);}
public void check(MouseEvent e) {
if (e.isPopupTrigger()) { //if the event shows the menu
jList.setSelectedIndex(jList.locationToIndex(e.getPoint())); //select the item
jPopupMenu.show(jList, e.getX(), e.getY()); //and show the menu
}
}
This should work.
EDIT: The code now checks both press and release events, because some platforms show popups when mouse presses and some other on release. See the Swing tutorial for more info.
If you want to continue to use setComponentPopupMenu (which is nice because it handles mouse and keyboard invocations of the popup in a cross platform way), you could override JPopupMenu.show(Component, int, int) to select the appropriate row.
JPopupMenu jPopupMenu = new JPopupMenu() {
#Override
public void show(Component invoker, int x, int y) {
int row = jList.locationToIndex(new Point(x, y));
if (row != -1) {
jList.setSelectedIndex(row);
}
super.show(invoker, x, y);
}
};
jList.setComponentPopupMenu(jPopupMenu);
Note that when your popup is invoked via the keyboard (and you don't also override getPopupLocation on your target component), the x, y location you get in JPopupMenu.show will be the midpoint of your component. If there's already a selection in this case you probably don't want to change the selection.
The solution I came up with to solve the keyboard vs. mouse invocation problem was to set a client property on the component in an override of getPopupLocation and then check it when showing the popup. The argument to getPopupLocation will be null when invoked via the keyboard. Here's the core code (perhaps implemented in a utility class available to your component and its popup menu).
private static final String POPUP_TRIGGERED_BY_MOUSE_EVENT = "popupTriggeredByMouseEvent"; // NOI18N
public static Point getPopupLocation(JComponent invoker, MouseEvent event)
{
boolean popupTriggeredByMouseEvent = event != null;
invoker.putClientProperty(POPUP_TRIGGERED_BY_MOUSE_EVENT, Boolean.valueOf(popupTriggeredByMouseEvent));
if (popupTriggeredByMouseEvent)
{
return event.getPoint();
}
return invoker.getMousePosition();
}
public static boolean isPopupTriggeredByMouseEvent(JComponent invoker)
{
return Boolean.TRUE.equals(invoker.getClientProperty(POPUP_TRIGGERED_BY_MOUSE_EVENT));
}
Then override getPopupLocation in your component:
#Override
public Point getPopupLocation(MouseEvent event)
{
return PopupMenuUtils.getPopupLocation(this, event);
}
and call isPopupTriggeredByMouseEvent in an override of JPopupMenu.show to determine whether or not to select the row at the popup location (or whatever action may make sense for the underlying component):
JPopupMenu jPopupMenu = new JPopupMenu() {
#Override
public void show(Component invoker, int x, int y) {
int row = jList.locationToIndex(new Point(x, y));
if (row != -1 && PopupMenuUtils.isPopupTriggeredByMouseEvent((JComponent) invoker)) {
jList.setSelectedIndex(row);
}
super.show(invoker, x, y);
}
};