Why the Dialog is not shown in this situation? - java-me

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 :
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 ?

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.

Related

How to position text over resizable imageview?

It has a stackpane and few childs inside it:
public class SlideFromText {
private StackPane sp = new StackPane();
private ImageView iv = new ImageView();
private int lines_count = 0;
private Group text_group = new Group();
public SlideFromText(Image img, String t) {
iv.setImage(img);
iv.setPreserveRatio(true);
iv.fitWidthProperty().bind(sp.widthProperty());
iv.fitHeightProperty().bind(sp.heightProperty());
sp.setStyle("-fx-border-color : red; -fx-border-width: 1; -fx-border-style: solid;");
sp.setAlignment(Pos.CENTER);
sp.setMinSize(0, 0);
sp.getChildren().add(iv);
String lines[] = t.split("\\r?\\n");
double height = 0;
for (String line : lines) {
Text text = new Text(line);
text.setFill(Color.BLACK);
text.setY(height);
text_group.getChildren().add(text);
lines_count++;
height += text.getBoundsInLocal().getHeight();
System.out.println("Height: " + height);
}
sp.getChildren().add(text_group);
iv.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
#Override
public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
Node node = sp.getChildren().get(1);
if (node.getClass() != Group.class) {
return;
}
Group group_node = (Group) node;
double scale_x = iv.getBoundsInParent().getWidth() / group_node.getBoundsInParent().getWidth();
double scale_y = iv.getBoundsInParent().getHeight() / group_node.getBoundsInParent().getHeight();
double scale = Math.min(scale_x, scale_y);
group_node.setScaleX(group_node.getScaleX() * scale);
group_node.setScaleY(group_node.getScaleY() * scale);
System.out.println("IVB: " + iv.getBoundsInParent());
System.out.println("GVB: " + group_node.getBoundsInParent());
}
});
}
public Pane getPane() {
return sp;
}
}
I need to have multi-line text inside group. I want to resize my window and keep image aspect ratio and text proportions and position inside image.
In my example I can't set my text on the top of image.
Maybe it has more easy way.

MvvmCross - MvxTabBarViewController does not show the "More" button when using a Storyboard

