before cell select jtable event - java

Are there any event that is fired when cell is about to be selected? There is ListSelectionListener, but it has event that is fired only after selection has happened. I need some way to cancel selection event and using ListSelectionListener it is not easy as selection has already happened and I need to have some state variable that indicates if selection is normal or is cancel of a previous selection.
Are there a way to switch off selection notifications? However this is not 100% good solution (there will be problems if some listeners saves selection state in its local storage) this is better than nothing.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.JTable;
public class JTableExample extends JFrame {
/**
*
*/
private static final long serialVersionUID = 6040280633406589974L;
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableExample frame = new JTableExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTableExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
table = new JTable(new MyTableModel());
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectionModel.addListSelectionListener(new MySelectionListener());
contentPane.add(table, BorderLayout.CENTER);
}
class MyTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = -8312320171325776638L;
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 10;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex * columnIndex;
}
}
class MySelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow == 5) {
System.out.println("I would like this selection never happened.");
}
}
}
}

whatever the goal is that you want to achieve: thinking "mouseEvent" is not enough, selection might change for other reasons (f.i. keyboard input, programmatic trigger, ..). Reverting an unwated change in a listener is not an option: as you already noted that would require to keep a duplicate of the selection and might confuse other listeners.
The only way (that I see, could be others, of course ;-) is not to let it happen in the first place: implement a List SelectionModel which doesn't change the selection if certain conditions are met. My favourite (biased me :-) is a VetoableListSelectionModel It's a subclass of DefaultListSelectionModel which in SingleSelectionMode waits for vetoes from interested parties before actually changing.
Here's a (raw) code snippet using it:
VetoableListSelectionModel vetoableSelection = new VetoableListSelectionModel();
VetoableChangeListener navigationController = new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
// custom method that implements your condition
if (!canSelect((int) evt.getOldValue(), (int) evt.getNewValue()))
throw new PropertyVetoException("uncommitted changes",
evt);
}
};
vetoableSelection.addVetoableChangeListener(navigationController);
myTable.setSelectionModel(vetoableSelection);

The only way to do this that i can think of is handle the MouseEvent and using MouseAdapters, get the coordinates and somehow to check whether the mouse pointer is hovering over a cell or not, if it is, do what you want to do. you probably have to do addMouseListener to get the effect.

Assuming you have set the selection mode of the ListSelectionModel to the desired value and added a listener, you may find it helpful to examine the predicate getValueIsAdjusting(), which "Returns true if the selection is undergoing a series of changes." In practical terms, it is true when the mouse is down, or when it is being dragged in one of the INTERVAL modes.
It may also help to know more about the goal of the this effort, as another approach may be helpful. Naturally, an sscce is always in order.
Addendum: This appears to work, but #kleopatra's approach would prevent losing the previous selection.
private static final int FORBID = 5;
class MySelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow == FORBID) {
selectionModel.removeIndexInterval(FORBID, FORBID);
System.out.println(FORBID + " forbidden.");
}
}
}
user is forbidden to change row if he has not accepted change he made in that row.
You could use a custom CellEditor that conditions stopCellEditing(), as shown in this example.

Related

Prevent a JComboBox popup from appearing. Passing InputVerifier

