user806574
user806574

Reputation:

Why the Dialog is not shown in this situation?

I want to display a Dialog when I click on a userdefined component that I have created : I named its class name to ListBox. I want to simulate a LWUIT ComboBox with my user defined component which will accepts the blank area ; because LWUIT ComboBox doesn't accept the blank area.

The problem is that when my ListBox is at a coordinate such that there is no more space below it to display the Dialog then the Dialog is not shown ; but if there are more space below then the Dialog is shown.

Here is the captured image when the Dialog is not shown :

enter image description here

In the captured image the user defined ListBox is the component just above the two Buttons at the bottom of the phone screen.

And here are the codes :

public class ListBox extends Container implements ActionListener
{
    private Container cListBox = new Container(new BorderLayout());
    private Label[] tLabel;
    private int[] tLabelW;
    private int largestLabelW;
    private Label libelle = new Label();
    private Label arrow = new Label((MenuPrincipalForm.r).getImage("listboxarrow"));
    private int preferredWidth, preferredHeight, screenWidth, screenHeight;
    private Vector vData = new Vector();
    private final int leftPadding = 3;
    private CListCellListBox listRenderer;
    private List list;
    private Dialog dialog;
    private String selectedData;

    public ListBox(String[] lData, int prefHeight, int formWidth, int formHeight, int topMargin, int bottomMargin)
    {
        super(new FlowLayout(Component.CENTER));
        setFocusable(true);
        screenWidth = formWidth;
        screenHeight = formHeight;
        tLabel = new Label[lData.length + 1];
        tLabelW = new int[lData.length + 1];
        for (int i = 0 ; i < lData.length + 1 ; i++)
        {
            if (i < lData.length)
            {
                vData.addElement(new String(lData[i]));
                tLabel[i] = new Label(lData[i]);
                tLabelW[i] = tLabel[i].getPreferredW();
            }
            else
            {
                vData.addElement(new String(""));
                tLabel[i] = new Label("");
                tLabelW[i] = 0;
            }
        }
        largestLabelW = Comparator.max(tLabelW);
        preferredWidth = leftPadding + largestLabelW + arrow.getPreferredW();
        preferredHeight = prefHeight - 2 ;
        selectedData = String.valueOf(vData.lastElement());
        libelle.setText(String.valueOf(vData.lastElement()));
        libelle.setTextPosition(Label.LEFT);
        libelle.setPreferredW(preferredWidth);
        libelle.setPreferredH(preferredHeight);
        arrow.setAlignment(Label.CENTER);
        arrow.setPreferredH(preferredHeight);
        listRenderer = new CListCellListBox(false);
        list = (new CList(vData, false)).createList(listRenderer, this);
        list.setItemGap(0);
        list.setSelectedIndex(vData.indexOf(vData.lastElement()));
        list.getUnselectedStyle().setPadding(0, 0, 0, 0);
        list.getSelectedStyle().setPadding(0, 0, 0, 0);
        list.setPreferredW(leftPadding+preferredWidth+arrow.getPreferredW());
        dialog = new Dialog();
        dialog.setScrollableY(false);
        dialog.getContentPane().getSelectedStyle().setPadding(0, 0, leftPadding, 0);
        dialog.getContentPane().getUnselectedStyle().setPadding(0, 0, leftPadding, 0);
        dialog.addComponent(list);
        cListBox.addComponent(BorderLayout.WEST, libelle);
        cListBox.addComponent(BorderLayout.EAST, arrow);
        cListBox.setPreferredH(preferredHeight);
        getUnselectedStyle().setPadding(Component.LEFT, leftPadding);
        getSelectedStyle().setPadding(Component.LEFT, leftPadding);
        getUnselectedStyle().setBorder(Border.createLineBorder(1));
        getSelectedStyle().setBorder(Border.createLineBorder(1));
        addComponent(cListBox);
        setPreferredH(preferredHeight);
        getUnselectedStyle().setMargin(Component.TOP, topMargin);
        getSelectedStyle().setMargin(Component.TOP, topMargin);
        getUnselectedStyle().setMargin(Component.BOTTOM, bottomMargin);
        getSelectedStyle().setMargin(Component.BOTTOM, bottomMargin);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if ( (ae.getSource() instanceof List) && ((List)ae.getSource()).equals(list) )
        {
            dialog.dispose();
            if (list.getSelectedItem() instanceof Content)
            {
                Content valeur = (Content)list.getSelectedItem();
                selectedData = valeur.getEnreg();
                libelle.setText(selectedData);
                repaint();
            }
        }
    }
    public void setSelectedIndex(int idx)
    {
        list.setSelectedIndex(idx);
        selectedData = String.valueOf(vData.elementAt(idx));
        libelle.setText(String.valueOf(vData.elementAt(idx)));
        repaint();
    }
    public String getSelectedData()
    {
        return selectedData;
    }
    public void pointerPressed(int x, int y)
    {
        int espaceVertRestant, top, bottom, left, right;
        espaceVertRestant = screenHeight - ( libelle.getAbsoluteY() + preferredHeight );
        if (espaceVertRestant > list.getPreferredH())
        {
            top = getAbsoluteY() + preferredHeight - 1 ;
            bottom = screenHeight - ( getAbsoluteY() + preferredHeight + list.getPreferredH() ) ;
        }
        else
        {
            top = screenHeight - ( list.getPreferredH() + preferredHeight + espaceVertRestant ) ;
            bottom = getAbsoluteY() ;
        }
        left = getAbsoluteX() ;
        right = screenWidth - ( getAbsoluteX() + getPreferredW() );
        dialog.show(top, bottom, left, right, false, true);
    }
}

