deleting menu items remotely - java

consider the following code:
mntmProfilesDelete.get(index).addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
JMenuItem eMntm = (JMenuItem) e.getSource();
String text = eMntm.getText();
Component[] mns = mnDelete.getParent().getComponents();
for(Component mn : mns)
{
System.err.println((String)(((JMenu)mn).getText()));
if(mn instanceof JMenu && ((JMenu)mn).getText().toLowerCase().equals("open"))
{
System.err.println((String)(((JMenu)mn).getText()));
Component[] mntms = ((JMenu) mn).getComponents();
for(Component mntm : mntms)
{
System.err.println((String)(((JMenu)mn).getText())+"\n"+(String)(((JMenuItem)mntm).getText()));
if(mntm instanceof JMenuItem && ((JMenuItem)mntm).getText().toLowerCase().equals(text.toLowerCase()))
{
System.err.println((String)(((JMenu)mn).getText())+"\n"+(String)(((JMenuItem)mntm).getText()));
((JMenu)mn).remove((JMenuItem)mntm);
}
}
}
}
mnDelete.remove(eMntm);
}
}
which I'm using to delete two menu items, like below:
(jmenu)father
(jmenu)open
(jmenuitem)item1
(jmenuitem)item2
etc
(jmenu)delete
(jmenuitem)item1
(jmenuitem)item2
etc
the action listener is attached to -item1- below -delete-
for some reason the -item1- below -open- doesn't get removed using the above code. I can't seem to understand why.
many thanks.

JMenu's remove(Component c) JavaDoc says: "Removes the component c from this menu."
AFAIC interpret from looking at the last line of your code (my weekend mind refused to dig through the casting galore[1] ;-) and your explanation: your this menu mnDelete is /father/delete. The item you're trying to remove (/father/open/item1) is not in this menu.
[1] Why the heck are you casting public String getText() to (String)?

So I would save off my menu that I want to modify so I can easily perform this change. As opposed to trying to wind through the hierarchy. This will make it much easier to read your code. For example:
public SomeApp {
JMenu openMenu;
JMenu deleteMenu;
public JMenu buildMenus(List<SomeObject> objsThatGoInMenu) {
openMenu = new JMenu("Open");
deleteMenu = new JMenu("Delete");
for( SomeObject so : objsThatGoInMenu ) {
addMenuOptions( so );
}
JMenu father = new JMenu("Father");
father.add( openMenu );
father.add( deleteMenu );
}
public addMenuOptions( final SomeObject so ) {
final JMenuItem openMenuItem = new JMenuItem( new AbstractAction( so.getName() ) {
public void actionPerformed(ActionEvent evt) {
// todo open
}
}));
final JMenuItem deleteMenuItem = new JMenuItem( new AbstractAction( so.getName() ) {
public void actionPerformed(ActionEvent evt) {
deleteMenu.remove( deleteMenuItem );
openMenu.remove( openMenuItem );
}
}));
openMenu.add( openMenuItem );
deleteMenu.add( deleteMenuItem );
}
}

solved it, every part of my code works as it should except:
Component[] mntms = ((JMenu) mn).getComponents();
which does not return any JMenuItems, it should be:
Component[] mntms = ((JMenu) mn).getMenuComponents();

Related

Java - Select all item in the menu