Given the following scenario:
one has a JComboBox and a JTextField
the latter has an InputVerifier providing its own error message.
the textField is the current focus owner and its input does not satisfy the inputVerifier (Here: Less than 3 characters).
Now you click on the combo, which will fire the InputVerifier.
The InputVerifier in turn will show its error message.
You acknowledge the error message and, lo and behold, the combo's popup appears, ready to accept a selection.
To me this is an undesired behaviour, and I would prefer to have no other
component change its input until the inputVerifier is satisfied. Paticularly
in the described scenario the JComboBox could have its own ActionListener
changing the whole situation in case of a new selection.
The following code solves this problem by applying a PopupMenuListener, which
checks a boolean in the verifier telling its state, and then decides whether to
show the popup or not.
What I would like to do now is to write an extended JComboBox class implementing this functionality and accepting all kinds of (extended) InputVerifiers. But I
am already stuck in my test code here, for I don't see a way how to give the
PopupMenuListener access to the verifers' boolean 'satisfied' in a general way.
Passing just an InputVerifier as parameter would need casting later, which
unmakes the generality. - Or is there any "variable" casting?
Any idea is welcome, even if it is a completely different approach to the initial problem.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
public class DisableComboPopupByVerifier extends JFrame {
public static final long serialVersionUID = 100L;
JComboBox<String> cmb;
public DisableComboPopupByVerifier() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450, 240);
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
cmb= new JComboBox<>(new String[]{"AA", "BB", "CC"});
cmb.setPreferredSize(new Dimension(43, 20));
MyMinLenVerifier verifier= new MyMinLenVerifier(this, 3);
MyPopupListener popupListener= new MyPopupListener(verifier);
cmb.addPopupMenuListener(popupListener);
add(cmb);
JTextField tf1= new JTextField(5);
tf1.setInputVerifier(verifier);
add(tf1);
JTextField tf2= new JTextField(5);
tf2.setInputVerifier(verifier);
add(tf2);
SwingUtilities.invokeLater(() -> tf1.requestFocusInWindow());
setVisible(true);
}
static public void main(String args[]) {
EventQueue.invokeLater(DisableComboPopupByVerifier::new);
}
class MyMinLenVerifier extends InputVerifier {
boolean satisfied;
int minlen;
Component parent;
public MyMinLenVerifier(Component parent, int minlen) {
this.minlen= minlen;
this.parent= parent;
}
public boolean verify(JComponent input) {
String s= ((JTextField)input).getText();
if (s.length()>=minlen) {
satisfied= true;
}
else {
satisfied= false;
JOptionPane.showMessageDialog(parent,
"Enter at least "+minlen+" characters.",
"Error", JOptionPane.ERROR_MESSAGE);
}
return satisfied;
}
}
class MyPopupListener implements PopupMenuListener {
//** To accept all kinds of verifiers the specific one here needs to be replaced
by the super class InputVerifier, which, however, makes casting necessary to access the boolean 'satisfied' below.
**//
MyMinLenVerifier verifier;
public MyPopupListener(MyMinLenVerifier verifier) {
this.verifier= verifier;
}
public void popupMenuCanceled(PopupMenuEvent e) {
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
//** boolean 'satisfied' is a requirement in all passed verifiers. **//
if (!verifier.satisfied) {
BasicComboPopup popup= (BasicComboPopup)cmb.getUI()
.getAccessibleChild(cmb, 0);
SwingUtilities.invokeLater(() -> {
popup.setVisible(false);
// This restauration of the button background is not reliable!
MetalComboBoxButton btn= (MetalComboBoxButton)cmb.getComponent(0);
btn.getModel().setPressed(false);
btn.repaint();
((JComboBox)e.getSource()).repaint();
});
}
}
}
}
For lack of a better idea I overrode toString() in MyMinLenVerifier
#Override
public String toString() {
return "satisfied:"+satisfied;
}
And the MyPopupListener class looks now like this:
class MyPopupListener implements PopupMenuListener {
InputVerifier verifier;
public MyPopupListener(InputVerifier verifier) {
this.verifier= verifier;
}
.
.
.
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
if (verifier.toString().endsWith("false")) {
...

Change JList item background color on hover

I'm trying to change the background color of a JList cell when it is being hovered over, but I'm not sure how to do it. Here is what I currently have:
package cats.youtube.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import cats.youtube.search.YoutubeSearchResult;
public class SearchResultsList extends JPanel{
private class Renderer extends DefaultListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focused){
final JTextArea area = new JTextArea(model.get(index).toString());
area.setBorder(new EmptyBorder(5, 0, 5, 0));
area.setForeground(selected || focused ? Color.WHITE : Color.BLACK);
area.setBackground(selected || focused ? Color.RED : Color.WHITE);
return area;
}
}
public class Model extends AbstractListModel<String>{
private LinkedList<YoutubeSearchResult> results;
private Object lock;
private Model(){
results = new LinkedList<YoutubeSearchResult>();
lock = new Object();
}
public int getSize(){
return results.size();
}
public String getElementAt(final int i){
return results.get(i).toString();
}
public YoutubeSearchResult get(final int i){
return results.get(i);
}
public void add(final YoutubeSearchResult r){
synchronized(lock){
results.add(r);
fireContentsChanged(this, 0, getSize());
try{
lock.wait(500L);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void remove(final YoutubeSearchResult r){
results.remove(r);
fireContentsChanged(this, 0, getSize());
}
public void removeAll(){
results.clear();
fireContentsChanged(this, 0, getSize());
}
}
private JList<String> list;
private JScrollPane scroll;
private Model model;
private Renderer renderer;
public SearchResultsList(){
super(new BorderLayout());
list = new JList<String>(){
public void processMouseMotionEvent(final MouseEvent e){
super.processMouseMotionEvent(e);
final int i = locationToIndex(e.getPoint());
if(i > -1){
final Rectangle bounds = getCellBounds(i, i+1);
if(bounds.contains(e.getPoint())){
// <--------- here is line 95
}
}
}
};
list.setModel(model = new Model());
list.setCellRenderer(renderer = new Renderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(scroll = new JScrollPane(list), BorderLayout.CENTER);
}
public Model getModel(){
return model;
}
}
The problem is on line 95; I don't know what to put there. I tried multiple approaches, one being calling the getListCellRendererComponent method through my renderer and changing the background like that, but that didn't work.
The hovering portion does work (it does get the correct indexes) I just don't know what to put on line 95. If anyone could help me, it would be very much appreciated.
I, personally, would use a MouseMotionListener over overriding the processMouseMotionEvent, but that's just me.
You need some way to tell the renderer which rows are "highlighted", the two immediate ways I can think of achieving this is to ...
Create a custom JList which has methods to set/get the highlighted row. You would then need to cast to this implementation and interrogate the appropriate method, taking action as required.
Provide a method within the list data that mark the row as highlighted or not. This would allow you to interrogate the data directly.
The advantage of the first approach is that it isolates the responsibility to the view, where it really belongs. It does have the disadvantage of meaning you need to create a custom JList. It might be easier to use the getClientProperty and putClientProperty methods instead, this would mean you wouldn't need a custom implementation nor cast the list in the renderer, but is has the disadvantage of not being obvious to other developers.
The second approach mixes display and data information together, not something I would encourage as you really want to keep this kind of stuff separated ;)
Here's how I did it (solution here: http://objectmix.com/java/73071-highlight-itemin-jlist-mouseenter-mouse-over.html, second message):
private int mHoveredJListIndex = -1;
...
mList.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent me) {
Point p = new Point(me.getX(),me.getY());
int index = mList.locationToIndex(p);
if (index != mHoveredJListIndex) {
mHoveredJListIndex = index;
mList.repaint();
}
}
});
And in your renderer:
public class CellRenderer extends JComponent implements ListCellRenderer
{
#Override
public Component getListCellRendererComponent(JList aList, Object aValue, int aIndex, boolean aIsSelected, boolean aCellHasFocus)
{
Color backgroundColor = mHoveredJListIndex == aIndex ? Color.gray : Color.white;
JPanel pane = new JPanel(new BorderLayout()); // add contents here
pane.setBackground(backgroundColor);
return pane;
}
}
AFAIK good RolloverSupportTest / Hightlighter is implemented
Substance L&F has hightlighter
RolloverSupportTest by #aephyr

