Manually resizeing a com.jidesoft.combobox.PopupPanel

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby td84 » Wed Nov 02, 2016 6:13 pm

Hello,

for some times ago i applied a code snippet to remove the cancel button from a CheckBoxListComboBox instance:
viewtopic.php?f=18&t=16394&p=80685&hilit=cancel+button&sid=70eaef581a5b6faef918b4201932a781#p80685
It worked good. The only problem it appears, if i try to set the width of the popuppanel, sometimes the popuppanel is right aligned, and somtimes left aligned.
I use this code, to set the size:
public class MyClass extends CheckBoxListComboBox {

@Override public void popupMenuWillBecomeVisible(PopupMenuEvent pme) {
isvisible = true;
//set the size of the popuppanel
if(this._popupPanel.getPreferredSize().width < 150) {
this._popupPanel.setPreferredSize( new Dimension(150, this._popupPanel.getSize().height) );
}
}

}

Left/Right aligned: the position of the popuppanel relative to the dropdown menu.
It seems, the popuppanel get's left aligned if there is a long text.
The right aligned popup contains only short texts.

Can you help to turn the right alignment into a left alignment?

Thank you,
Daniel
Attachments
left_aligned.jpg
popuppanel has left alignment
left_aligned.jpg (9.33 KiB) Viewed 38346 times
right_aligned.jpg
popuppanl has right alignment
right_aligned.jpg (4.93 KiB) Viewed 38346 times
td84
 
Posts: 32
Joined: Tue Dec 08, 2015 4:02 pm

Re: Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby JIDE Support » Mon Nov 07, 2016 9:58 am

Sorry for the late reply. The forum email notification is broken after we moved our server. It is fixed now.

Could you please send me a test case so that I can have the complete code to reproduce the issue?
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37250
Joined: Sun Sep 14, 2003 10:49 am

Re: Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby td84 » Tue Nov 15, 2016 3:22 pm

Welcome back :-)
Here is the code (and the image) to reproduce it.
Meanwhile i found another way to change the size of the popuppanel: i create a backup from the OK-Button, remove the Cancel-Button, and then readd the Ok, and an empty JPanel to the Buttonpanel and replace the layoutmanager of the Buttonpanel with a FlowLayoutmanager. So to resize the popuppanel, i change the size of the JPanel. (To do this uncomment the parts BLOCK1 and BLOCK2)
It's not so elegant :-), but the problem with the alignment disappears.


Code: Select all
//class MyList
package checkboxlist;

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.event.PopupMenuEvent;
import java.awt.*;

import com.jidesoft.combobox.CheckBoxListChooserPanel;
import com.jidesoft.combobox.CheckBoxListComboBox;
import com.jidesoft.combobox.MultiSelectListChooserPanel;
import com.jidesoft.converter.ConverterContext;