I'm using the javax.swing library, and I try to solve this problem:
I have a MenuBar in which I created JMenu, this menu has JCheckBoxMenuItem items. Like this:
//creating objects:
jMenuBar = new javax.swing.JMenuBar();
jMenuAlgorithms = new javax.swing.JMenu();
jCheckBoxSPEA = new javax.swing.JCheckBoxMenuItem();
jCheckBoxNSGAII = new javax.swing.JCheckBoxMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jCheckBoxMenuEnableAll = new javax.swing.JCheckBoxMenuItem();
jCheckBoxMenuDisableAll = new javax.swing.JCheckBoxMenuItem();
//settings and putting them together:
jCheckBoxSPEA.setSelected(true);
jCheckBoxSPEA.setText("SPEA");
jMenuAlgorithms.add(jCheckBoxSPEA);
jCheckBoxNSGAII.setSelected(true);
jCheckBoxNSGAII.setText("NSGAII");
jMenuAlgorithms.add(jCheckBoxNSGAII);
jMenuAlgorithms.add(jSeparator1);
jCheckBoxMenuEnableAll.setSelected(true);
jCheckBoxMenuEnableAll.setText("Enable all");
jCheckBoxMenuEnableAll.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jCheckBoxMenuEnableAllMouseClicked(evt);
}
});
jMenuAlgorithms.add(jCheckBoxMenuEnableAll);
jCheckBoxMenuDisableAll.setText("Disable all");
jMenuAlgorithms.add(jCheckBoxMenuDisableAll);
jMenuBar.add(jMenuAlgorithms);
If the user selects jCheckBoxMenuEnableAll item, I would like to select all the items above the separator. If he selects jCheckBoxMenuDisableAll, I would like to deselect all the items above the separator.
As you can see, I've added mouseClicked action to the jCheckBoxMenuEnableAll item. Now, I would like to do something like this:
private void jCheckBoxMenuEnableAllMouseClicked(java.awt.event.MouseEvent evt) {
for(JCheckBoxMenuItem item : jMenuAlgorithms){
item.setSelected(true);
}
//deselect then jCheckBoxMenuDisableAll, it's not essential for instance
}
But apparently, I can't do the for loop like this, as the menu item isn't an array or Iterable.
Well, just for testing, I had done something very stupid (code below) - I pass all the items in the menu, and if the item is a check box, I make its copy, set ist value to "true" (selected) and then replace the original item by its copy. Very stupid, I know and I absolutely don't want to do like this, however, I didn't find another way to do it. I just wanted to see if this would work. I supposed it should, but it stoll doesn't work. What am I doing wrong? Thank you very, very much for your time.
private void jCheckBoxMenuEnableAllMouseClicked(java.awt.event.MouseEvent evt) {
if(jCheckBoxMenuEnableAll.isSelected()){
for(int i =0; i< jMenuAlgorithms.getItemCount(); i++){ //for all items in the menu, separators included
if(jMenuAlgorithms.getItem(i) instanceof JCheckBoxMenuItem){
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)jMenuAlgorithms.getItem(i));
item.setSelected(true);
jMenuAlgorithms.insert(item, i);
}
}
}
}
I think JPopupMenu#getSubElements() is what you are looking for.
see also: JMenu#getSubElements()
Returns an array of MenuElements containing the submenu for this menu
component. If popup menu is null returns an empty array. This method
is required to conform to the MenuElement interface. Note that since
JSeparators do not conform to the MenuElement interface, this array
will only contain JMenuItems.
import java.awt.*;
import java.util.Arrays;
import java.util.stream.Stream;
import javax.swing.*;
public class MenuSubElementsTest {
public JComponent makeUI() {
JMenu jMenuAlgorithms = new JMenu("MenuAlgorithms");
JMenuItem jCheckBoxMenuEnableAll = new JMenuItem("Enable all");
jCheckBoxMenuEnableAll.addActionListener(e -> {
for (MenuElement me: jMenuAlgorithms.getPopupMenu().getSubElements()) {
System.out.println("debug1: " + me.getClass().getName());
if (me instanceof JCheckBoxMenuItem) {
((JCheckBoxMenuItem) me).setSelected(true);
}
}
//or: getJCheckBoxMenuItem(jMenuAlgorithms).forEach(r -> r.setSelected(true));
});
JMenuItem jCheckBoxMenuDisableAll = new JMenuItem("Disable all");
jCheckBoxMenuDisableAll.addActionListener(e -> {
getJCheckBoxMenuItem(jMenuAlgorithms).forEach(r -> r.setSelected(false));
});
jMenuAlgorithms.add(new JCheckBoxMenuItem("SPEA", true));
jMenuAlgorithms.add(new JCheckBoxMenuItem("NSGAII", true));
jMenuAlgorithms.addSeparator();
jMenuAlgorithms.add(jCheckBoxMenuEnableAll);
jMenuAlgorithms.add(jCheckBoxMenuDisableAll);
JMenuBar jMenuBar = new JMenuBar();
jMenuBar.add(jMenuAlgorithms);
JPanel p = new JPanel(new BorderLayout());
p.add(jMenuBar, BorderLayout.NORTH);
return p;
}
private static Stream<JCheckBoxMenuItem> getJCheckBoxMenuItem(MenuElement p) {
Class<JCheckBoxMenuItem> clz = JCheckBoxMenuItem.class;
return stream(p).filter(clz::isInstance).map(clz::cast);
}
// public static Stream<MenuElement> stream(MenuElement p) {
// return Arrays.stream(p.getSubElements())
// .map(MenuSubElementsTest::stream).reduce(Stream.of(p), Stream::concat);
// }
public static Stream<MenuElement> stream(MenuElement p) {
return Stream.concat(Stream.of(p), Arrays.stream(p.getSubElements())
.peek(me -> System.out.println("debug2: " + me.getClass().getName()))
.flatMap(MenuSubElementsTest::stream));
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MenuSubElementsTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

How to clear results using JMenuItem

What code do I use for the JMenuItem Clear in order to clear results from the memory in Java GUI appliation? I know how to clear from a JText field, by using the object.setText("");, but how do I clear results using a JMenuItem?
Your question is a bit vague, so I'll try and answer what I think your asking...
Removing from a List using JMenuItems
Remove things from somewhere else in response to clicking on the JMenuItem:
Item class:
public class Item {
public static final String name;
public static int cost;
public Item(String name, int cost) {
this.name = name;
this.int = int;
}
}
Main class:
public class Main {
public static void main() {
List<Item> list = new LinkedList<>();
JMenu menu = new JMenu();
menu.add( new JMenuItem(
new AbstractAction("Add Carwash") {
public void actionPerformed(ActionEvent e) {
list.add( new Item("CarWash", 10) );
}
}
);
menu.add( new JMenuItem(
new AbstractAction("Oil Change") {
public void actionPerformed(ActionEvent e) {
list.add( new Item("Oil Change", 10) );
}
}
);
menu.add( new JMenuItem(
new AbstractAction("Clear") {
public void actionPerformed(ActionEvent e) {
list.clear();
}
}
);
menu.add( new JMenuItem(
new AbstractAction("Total") {
public void actionPerformed(ActionEvent e) {
System.out.println(
list.stream().mapToInt().sum()
);
}
}
);
}
}
Note that this is not complete, may not not compile, and does not actually display anything yet (add the menu to a JFrame).

Java Swing: how to stop unwanted shift-tab keystroke action

When I have a JTextField in a JPanel and it has focus, pressing "tab" doesn't do anything... but pressing "shift-tab" causes focus to be lost (FocusEvent.getOppositeComponent() is null).
If there are other focusable components on the JPanel (or rather under the "focus cycle root") this doesn't happen: instead, they get the focus on shift-tab.
In the SSCCE below I demonstrate this... each time you press Return in the search box you add a row to the JTable, which causes it to become focusable. You can also uncomment the line which makes the JRadioButtons unfocusable.
I looked at the InputMaps as well to see whether shift-tab is somehow involved there ... not at all.
I also tried experimenting with FocusTraversalPolicy to see whether I could understand the problem. No joy.
My goal: to stop "shift-tab" causing a loss of focus (focus disappears) when there is a single focusable component in the focus cycle root's ambit.
later
a workaround is to add the line
if( oppComp == null ){ impl.searchBox.requestFocus(); }
at the end of the focusLost method of the search box's FocusListener ... but for me this is only a workaround... 1) it doesn't solve the problem through understanding of the focus traversal mechanism; 2) there might be circs when you would need the focus to be lost from the search box...
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
class BackTabProb {
JFrame mainFrame;
JTextField searchBox;
JTable resultsTable;
public static void print(String msg) {
System.out.println(msg);
}
public static void main(String[] a_args) throws InvocationTargetException, InterruptedException {
final BackTabProb impl = new BackTabProb();
class Show implements Runnable {
public void run() {
impl.mainFrame = new JFrame("Back Tab problem");
impl.mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
impl.mainFrame.dispose();
}
});
impl.resultsTable = new JTable();
impl.resultsTable.setFocusable(false);
Vector<Object> dataVector = new Vector<Object>();
Vector<Object> colIdentifiers = new Vector<Object>(Arrays.asList(new String[] { "ONE", "TWO" }));
((DefaultTableModel) impl.resultsTable.getModel()).setDataVector(dataVector, colIdentifiers);
JScrollPane jsp = new JScrollPane(impl.resultsTable);
JPanel northPanel = new JPanel();
northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
impl.searchBox = new JTextField("Enter search text", 10);
JLabel label = new JLabel("Search:");
label.setLabelFor(impl.searchBox);
northPanel.add(label);
northPanel.add(impl.searchBox);
ButtonGroup buttonGroup = new ButtonGroup();
ArrayList<JRadioButton> indexButtons = new ArrayList<JRadioButton>();
for (int i = 0; i < 2; i++) {
JRadioButton indexButton = new JRadioButton(i == 0 ? "Stemmer" : "Simple");
// commenting this out means back-tabbing from search box does not result
// in focus going "nowhere"
indexButton.setFocusable(false);
buttonGroup.add(indexButton);
northPanel.add(indexButton);
}
impl.mainFrame.getContentPane().setLayout(new BorderLayout());
impl.mainFrame.getContentPane().add(northPanel, BorderLayout.NORTH);
impl.mainFrame.getContentPane().add(jsp, BorderLayout.CENTER);
impl.mainFrame.pack();
impl.mainFrame.setVisible(true);
print("=== visible");
}
}
EventQueue.invokeAndWait(new Show());
class AddMore implements Runnable {
public void run() {
impl.mainFrame.setFocusTraversalPolicyProvider(true);
class SearchBoxFocusListener implements FocusListener {
public void focusGained(FocusEvent focusEvent) {
print("=== search box got focus");
impl.searchBox.selectAll();
}
public void focusLost(FocusEvent focusEvent) {
Component oppComp = focusEvent.getOppositeComponent();
print(String.format("=== search box lost focus to %s",
oppComp == null ? "nowhere" : oppComp.getClass()));
}
}
impl.searchBox.addFocusListener(new SearchBoxFocusListener());
class SearchBoxActionListener implements ActionListener {
public void actionPerformed( ActionEvent actionEvent ){
if( actionEvent.getSource() != null ){
((DefaultTableModel)impl.resultsTable.getModel()).insertRow( 0, new Object[]{ "blip", "blap" });
// as soon as the table has at least one row it is set to "focusable"
// commenting this out means back-tabbing from search box results
// in focus going "nowhere"
impl.resultsTable.setFocusable( true );
}
}
}
impl.searchBox.addActionListener( new SearchBoxActionListener() );
ActionMap am = impl.searchBox.getActionMap();
print("=== ActionMap");
for (Object key : am.allKeys()) {
print(String.format(" === action key %s", key));
}
for (int i = 0; i < 3; i++) {
print(String.format("=== InputMap type %d", i));
InputMap im = impl.searchBox.getInputMap(i);
KeyStroke[] allKeys = im.allKeys();
if (allKeys != null) {
for (KeyStroke ks : allKeys) {
print(String.format(" === keystroke %s object %s", ks, im.get(ks)));
}
}
}
// various experiments with FocusTraversalPolicy... NB LayoutTraversalPolicy
// is what the framework uses here by default
class MainFrameFocusTraversalPolicy extends LayoutTraversalPolicy {
public Component getComponentAfter(Container arg0, Component arg1) {
Component comp = super.getComponentAfter(arg0, arg1);
print(String.format("=== comp after %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getComponentBefore(Container arg0, Component arg1) {
Component comp = super.getComponentBefore(arg0, arg1);
print(String.format("=== comp before %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getDefaultComponent(Container arg0) {
Component comp = super.getDefaultComponent(arg0);
print(String.format("=== default comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
public Component getFirstComponent(Container arg0) {
Component comp = super.getFirstComponent(arg0);
print(String.format("=== first comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
// return impl.searchBox;
}
public Component getLastComponent(Container arg0) {
Component comp = super.getLastComponent(arg0);
print(String.format("=== last comp %s", comp == null ? "Null" : comp.getClass()));
return comp;
}
protected boolean accept(Component comp) {
boolean accept = super.accept(comp);
print(String.format("=== accept %s? %s", comp == null ? "Null" : comp.getClass(), accept));
return accept;
}
}
impl.mainFrame.setFocusTraversalPolicy(new MainFrameFocusTraversalPolicy());
}
}
EventQueue.invokeAndWait(new AddMore());
}
}
Maybe you can use Container#setFocusTraversalKeys(...) method:
Set<AWTKeyStroke> backwardKeys = Collections.emptySet();
//alone JTextField(pointed out by #mKorbel): impl.mainFrame.setFocusTraversalKeys(
impl.searchBox.setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardKeys);
this still comes under the workaround heading for me... but it's a bit more useful than the workaround I gave under "later"
if( oppComp == null ){
final Component srcComp = (Component)focusEvent.getSource();
FocusTraversalPolicy ftp = impl.mainFrame.getFocusTraversalPolicy();
Component lastComp = ftp.getLastComponent( impl.mainFrame );
Component beforeComp = ftp.getComponentBefore( impl.mainFrame, srcComp );
if( lastComp == beforeComp ){
EventQueue.invokeLater( new Runnable(){
public void run() {
if( impl.mainFrame.isFocused()){
srcComp.requestFocus();
};
}});
}
}
Indeed, this is the situation faced by a single focusable component: on Shift-Tab the focus traversal policy then finds that the "before" comp is the same as the "last" comp. Under these circs component focus (as opposed to window focus) appears to vanish. The odd thing is that on Tab (traverse forwards), when the "after" comp is the same as the "first" comp, it doesn't. Is this a bug?
Anyway, having ascertained this you then have to check the focus of the window asynchronously... so that focus can be allowed to go to another window. Results in a slight flicker (boo!) on Shift-Tab.

How to cut/copy/paste text to JTextField when some JComponent is clicked?

I'm creating custom popup menu, using just extended JComponent as a menu items and extended JWindow to hold them. My question is - how to send signal from JComponent instance when it's clicked (has MouseListener) to JTextField to perform cut/copy/paste actions?
EDIT:
I will try to explain more precisely.
JTextField class (simplified):
public class TextInputField extends JTextField implements FocusListener {
private MenuPopupWindow popUp;
public TextInputField() {
popUp = new MenuPopupWindow();//MenuPopupWindow class extends JWindow
MenuItem paste = new MenuItem("Paste",
new ImageIcon(getClass().getResource("/images/paste_icon.png")),
"Ctrl+V");//MenuItem class extends JComponent, has implemented MouseListener - and when mouseClicked(MouseEvent e) occurs, somehow action signal have to be sent to this class
MenuItem copy = ....
MenuItem cut = ....
Action pasteAction = getActionMap().get(DefaultEditorKit.pasteAction);
paste.setAction(pasteAction);//How to make it to work?
popUp.addMenuItem(paste);
popUp.addMenuItem(cut);
popUp.addMenuItem(copy);
}
}
How to do it right?
I'm creating custom popup menu, using just extended JComponent as a menu items and extended JWindow to hold them.
Not really sure what all that means.
You should just use a JPopupMenu and add JMenuItems to it. Read the section from the Swing tutorial on Bringing Up a Popup Menu for an example.
Then, if you want cut/copy/paste functionality, you can use the default actions provided by the DefaultEditorKit:
popup.add( new JMenuItem(new DefaultEditorKit.CopyAction()) );
In light of your posted code, I think all you need to do in your TextInputField class, is add:
paste.addActionListener(pasteAction);
then in your MenuItem class you have to put in code to call those action listeners.
public class MenuItem implements MouseListener
{
...
#Override public void mouseClicked(MouseEvent event)
{
ActionListener[] listeners = (ActionListener[])
MenuItem.this.getListeners(ActionListener.class);
for(int i = 0; i < listeners.length; i++)
{
listeners[i].actionPerformed
(
new ActionEvent(MenuItem.this,someID, someCMDName)
);
}
}
In your class that extends JComponent (I'll call it class 'A') you will need to get a reference to your JTextField. A simple way to do this is to add a private instance variable of type JTextField to class A, and pass in the JTextField through the constructor.
so your class should look something like this:
public class A extends JComponent implements ActionListener
{
private JTextField updateField;
public A(JTextField updateField[,<your other contructor arguments>...])
{
this.updateField = updateField;
this.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource().equals(this)
{
//copy, paste or do whatever with the JTextField
//by way of this.updateField;
//e.g. this.updateField.setText(...);
//or to simply pass the event along to the JTextField's handlers
//this.updateField.dispatchEvent(event);
}
}
}
then you just have to remember to pass the jtextField into the constructor when you create your component
So my working example follows (simplified):
public class TextInputField extends JTextField {
private MenuPopupWindow popUp;
private MenuItem copy,
cut,
paste,
selectAll;
public TextInputField() {
popUp = new MenuPopupWindow();
paste = new MenuItem(this, "Paste", new ImageIcon(getClass().getResource("/images/Paste-icon.png")), "Ctrl+V");
cut = new MenuItem(this, "Cut", new ImageIcon(getClass().getResource("/images/Cut-icon.png")), "Ctrl+X");
copy = new MenuItem(this, "Copy", new ImageIcon(getClass().getResource("/images/Copy-icon.png")), "Ctrl+C");
selectAll = new MenuItem(this, "Select All", null, "Ctrl+A");
popUp.addMenuItem(paste);
popUp.addMenuItem(cut);
popUp.addMenuItem(copy);
popUp.addMenuItem(selectAll);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
if (getSelectedText() == null) {
copy.setEnabled(false);
cut.setEnabled(false);
} else {
copy.setEnabled(true);
cut.setEnabled(true);
}
if (getText().equals("")) {
selectAll.setEnabled(false);
} else {
selectAll.setEnabled(true);
}
Clipboard c = getToolkit().getSystemClipboard();
Transferable t = c.getContents(this);
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String s;
try {
s = (String) t.getTransferData(DataFlavor.stringFlavor);
if (s.equals("")) {
paste.setEnabled(false);
} else {
paste.setEnabled(true);
}
} catch (UnsupportedFlavorException | IOException ex) {
Logger.getLogger(TextInputField.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
paste.setEnabled(false);
}
popUp.setLocation(e.getXOnScreen(), e.getYOnScreen());
getCaret().setVisible(false);
popUp.setVisible(true);
} else {
Object obj = e.getSource();
if (obj instanceof MenuItem) {
MenuItem menuItem = (MenuItem) obj;
if (paste == menuItem) {
paste();
} else if (cut == menuItem) {
cut();
} else if (copy == menuItem) {
copy();
} else if (selectAll == menuItem) {
selectAll();
}
}
getCaret().setVisible(true);
popUp.setVisible(false);
}
}
});
}
}
And at MenuItem class added (simplified):
#Override
public void mouseClicked(MouseEvent e) {
textField.dispatchEvent(e);
}
Works superb :)
Decided to use Component instead of JComponent in MenuItem class, because there is no need for paintComponent, paintBorder and paintChildren constructors - saving resources.

Java Swing Combobox removeAllItems calling ItemStateChanged also?

My code is quite simple actually. I saw a simple and similar code was from this article.
At first, I have 1 combobox. I have a listener on it called itemStateChanged(). My purpose to add into this listener is that; "to execute some code when user click (select) an item from its dropbox".
Cmb_ItemCategory = new javax.swing.JComboBox();
Cmb_ItemCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Loading..." }));
Cmb_ItemCategory.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
Cmb_ItemCategoryItemStateChanged(evt);
}
});
private void Cmb_ItemCategoryItemStateChanged(java.awt.event.ItemEvent evt) {
if(evt.getStateChange() == java.awt.event.ItemEvent.SELECTED){
System.err.println("Sombody click or change my model content");
}
}
Behind the code, I grab some data, and then calling a method of removeAllItems() .
And then I set a new Model (from new data) into it.
-- at another line of code ---
Cmb_ItemCategory.removeAllItems();
Cmb_ItemCategory.setModel(newModel);
I juz realized that my itemStateChanged() is called when i do the removeAllItem() method. called once.
So, How to make it only called once user Click (select) only AND not when removeAllItems() called?
it similar to this article. But it's not removingItems case. CMIIW.
Here check this code out, this works flawlessly, might be you doing something wrong where you calling removeAllItems() :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboState
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Combo State Testing : ");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JComboBox cbox = new JComboBox();
cbox.addItem("One");
cbox.addItem("Two");
cbox.addItem("Three");
cbox.addItem("Four");
cbox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
System.out.println("Item Selected is : " + ie.getItem());
}
/*else
{
System.out.println("I am called for other reasons.");
}*/
}
});
/*
* Click me to remove JComboBox's items.
*/
JButton removeButton = new JButton("REMOVE");
removeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cbox.removeAllItems();
}
});
frame.add(cbox, BorderLayout.CENTER);
frame.add(removeButton, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ComboState().createAndDisplayGUI();
}
});
}
}
As nIcE cOw already illustrated in his example, it should certainly work when you use a DefaultComboBoxModel (which is the case in his sample code, although it happens behind the screens).
I could explain the behavior you encounter for the non-DefaultComboBoxModel case, although your code snippet suggests you use one. Looking at the source code for JComboBox#removeAllItems there is a different code path since the removeAllElements method is not part of the MutableComboBoxModel interface
public void removeAllItems() {
checkMutableComboBoxModel();
MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel;
int size = model.getSize();
if ( model instanceof DefaultComboBoxModel ) {
((DefaultComboBoxModel)model).removeAllElements();
}
else {
for ( int i = 0; i < size; ++i ) {
E element = model.getElementAt( 0 );
model.removeElement( element );
}
}
selectedItemReminder = null;
if (isEditable()) {
editor.setItem(null);
}
}
So with a non-DefaultComboBoxModel you are going to remove the items one by one. This means that at a certain point in time, you will remove the selected element. A possible implementation of your model might change the selected element at that point. If you look for example at the implementation in DefaultComboBoxModel (although this code will not be triggered) you can clearly see it changes the selection.
public void removeElementAt(int index) {
if ( getElementAt( index ) == selectedObject ) {
if ( index == 0 ) {
setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
}
else {
setSelectedItem( getElementAt( index - 1 ) );
}
}
objects.removeElementAt(index);
fireIntervalRemoved(this, index, index);
}
Perhaps your model does something similar, which explains the event. Just for making this post complete, the code behind the DefaultComboBoxModel#removeAllElements where you can clearly see it sets the selection to null and does not select another object. Only weird thing in that code is that it does not fire a DESELECTED event first, although you know the selection has changed if you listen for the intervalRemoved event ... but that is not really relevant to your problem
public void removeAllElements() {
if ( objects.size() > 0 ) {
int firstIndex = 0;
int lastIndex = objects.size() - 1;
objects.removeAllElements();
selectedObject = null;
fireIntervalRemoved(this, firstIndex, lastIndex);
} else {
selectedObject = null;
}
}
So to conclude: I say the solution for your problem is located in your model, and not in the code you posted
One quick way to do is to have a bool set to true before you call removeAllItem() and back false after. Execute the code in your itemStateChanged() only if the bool variable is false.
Ideally you could override the removeAllItem() function.
not clear from whole discusion,
you have to remove all Listener(s) from JComboBox before removing all Items, after Items are removed you can add Listener(s) back,
still not sure if you want to add & remove Items dynamically, or you can set whatever value for another JComponent(s),
(against complicating simple things) did you see there remove,
.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
public ComboBoxTwo() {
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
mainComboBox = new JComboBox(items);
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//prevent action events from being fired when the up/down arrow keys are used
//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox();// Create sub combo box with multiple models
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
subComboBox.addItemListener(this);
getContentPane().add(subComboBox, BorderLayout.EAST);
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
subItems.put(items[1], subItems1);
String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
subItems.put(items[2], subItems2);
String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[3], subItems3);
// mainComboBox.setSelectedIndex(1);
}
#Override
public void actionPerformed(ActionEvent e) {
String item = (String) mainComboBox.getSelectedItem();
Object o = subItems.get(item);
if (o == null) {
subComboBox.setModel(new DefaultComboBoxModel());
} else {
subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
}
}
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == mainComboBox) {
if (mainComboBox.getSelectedIndex() != 0) {
FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... ");
}
}
}
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent, String winTitle, String msgString) {
super(parent, winTitle);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel = new JLabel(msgString);
JButton bNext = new JButton("Stop Processes");
add(myLabel, BorderLayout.CENTER);
add(bNext, BorderLayout.SOUTH);
bNext.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
t.setRepeats(false);
t.start();
setLocationRelativeTo(parent);
setSize(new Dimension(400, 100));
setVisible(true);
}
}
public static void main(String[] args) {
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The method removeAllItems does not call ItemStateChanged, but it call actionPerformed, you can check it by running this simple code:
public class Tuto {
public static void main(String[] args) {
//create the main frame
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setLocation(new Point(10, 10));
frame.setPreferredSize(new Dimension(400, 300));
JComboBox<String> combo = new JComboBox();
combo.addItem("item 1");
combo.addItem("item 2");
combo.addItem("item 3");
combo.setBounds(50, 30, 300, 20);
combo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(" action Performed ");
}
});
frame.add(combo);
JButton button = new JButton("Remove");
button.setBounds(50, 100, 100, 30);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
combo.removeAllItems();
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
}

Categories