JComboBox not showing arrow

I have been searching this site and google for a solution to my problem, and I can't find anything. I think it's supposed to just work; however, it doesn't. The arrow icon for my JComboBox doesn't show up, and I can't find anywhere to set its visibility to true.
Here's my code:
public class Driver implements ActionListener {
private JTextField userIDField;
private JTextField[] documentIDField;
private JComboBox repository, environment;
private JButton close, clear, submit;
private JFrame window;
public Driver()
{
window = makeWindow();
makeContents(window);
window.repaint();
}
private JFrame makeWindow()
{
JFrame window = new JFrame("");
window.setSize(500,300);
window.setLocation(50,50);
window.getContentPane().setLayout(null);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
return window;
}
private void makeContents(JFrame w)
{
makeDropDowns(w);
w.repaint();
}
private void makeDropDowns(JFrame w)
{
String[] repositoryArray = {"Click to select", "NSA", "Finance", "Test"};
repository = new JComboBox(repositoryArray);
repository.setSelectedIndex(0);
repository.addActionListener(this);
repository.setSize(150,20);
repository.setLocation(175,165);
repository.setEditable(false);
w.add(repository);
String[] environmentArray = {"Click to select", "Dev", "Test", "Qual"};
environment = new JComboBox(environmentArray);
environment.setSelectedIndex(0);
environment.addActionListener(this);
environment.setSize(150,20);
environment.setLocation(175,195);
//environment.setEditable(false);
w.add(environment,0);
}
public void actionPerformed(ActionEvent e)
{
String repositoryID = "null", environmentID = "null";
if (e.getSource() == repository)
{
repositoryID = (String)repository.getSelectedItem();
}
if(e.getSource() == environment)
{
environmentID = (String)environment.getSelectedItem();
}
}
}
Here's a link to a picture of the problem:
If anyone could help that would be awesome.
It doesn't appear to be the issue you were suffering from, but I found this post due to the same resulting issue of the arrow disappearing.
In my case it was due to me mistakenly using .removeAll() on the JComboBox rather than .removeAllItems() when I was attempting to empty and then reuse the JComboBox after a refresh of the data I was using. Just thought I'd include it as an answer in case someone else comes across this thread for similar reasons.
The code you show works, but it looks like you're fighting the enclosing container's default layout. Here, ComboTest is a JPanel which defaults to FlowLayout.
Addendum: In general, do not use absolute positioning, as shown in your update. I've changed the example to use GridLayout; comment out the setLayout() call to see the default, FlowLayout.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* #see https://stackoverflow.com/a/10824504/230513
*/
public class ComboTest extends JPanel {
private JComboBox repository = createCombo(new String[]{
"Click to select", "NSA", "Finance", "Test"});
private JComboBox environment = createCombo(new String[]{
"Click to select", "Dev", "Test", "Qual"});
public ComboTest() {
this.setLayout(new GridLayout(0, 1));
this.add(repository);
this.add(environment);
}
private JComboBox createCombo(String[] data) {
final JComboBox combo = new JComboBox(data);
combo.setSelectedIndex(1);
combo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand()
+ ": " + combo.getSelectedItem().toString());
}
});
return combo;
}
private void display() {
JFrame f = new JFrame("ComboTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ComboTest().display();
}
});
}
}
I had the same issue. I fixed it by revalidating and repainting the panel with the following code :
myPanel.revalidate();
myPanel.repaint();
Maybe a little late, but for those who are still looking for an easy and fail-safe way to use the JComboBox can use this:
public class FixedJComboBox<E>
extends JComboBox<E> {
// Copied constructors
public FixedJComboBox() {
super();
}
public FixedJComboBox(ComboBoxModel<E> aModel) {
super(aModel);
}
public FixedJComboBox(E[] items) {
super(items);
}
public FixedJComboBox(Vector<E> items) {
super(items);
}
#Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
// The arrow is the first (and only) component
// that is added by default
Component[] comps = getComponents();
if (comps != null && comps.length >= 1) {
Component arrow = comps[0];
// 20 is the default width of the arrow (for me at least)
arrow.setSize(20, height);
arrow.setLocation(width - arrow.getWidth(), 0);
}
}
}
As described here, the bug is caused by incorrectly setting both the location and the size of the arrow to (0,0), followed by some repainting issues. By simply overriding the setBounds() function, the arrow is always corrected after the UI/layout manager has wrongly updated the arrow.
Also, since new components are added after the old ones (i.e. higher index), the arrow will always be at the first element in the array (assuming you don't remove and re-add the arrow).
The disadvantage is of this class is that the width of the arrow is now determined by a constant instead of the UI/layout manager.

PropertyChangeSupport for SpinnerNumberModel

I want to listen to the changes of the value of the SpinnerNumberModel for a JSpinner.
I create a PropertyChangeSupport and put the model into it.
I need the propertyChangeListener, because it shows me the old and new value of the property.
The snippet doesn't work: the propertyChange method prints nothing, when I click on the JSpinner.
A simple ChangeListener give only the new value, but I need also the old value, how can I get it?
package de.unikassel.jung;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class PropertyChangeTest implements PropertyChangeListener {
public static void main(String[] args) {
new PropertyChangeTest();
}
public PropertyChangeTest() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int value = 1;
int min = 0;
int max = 10;
int step = 1;
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(value, min, max, step);
PropertyChangeSupport pcs = new PropertyChangeSupport(spinnerModel);
pcs.addPropertyChangeListener("value", this);
JSpinner spinner = new JSpinner(spinnerModel);
frame.getContentPane().add(spinner);
frame.setVisible(true);
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt);
System.out.println(evt.getSource());
}
}
Instead of listening to the model, listen to the editor's JFormattedTextField, as suggested below.
JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1));
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField().addPropertyChangeListener("value", this);
Monday morning ... classical time for not resisting a couple of comments :-)
#timaschew
"need the propertyChangeListener, because it shows me the old and new value of the property." - (nitpicking - but always have this strong urge to separate requirement and solution :), I think it's the other way round: on a change notification you need access to both the old and new value, a propertyChangeEvent/Listener is a notification type which supports it, there might be others
PropertyChangeSupport is not supposed to be used on part of the the observing code, it's supposed to be used on the obervable's side (just as #Hovercraft did in his example): it's sole responsibility is manage and notify the listeners registered to the observable
occasionally, accessibleContext provides a hook for hacks - nevertheless, it's a hack to hook into it (except you really need to support accessibility, which might well be the case :-) As with all hacks, that's a brittle solution which most probably will cause pain sometime in the future. Much more stable to follow the link about how Action and AbstractButton interact
#Hovercraft
enhancing the model with a richer change notification is the way-to-go (as in: my absolute favourite :-)
just a small detail: if you have a slave let him do all the work - PropertyChangeSupport has methods which take the old/new value, no need to feed to create an event on the observable. It will be thrown away anyway when old and new are equal
for newValue in the notification event, don't use the parameter but instead use getValue again (super might have rejected the change)
#trashgod
haha - you already guessed that I don't like solution: it breaks encapsulation in that it relies on an implementation detail, so don't except when in complete control of the JSpinner creation and are absolutely sure its editor is never changed
For a PropertyChangeSupport to work you need to call its firePropertyChange method, but more importantly the support object needs to have access to the setXXX method of the property that it is listening to, and in that method it needs to call PropertyChangeSupport's firePropertyChange method. And so I think for your idea to work, you'll need to extend the model's class, give it a PropertyChangeSupport object, give it the add and remove listener methods, and be sure to listen to changes made in the model's setValue method which is key. In my example that method looks like this:
#Override
public void setValue(Object newValue) {
// store old value and set the new one
Object oldValue = getValue();
super.setValue(newValue);
// construct the event object using these saved values
PropertyChangeEvent evt = new PropertyChangeEvent(this, VALUE, oldValue,
newValue);
// notify all of the listeners
pcs.firePropertyChange(evt);
}
Here's my sample model class that uses PropertyChangeSupport:
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings("serial")
class MySpinnerNumberModel extends SpinnerNumberModel {
public static final String VALUE = "value";
private SwingPropertyChangeSupport pcs = new SwingPropertyChangeSupport(this);
// you will likely need to create multiple constructors to match
// the ones available to the SpinnerNumberModel class
public MySpinnerNumberModel(int value, int min, int max, int step) {
super(value, min, max, step);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
#Override
public void setValue(Object newValue) {
// store old value and set the new one
Object oldValue = getValue();
super.setValue(newValue);
// construct the event object using these saved values
PropertyChangeEvent evt = new PropertyChangeEvent(this, VALUE, oldValue,
newValue);
// notify all of the listeners
pcs.firePropertyChange(evt);
}
}
And finally the test class to test out the above class to see if it is working properly:
import java.beans.*;
import javax.swing.*;
public class TestSpinnerPropChange {
private static void createAndShowUI() {
final MySpinnerNumberModel myModel = new MySpinnerNumberModel(1, 0, 10, 1);
final JSpinner spinner = new JSpinner(myModel);
final JTextField oldValueField = new JTextField(10);
final JTextField newValueField = new JTextField(10);
JPanel panel = new JPanel();
panel.add(spinner);
panel.add(new JLabel("old value:"));
panel.add(oldValueField);
panel.add(new JLabel("new value:"));
panel.add(newValueField);
myModel.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// checking the property name is overkill here, but is a good habit
// to get into, especially if listening to more than one property.
if (evt.getPropertyName().equals(MySpinnerNumberModel.VALUE)) {
oldValueField.setText(evt.getOldValue().toString());
newValueField.setText(evt.getNewValue().toString());
}
}
});
JFrame frame = new JFrame("TestSpinnerPropChange");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html#bound
You have to fireThePropertyChange in the setters.

JInternalFrame selection

I have a JDesktopPane containing some JInternalFrames. I want some menus on the menubar to be activated only when one of the JInternalFrames is selected. I've tried using VetoableChangeListener, with the following code in it:
JInternalFrame selectedFrame = desk.getSelectedFrame();
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
But the results are not what I expected - for example, the menu is enabled only the second time I add a frame. when I close all frames, it remains enabled.
How can I make this work?
you have to read basic tutorial about JInternalFrames with link to the InternalFrameListener,
but another and look like as better way is programatically to know those event in all cases and evety times is by adding PropertyChangeListener as shows examples Getting All Frames in a JDesktopPane Container, by adding PropertyChangeListener you can listeng for these events
Add an InternalFrameListener to each internal frame added to the desktop pane, and each time an event is triggered, execute the code you have shown in your question.
This code could be better written though:
setEnabled takes a primitive boolean as argument, not a java.lang.Boolean. Use true and false rather than Boolean.TRUE and Boolean.FALSE.
The expression (selectedFrame != null) evaluates as a boolean. Just write
imageMenu.setEnabled(selectedFrame != null);
instead of
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
I would just create a custom event and fire it when a JInternalFrame gets focus (isActivated).
The menu items would listen for this event, intercept it and set their status enabled or disabled accordingly.
The advantage here is that you don't have to handle what menu items should be available for which types of internal frames, just fire the appropriate event. It'll make your life easier if you add more internal frames in the future.
This answer is based on the answer by #mKorbel. This example shows one of the ways to detect focus between internal frames as is demonstrated here:
package com.apexroot.sandbox;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
/**
* author grants unlimited license to modify, reuse and redistribute. based on
* the suggestion by #mKorbel on stackoverflow at
* http://stackoverflow.com/questions/7219860/jinternalframe-selection
* please keep a URL to the original version in the source code.
* http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
*
* #author Apexroot
*/
public class InternalFrameFocusListenerExample {
public static final String INTERNAL_FRAME_FOCUS_EVENT_PROPERTY = "selected";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final JFrame jFrame = new JFrame();
final JDesktopPane jDesktopPane = new JDesktopPane();
final JInternalFrame[] jInternalFrames = new FocusInternalFrame[3];
for (int i = 0; i < jInternalFrames.length; i++) {
jInternalFrames[i] = new FocusInternalFrame();
}
jFrame.dispose();
jFrame.setContentPane(jDesktopPane);
jDesktopPane.setPreferredSize(new Dimension(400, 200));
jFrame.pack();
jFrame.setVisible(true);
for (int i = 0; i < jInternalFrames.length; i++) {
jDesktopPane.add(jInternalFrames[i]);
jInternalFrames[i].setLocation(10 + 60 * i, 10 + 40 * i);
jInternalFrames[i].setVisible(true);
}
}
});
}
public static class FocusInternalFrame extends JInternalFrame {
public FocusInternalFrame() {
final JLabel jLabel = new JLabel("placeholder for pack();");
setContentPane(jLabel);
pack();
this.addPropertyChangeListener(
INTERNAL_FRAME_FOCUS_EVENT_PROPERTY,
new LabelFocusListener(jLabel));
}
}
private static class LabelFocusListener implements PropertyChangeListener {
private final JLabel jLabel;
public LabelFocusListener(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// please keep a URL to the original version in the source code.
// http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
if (INTERNAL_FRAME_FOCUS_EVENT_PROPERTY.equals(
evt.getPropertyName())) {
final Object oldValue = evt.getOldValue();
final Object newValue = evt.getNewValue();
if (oldValue instanceof Boolean
&& newValue instanceof Boolean) {
boolean wasInFocus = (Boolean) oldValue;
boolean isInFocus = (Boolean) newValue;
if (isInFocus && !wasInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus gained
jLabel.setText("focus gained");
}
});
} else if (wasInFocus && !isInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus lost
jLabel.setText("focus lost");
}
});
}
}
}
}
}
}

Categories

Resources