Here is the code of the Form where I add this ListBox component :

public class ModifierEcheanceForm extends Form implements ActionListener, DataChangedListener {

    private VirtualKeyboard vkNombre = new VirtualKeyboard();
    private String textFieldStatus, listBoxStatus;
    private Container cntnr = new Container();
    private Container c = new Container();
    private Container c1 = new Container();
    private Container c2 = new Container();
    private Container c3 = new Container();
    private Container c4 = new Container();
    private Container c5 = new Container();
    private Container c6 = new Container();
    private Container c7 = new Container();
    private Container c8 = new Container();
    private Container c9 = new Container();
    private Container c10 = new Container(new FlowLayout(Component.CENTER));
    private Container cz1 = new Container();
    private Container cz2 = new Container();
    private Container cz3 = new Container();
    private Container cz4 = new Container();
    private Container cz5 = new Container();
    private TextField t1=new TextField("zzz"),t2=new TextField("zzz"),t3=new TextField("zzz"),t4=new TextField("zzz"),t5=new TextField("zzz");
    private Button modifierBtn, reset;
    private BoxLayout bxl = new BoxLayout(BoxLayout.Y_AXIS);
    private BoxLayout bxltx = new BoxLayout(BoxLayout.X_AXIS);
    private boolean isMenuShown;
    private Command annulerCmd;
    private Command listeMenu, listeClientCmd, listeCreditCmd, ppalCmd;
    private Command ficheMenu, backCmd;
    private SmartPhoneBanking controler;
    private Form backForm, OldbackForm;
    private EcheanceDB echeancedb;
    private Vector listecheance = new Vector();
    private int idEcheance;
    private Label echedatelbl, montantlbl, eche_payelbl, eche_retardlbl, ecehe_nbretardlbl, eche_crdlbl, eche_rembourselbl, eche_interetlbl,flagPayelbl;
    private TextField echedatetxt, montanttxt, eche_payetxt, eche_retardtxt, ecehe_nbretardtxt, eche_crdtxt, eche_remboursetxt, eche_interettxt;
    private ListBox flagPayetxt;   // here is the ListBox
    private CreditDB creditdb = new CreditDB();
    private Vector listeCredit = new Vector();
    private String idClient, idCredit;
    public static int listEcheanceSelectedRow;