public class MyList extends CheckBoxListComboBox {
   private String[] _content;
   private boolean isvisible;
   private KeyboardFocusManager manager;
   
   
   public MyList(String[] content) {
      super(content, String[].class);
      _content = content;
      isvisible = false;
      
      
      setupShortcuts();
   }
   
   
   @Override protected MultiSelectListChooserPanel createListChooserPanel(ComboBoxModel dataModel, Class<?> clazz, ConverterContext converterContext) {
        return new CheckBoxListChooserPanel(dataModel, clazz, converterContext, getDialogOKAction(), getDialogCancelAction()) {
           @Override public Component createButtonPanel(int alignment) {
                Component buttonPanel = super.createButtonPanel(alignment);
                Component okbtn = null;
            
                for (Component comp: Main.getAllComponents((Container) buttonPanel)) {
                  if(comp.getClass() == JButton.class  &&  ((JButton)comp).getActionCommand().equalsIgnoreCase("cancel")) {
                     ((Container)buttonPanel).remove(comp);
                  }
               /*BLOCK1  if(comp.getClass() == JButton.class  &&  ((JButton)comp).getActionCommand().equalsIgnoreCase("ok")) {
                     ((Container)buttonPanel).remove(comp);
                     okbtn = comp;
                  }*/
                }
            
               //Change the size of the popuppanel:
               //1) create an empty panel as placeholder
               /*BLOCK2  JPanel empty = new JPanel();
               empty.setBorder(new EmptyBorder(1, 70, 0, 0));
               empty.setBackground(getBackground());
               
               //2) set a new layoutmanager
               FlowLayout myLayout = new FlowLayout();
               myLayout.setAlignment(FlowLayout.RIGHT);
               ((Container)buttonPanel).setLayout(myLayout);
               ((Container)buttonPanel).setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
               
               //3) readd the button + panel to the container + resize the button
               ((Container)buttonPanel).add(empty);
               ((Container)buttonPanel).add(okbtn);
               okbtn.setPreferredSize( new Dimension(okbtn.getPreferredSize().width + 30, okbtn.getPreferredSize().height) );*/
               
                return buttonPanel;
            }
        };
    }
   
   
   protected void setupShortcuts() {
      manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher( new KeyEventDispatcher() {
            @Override public boolean dispatchKeyEvent(KeyEvent e) {
                if (isvisible == true  &&  ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)  &&
                   e.getID() == KeyEvent.KEY_RELEASED  &&  e.getKeyCode() == KeyEvent.VK_A) {
                   setSelectedObjects(_content);
                }
                return false;
            }
        });
   }
   
   
   @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent pme) {
      isvisible = false;
   }
   
   
   @Override public void popupMenuWillBecomeVisible(PopupMenuEvent pme) {
      isvisible = true;
      if(this._popupPanel.getPreferredSize().width < 150) {
         this._popupPanel.setPreferredSize( new Dimension(150, this._popupPanel.getSize().height) );
      }
   }
   
}


Code: Select all
//class Main
package checkboxlist;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.util.*;


public class Main extends JFrame {
   public static void main(String s[]) {
      JFrame frame = new JFrame("JFrame Example");
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());
      JLabel label = new JLabel("The list: ");
      MyList lst = new MyList(new String[]{"first", "second", "third", "fourth"});
      panel.add(label);
      panel.add(lst);
      frame.add(panel);
      frame.setSize(300, 300);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
   
   public static ArrayList<Component> getAllComponents(final Container c) {
        Component[] comps = c.getComponents();
        ArrayList<Component> compList = new ArrayList<Component>();
        for (Component comp : comps) {
          compList.add(comp);
          if (comp instanceof Container) {
            compList.addAll((Collection<? extends Component>) getAllComponents((Container) comp));
          }
        }
        return compList;
    }
}
Attachments
reproduced.jpg
reproduced.jpg (17.65 KiB) Viewed 38320 times
td84
 
Posts: 32
Joined: Tue Dec 08, 2015 4:02 pm

Re: Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby JIDE Support » Wed Nov 16, 2016 10:18 am

I ran your test case by commenting out both BLOCK1 and 2. The comboBox popup shows up as right aligned.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37250
Joined: Sun Sep 14, 2003 10:49 am

Re: Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby td84 » Wed Nov 16, 2016 1:56 pm

JIDE Support wrote:I ran your test case by commenting out both BLOCK1 and 2. The comboBox popup shows up as right aligned.


That's mysterious. I'm using Java 1.7, Windows 7, Jide3.6.12. Below is, what i get by enabling / disabling BLOCK1/2.
Maybe the Java version causes this problem...
Attachments
left_right_aligned.JPG
left_right_aligned.JPG (28.11 KiB) Viewed 38315 times
td84
 
Posts: 32
Joined: Tue Dec 08, 2015 4:02 pm

Re: Manually resizeing a com.jidesoft.combobox.PopupPanel

Postby JIDE Support » Wed Nov 16, 2016 3:00 pm

You are right. They are indeed different.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37250
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: Google [Bot] and 6 guests