In my application, I'm using the MvxTabBarViewController to display some tabs (the MvxTabBarViewController is loaded using a Storyboard):
public partial class RootView : MvxTabBarViewController
{
private int _tabIndex = 0;
private Dictionary<int, SectionBase> _sectionBaseForConfig;
public new RootViewModel ViewModel
{
get { return (RootViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
public RootView(IntPtr handle)
: base(handle)
{
}
UIViewController tab1, tab2, tab3, tab4, tab5, tab6, tab7;
public override async void ViewDidLoad()
{
base.ViewDidLoad();
if (ViewModel == null)
return;
tab1 = new UIViewController();
tab1.Title = "1";
tab1.View.BackgroundColor = UIColor.Green;
tab2 = new UIViewController();
tab2.Title = "2";
tab2.View.BackgroundColor = UIColor.Orange;
tab3 = new UIViewController();
tab3.Title = "3";
tab3.View.BackgroundColor = UIColor.Red;
tab4 = new UIViewController();
tab4.Title = "4";
tab4.View.BackgroundColor = UIColor.Blue;
tab5 = new UIViewController();
tab5.Title = "5";
tab5.View.BackgroundColor = UIColor.Brown;
tab6 = new UIViewController();
tab6.Title = "6";
tab6.View.BackgroundColor = UIColor.Brown;
tab7 = new UIViewController();
tab7.Title = "7";
tab7.View.BackgroundColor = UIColor.Brown;
var tabs = new UIViewController[] {
tab1, tab2, tab3, tab4, tab5, tab6, tab7
};
ViewControllers = tabs;
CustomizableViewControllers = null;
SelectedViewController = ViewControllers[0];
}
}
Unfortunately, even if there are 7 tabs, only 5 are displayed and the "More" button does not show. Anyone got an idea ? I stuck on this problem since 2 hrs now, without any clues :(
Thanks!

MvvmCross: Cannot add more than one UIPickerView

I'm building an iOS view using Xamarin and MvvmCross and I have come across an interesting little issue. I can't seem to add more than one UIPickerView to a UIView.
Add one view and all works well. Add a second and the simulator just hangs when I try and open the page.
This seems to be related to a UITextField with an InputView as I also have an issue if I try to add a UIDatePicker as well.
Nothing strange in the debug output.
Here is the code:
[Register("EditJobViewJobView")]
public class EditJobView : MvxViewController
{
public new EditJobViewModel ViewModel
{
get { return (EditJobViewModel)base.ViewModel; }
}
private const float _leftMargin = 6;
private const float _labelHeight = 20;
private const float _pickerHeight = 28;
private readonly UIFont _labelFont = UIFont.BoldSystemFontOfSize(18f);
private readonly UIFont _controlFont = UIFont.SystemFontOfSize(18f);
private readonly UIView _paddingInsert = new UIView(new RectangleF(0, 0, 4, 0));
private int _currentTop = 0;
public override void ViewDidLoad()
{
base.ViewDidLoad();
View = new UIView() { BackgroundColor = UIColor.White };
NavigationItem.SetRightBarButtonItem(new UIBarButtonItem("Save", UIBarButtonItemStyle.Bordered,
(sender, args) => ViewModel.OkCommand.Execute(null)), true);
// ios7 layout
if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
EdgesForExtendedLayout = UIRectEdge.None;
Title = "Edit Job";
AddLabel("Job Status");
MvxPickerViewModel jobStatusPickerViewModel;
var jobStatusTextView = AddPickerView(out jobStatusPickerViewModel);
AddLabel("Job Priority");
MvxPickerViewModel jobPriorityPickerViewModel;
var jobPriorityTextView = AddPickerView(out jobPriorityPickerViewModel);
var set = this.CreateBindingSet<EditJobView, EditJobViewModel>();
set.Bind(jobStatusPickerViewModel).For(p => p.SelectedItem).To(vm => vm.SelectedJobStatusType);
set.Bind(jobStatusPickerViewModel).For(p => p.ItemsSource).To(vm => vm.JobStatusTypes);
set.Bind(jobStatusTextView).To(vm => vm.SelectedJobStatusType);
set.Bind(jobPriorityPickerViewModel).For(p => p.SelectedItem).To(vm => vm.SelectedJobPriority);
set.Bind(jobPriorityPickerViewModel).For(p => p.ItemsSource).To(vm => vm.JobPriorities);
set.Bind(jobPriorityTextView).To(vm => vm.SelectedJobPriority);
set.Apply();
}
private void AddLabel(string caption)
{
_currentTop += 10;
var frame = new RectangleF(_leftMargin, _currentTop, 300, _labelHeight);
var label = new UILabel(frame);
label.Font = _labelFont;
label.Text = caption;
AddView(label);
_currentTop += 2;
}
private UITextField AddPickerView(out MvxPickerViewModel pickerViewModel)
{
var textField = AddTextField();
var pickerView = new UIPickerView();
pickerViewModel = new MvxPickerViewModel(pickerView);
pickerView.Model = pickerViewModel;
pickerView.ShowSelectionIndicator = true;
textField.InputView = pickerView;
return textField;
}
private UITextField AddTextField()
{
var frame = new RectangleF(_leftMargin, _currentTop, 300, _pickerHeight);
var textField = new UITextField(frame);
textField.Layer.BorderColor = UIColor.Black.CGColor;
textField.Layer.BorderWidth = 1f;
textField.Font = _controlFont;
textField.LeftView = _paddingInsert;
textField.LeftViewMode = UITextFieldViewMode.Always;
AddView(textField);
return textField;
}
private void AddView(UIView view)
{
View.AddSubview(view);
_currentTop += (int)view.Frame.Height;
}
}
Any ideas?
This was caused by creating a shared padding insert across a number of UITextFields.
To fix this I changed
textField.LeftView = _paddingInsert;
to
textField.LeftView = new UIView(new RectangleF(0, 0, 4, 0));

Add New Row in GridView winform on Button Click

Add New Row in GridView winform on Button Click.
Need to have single button in grid rather than DataGridViewButtonColumn
you can add it by using
GridView1.DataSource = datatable;
and then add new row by this
datatable.Rows.Add();
this will add new row and give control id into add().
use this
public partial class Form1 : Form
{
Button textBoxDgv1 = new Button();
Label labelDgv1 = new Label();
the next is on the Form_Load event
private void Form1_Load(object sender, EventArgs e)
{
labelDgv1.Text = "Delete";
labelDgv1.Height = 20;
labelDgv1.AutoSize = false;
labelDgv1.BorderStyle = BorderStyle.FixedSingle;
labelDgv1.TextAlign = ContentAlignment.MiddleCenter;
int Xdgv1 = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgv1;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
this.dataGridView1.Controls.Add(labelDgv1);
and one more section is in dataGridView1_CellPainting
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[3].Value != string.Empty)
{
sum += Convert.ToInt32(this.dataGridView1[3, i].Value);
}
}
textBoxDgv1.Text = sum.ToString();
int Xdgvx = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgvx;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
textBoxDgv1.Width = this.dataGridView1.Columns[3].Width;
Xdgvx = this.dataGridView1.GetCellDisplayRectangle(3, -1, true).Location.X;
textBoxDgv1.Location = new Point(Xdgvx, this.dataGridView1.Height - textBoxDgv1.Height);
}

Why does this Dialog immediately dispose in this situation?

There is a user defined component , derived from Container, inside my Form. It has a pointerPressed method implemented in its class code. In the code of that method I show a Dialog containing a List , and in the class code of the derived Dialog ( in the constructor ) I set the setDisposeWhenPointerOutOfBounds method with the argument value to true.
The problem is that in run time when I click the user defined component ( ListBox ) in my Form then the Dialog is shown , of course , but immediately it closes ( disposes ) , although I don't click outside the boundary of the Dialog !
So why does it have such a behavior ?
Codes :
public class ListBox extends Container
{
private Form containerForm;
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 int listW;
private List list;
private DialogListBox dialog;
private String selectedData;
public ListBox(Form containingForm, String[] lData, int prefHeight, int formWidth, int formHeight, int topMargin, int bottomMargin)
{
super(new FlowLayout(Component.CENTER));
setFocusable(true);
containerForm = containingForm;
screenWidth = formWidth;
screenHeight = formHeight;
tLabel = new Label[lData.length + 1];
tLabelW = new int[lData.length + 1];
if (lData.length > 0)
{
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;
}
}
}
else
{
vData.addElement(new String(""));
tLabel[0] = new Label("");
tLabelW[0] = 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);
dialog = new DialogListBox(leftPadding, this);
list = dialog.getList();
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 setSelectedIndex(int idx)
{
list.setSelectedIndex(idx);
selectedData = String.valueOf(vData.elementAt(idx));
libelle.setText(String.valueOf(vData.elementAt(idx)));
repaint();
}
public void setSelectedData(String data)
{
selectedData = data;
libelle.setText(selectedData);
repaint();
}
public String getSelectedData()
{
return selectedData;
}
public int getLastIndex()
{
return vData.indexOf(vData.lastElement());
}
public Vector getListBoxDataSource()
{
return vData;
}
private void showListBoxDialog()
{
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() - 1 ;
}
left = getAbsoluteX() ;
right = screenWidth - ( getAbsoluteX() + getPreferredW() );
listW = screenWidth - left - right ;
containerForm.setTintColor(containerForm.getSelectedStyle().getBgColor());
dialog.setListW(listW);
dialog.show(top, bottom, left, right, false, false);
}
public void keyPressed(int keyCode)
{
int gameAction = (Display.getInstance()).getGameAction(keyCode);
if (gameAction == Display.GAME_FIRE)
showListBoxDialog();
else
super.keyPressed(keyCode);
}
public void pointerPressed(int x, int y)
{
showListBoxDialog();
}
}
public class DialogListBox extends Dialog implements ActionListener
{
private Vector vData;
private CListCellListBox listRenderer;
private List list;
private ListBox theListBox;
public DialogListBox(int leftPadding, ListBox listBox)
{
super();
setFocusable(true);
setDisposeWhenPointerOutOfBounds(true);
getContentPane().getSelectedStyle().setPadding(0, 0, 0, 0);
getContentPane().getUnselectedStyle().setPadding(0, 0, 0, 0);
getContentPane().getStyle().setPadding(0, 0, 0, 0);
theListBox = listBox;
listRenderer = new CListCellListBox(false);
vData = listBox.getListBoxDataSource();
list = (new CList(vData, false)).createList(listRenderer, this);
list.setItemGap(0);
list.setSelectedIndex(vData.indexOf(vData.lastElement()));
list.getSelectedStyle().setPadding(0, 0, leftPadding, 0);
list.getUnselectedStyle().setPadding(0, 0, leftPadding, 0);
list.getUnselectedStyle().setBorder(Border.createLineBorder(1), false);
list.getSelectedStyle().setBorder(Border.createLineBorder(1), false);
list.setIsScrollVisible(false);
addComponent(list);
}
protected void onShow()
{
list.requestFocus();
repaint();
}
public void setListW(int prefW)
{
list.setPreferredW(prefW);
}
public List getList()
{
return list;
}
private void refreshListBox()
{
dispose();
if (list.getSelectedItem() instanceof Content)
{
Content valeur = (Content)list.getSelectedItem();
theListBox.setSelectedData(valeur.getEnreg());
}
}
public void keyPressed(int keyCode)
{
int gameAction = (Display.getInstance()).getGameAction(keyCode);
if (gameAction == Display.GAME_FIRE)
refreshListBox();
else
super.keyPressed(keyCode);
}
public void actionPerformed(ActionEvent ae) {
if ( (ae.getSource() instanceof List) && ((List)ae.getSource()).equals(list) )
refreshListBox();
}
}
public class CList {
private Vector data = new Vector();
private boolean showPhoto;
private Content[] contents;
public CList(Vector vData, boolean displayPhoto)
{
data = vData;
showPhoto = displayPhoto;
contents = new Content[vData.size()];
}
public List createList(CListCell renderer, ActionListener listener)
{
List theList;
if (showPhoto)
{
for(int i = 0; i < data.size(); i++)
{
Image img = getFirstImage(Formatage.getColumnValueAt(String.valueOf(data.elementAt(i)), 0));
contents[i] = new Content(img, String.valueOf(data.elementAt(i)));
}
}
else
{
for(int i = 0; i < data.size(); i++)
{
contents[i] = new Content(String.valueOf(data.elementAt(i)));
}
}
theList = new List(contents);
theList.setListCellRenderer(renderer);
theList.setFixedSelection(List.FIXED_NONE_CYCLIC);
theList.addActionListener(listener);
return theList;
}
// ... other methods
}
You should always use pointerReleased/keyReleased for navigation to different forms/dialogs.
Otherwise the pointer/key released will be sent to the next form/dialog and trigger an action there.
Pointer pressed is mostly used internally in LWUIT and for some special cases.

Resources