    public ModifierEcheanceForm(SmartPhoneBanking ctrl, int idEcheance, Form prevForm)
    {
        super("Modification échéance");
        vkNombre.setInputModeOrder(new String[]{VirtualKeyboard.NUMBERS_SYMBOLS_MODE});
        this.controler = ctrl;
        this.idEcheance = idEcheance;
        backForm = prevForm;
        OldbackForm = FicheCreditForm.backForm;

        echeancedb = new EcheanceDB();
        listecheance = echeancedb.echeanceParId(String.valueOf(idEcheance));

        idCredit = String.valueOf(listecheance.elementAt(5));
        listeCredit = creditdb.listCredit(Integer.parseInt(idCredit));
        idClient = String.valueOf(listeCredit.elementAt(12));

        c.setLayout(bxl);
        c1.setLayout(bxltx);
        c2.setLayout(bxltx);
        c3.setLayout(bxltx);
        c4.setLayout(bxltx);
        c5.setLayout(bxltx);
        c6.setLayout(bxltx);
        c7.setLayout(bxltx);
        c8.setLayout(bxltx);
        c9.setLayout(bxltx);

        echedatelbl = new Label("Date d'échéance");
        echedatelbl.setUIID("FicheLibelle");
        echedatetxt = new TextField();
        echedatetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(echedatetxt, vkNombre);

        montantlbl = new Label("Montant(Ar)");
        montantlbl.setUIID("FicheLibelle");
        montanttxt = new TextField();
        montanttxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(montanttxt, vkNombre);

        eche_payelbl = new Label("Payé (Ar)");
        eche_payelbl.setUIID("FicheLibelle");
        eche_payetxt = new TextField();
        eche_payetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_payetxt, vkNombre);

        eche_retardlbl = new Label("Retard");
        eche_retardlbl.setUIID("FicheLibelle");
        eche_retardtxt = new TextField();
        eche_retardtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_retardtxt, vkNombre);

        ecehe_nbretardlbl = new Label("Nombre de retard");
        ecehe_nbretardlbl.setUIID("FicheLibelle");
        ecehe_nbretardtxt = new TextField();
        ecehe_nbretardtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(ecehe_nbretardtxt, vkNombre);

        eche_crdlbl = new Label("Crédit (Ar)");
        eche_crdlbl.setUIID("FicheLibelle");
        eche_crdtxt = new TextField();
        eche_crdtxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_crdtxt, vkNombre);

        eche_rembourselbl = new Label("Remboursé (Ar)");
        eche_rembourselbl.setUIID("FicheLibelle");
        eche_remboursetxt = new TextField();
        eche_remboursetxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_remboursetxt, vkNombre);

        eche_interetlbl = new Label("Intérêt (Ar)");
        eche_interetlbl.setUIID("FicheLibelle");
        eche_interettxt = new TextField();
        eche_interettxt.addDataChangeListener(this);
        VirtualKeyboard.bindVirtualKeyboard(eche_interettxt, vkNombre);

        flagPayelbl = new Label("Payé");
        flagPayelbl.setUIID("FicheLibelle");
        flagPayetxt = new ListBox(new String[]{"oui","non","aaaaaa","bbbb"},echedatetxt.getPreferredH(),getPreferredW(),getPreferredH(),eche_interettxt.getSelectedStyle().getMargin(Component.TOP),eche_interettxt.getSelectedStyle().getMargin(Component.BOTTOM));

        c10.getStyle().setPadding(Component.LEFT, 0);
        c10.getStyle().setPadding(Component.RIGHT, 0);

        reset = new Button("Rétablir les données");
        reset.addActionListener(this);
        reset.getUnselectedStyle().setMargin(0, 0, 0, 0);
        reset.getSelectedStyle().setMargin(0, 0, 0, 0);
        reset.setAlignment(Label.CENTER);
        reset.setUIID("btnDetailCreditFieldset");
        c10.addComponent(reset);

        modifierBtn = new Button("Valider");
        modifierBtn.addActionListener(this);
        modifierBtn.getUnselectedStyle().setMargin(0, 0, 0, 0);
        modifierBtn.getSelectedStyle().setMargin(0, 0, 0, 0);
        modifierBtn.setAlignment(Label.CENTER);
        modifierBtn.setUIID("btnDetailCreditFieldset");

        c1.addComponent(echedatelbl);
        c1.addComponent(echedatetxt);

        c2.addComponent(montantlbl);
        c2.addComponent(montanttxt);

        c3.addComponent(eche_payelbl);
        c3.addComponent(eche_payetxt);

        c4.addComponent(eche_retardlbl);
        c4.addComponent(eche_retardtxt);

        c5.addComponent(ecehe_nbretardlbl);
        c5.addComponent(ecehe_nbretardtxt);

        c6.addComponent(eche_crdlbl);
        c6.addComponent(eche_crdtxt);

        c7.addComponent(eche_rembourselbl);
        c7.addComponent(eche_remboursetxt);

        c8.addComponent(eche_interetlbl);
        c8.addComponent(eche_interettxt);

        c9.addComponent(flagPayelbl);
        c9.addComponent(flagPayetxt);

        cz1.addComponent(t1);
        cz2.addComponent(t2);
        cz3.addComponent(t3);
        cz4.addComponent(t4);
        cz5.addComponent(t5);

        int[] labelW = new int[]{echedatelbl.getPreferredW(), montantlbl.getPreferredW(), eche_payelbl.getPreferredW(), eche_retardlbl.getPreferredW(), ecehe_nbretardlbl.getPreferredW(), eche_crdlbl.getPreferredW(), eche_rembourselbl.getPreferredW(), eche_interetlbl.getPreferredW(),flagPayelbl.getPreferredW()};
        int largeW = Comparator.max(labelW);
        echedatelbl.setPreferredW(largeW);
        montantlbl.setPreferredW(largeW);
        eche_payelbl.setPreferredW(largeW);
        eche_retardlbl.setPreferredW(largeW);
        ecehe_nbretardlbl.setPreferredW(largeW);
        eche_crdlbl.setPreferredW(largeW);
        eche_rembourselbl.setPreferredW(largeW);
        eche_interetlbl.setPreferredW(largeW);
        flagPayelbl.setPreferredW(largeW+2);

        c10.addComponent(modifierBtn);

        c.addComponent(c1);
        c.addComponent(c2);
        c.addComponent(c3);
        c.addComponent(c4);
        c.addComponent(c5);
        c.addComponent(c6);
        c.addComponent(c7);
        c.addComponent(c8);
        c.addComponent(cz1);
        c.addComponent(cz2);
        c.addComponent(cz3);
        c.addComponent(cz4);
        c.addComponent(cz5);
        c.addComponent(c9);
        c.addComponent(c10);

        cntnr.addComponent(c);
        this.setScrollableY(true);
        this.addComponent(cntnr);

        echedatetxt.setText(listecheance.elementAt(0).toString());
        montanttxt.setText(listecheance.elementAt(1).toString());
        eche_payetxt.setText(listecheance.elementAt(6).toString());
        eche_retardtxt.setText(listecheance.elementAt(7).toString());
        ecehe_nbretardtxt.setText(listecheance.elementAt(8).toString());
        eche_crdtxt.setText(listecheance.elementAt(9).toString());
        eche_remboursetxt.setText(listecheance.elementAt(10).toString());
        eche_interettxt.setText(listecheance.elementAt(11).toString());
        textFieldStatus = "NORMAL";
        if (String.valueOf(listecheance.elementAt(2)).toLowerCase().startsWith("o"))
            flagPayetxt.setSelectedIndex(0);
        else if (String.valueOf(listecheance.elementAt(2)).toLowerCase().startsWith("n"))
            flagPayetxt.setSelectedIndex(1);

        listeMenu      = new Command("Liste");
        listeClientCmd = new Command("Clients");
        listeCreditCmd = new Command("Crédits");
        ppalCmd        = new Command("Menu principal");
        ficheMenu      = new Command("Fiche");
        backCmd        = new Command("Retour");
        annulerCmd     = new Command("Annuler");
        isMenuShown    = false;
        this.addCommand(listeMenu);
        this.addCommand(ficheMenu);
        this.addCommandListener(this);
    } // end of constructor
    ...
}

You see that I have considered the two cases if there is space or not below the ListBox in the pointerPressed method of the ListBox class.

When I don't write in the calling Form's code the addComponent for the containers cz1 through cz5 then the Dialog is shown very well below the ListBox component.

So why doesn't the Dialog show when there is no more space at the bottom ?

Upvotes: 1

Views: 128

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

You invoke the dialog show method with sizes that don't leave any space for the dialog. You need to check that height - top - bottom > dialogPreferreHeight and the same should apply for width.

See the combo box popup code where we conditionally popup the dialog upwards if there is no room bellow.

Upvotes: 1

Related Questions