Click to See Complete Forum and Search --> : HELP! My choicecontrol moves by itself on a frame!!!


tamart00
July 29th, 1999, 03:01 AM
Hi!

Has anyone noticed a similar strange problem? The Choicecontrol component moves itself as the contents of it are changed! The whole story goes like this:

I have two Choicecontrol components on a frame. The first one (chTeam) is used for selecting a team and the other one is used for selecting an employee (chEmployee) from the selected team. Obviously, these components get their contents from a database, which in my case is Interbase 5.

As the selection from chTeam is made, a parameterized query is executed, the items of the chEmployee-Choicecontrol are removed and new items are set to that component. Now, without any visible reason, the chEmployee-Choicecontrol moves on a frame about 10 pixels left and about
the same amount upwards!!! I have the source code below, if it helps. I am using JBuilder2 Client/Server version with InterBase5 database. I have tried with JBCL's and AWT's Choicecontrols with the same results.

All the help is highly appreciated!

regards,

Tapio Martimo


public void addTeamEmps()
{
chEmployee.removeAll();
try
{
dMtask1.qryEmployees.close();
dMtask1.paramTeamname.setString("teamname", (String)chTeam.getSelectedItem());
dMtask1.qryEmployees.refresh();
dMtask1.qryEmployees.open();
for (int i = 0; i < dMtask1.qryEmployees.getRowCount(); i++)
{
chEmployee.add((String)dMtask1.qryEmployees.getString("FULLNAME"));
dMtask1.qryEmployees.next();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}

DAVID DONG
July 29th, 1999, 06:55 AM
I wander if this would be help for you.
first, you can remove the database related statement.
Then, you can add some choice item string in your method directly.and run it, to see if it works.
Second, you can print all the result of you database command by use your code in your method. this can give the test if the database related command is right exec.

so far. hope helps.

This is from david dong.

tamart00
July 29th, 1999, 08:52 AM
Hi again!

Thanks for your suggestions. That lead me forward a little bit, but not totally.

I have been digging into this problem now for the whole day and I have discovered that in the Applet window, the items of the Choicecontrol-component can be changed without this weird jumping behavior. BUT, in the frame dialog, which is opened from the applet, the problem occurs consistently. It does not matter, if the item contents (Strings) are retrieved from a database or if they are given as class variables etc. I've included a small sample of this problem in this reply. If you are interested, copy and compile this and see it for yourself.

regards,

Tapio Martimo


here is the code for the applet. In this, the behavior of the components is normal...


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.sun.java.swing.*;
import borland.jbcl.layout.*;

//import com.sun.java.swing.UIManager;
public class applet1 extends JApplet {
boolean isStandalone = false;
Choice ch1 = new Choice();
Button button1 = new Button();
String[] first = {"first", "second", "third"};
String[] second = {"Changedfirst", "Changedsecond", "Changedthird", "and the last one"};
boolean current = true;
Button button2 = new Button();
Frame1 f1 = new Frame1();
GridBagLayout gridBagLayout1 = new GridBagLayout();
//Get a parameter value

public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

//Construct the applet

public applet1() {
}
//Initialize the applet

public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//static {
// try {
// //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.metal.MetalLookAndFeel());
// //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel());
// UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
// }
// catch (Exception e) {}
//}
//Component initialization

private void jbInit() throws Exception {
this.setSize(400,300);
this.getContentPane().setLayout(gridBagLayout1);
button1.setLabel("change values in the control");
button2.setLabel("show the frame");
button2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button2_mouseClicked(e);
}
});
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button1_mouseClicked(e);
}
});
this.getContentPane().add(ch1, new GridBagConstraints2(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(130, 81, 0, 8), 164, 0));
this.getContentPane().add(button1, new GridBagConstraints2(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(118, 109, 4, 5), 0, 0));
this.getContentPane().add(button2, new GridBagConstraints2(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(118, 3, 4, 0), 17, 0));
// for (int i = 0; i < first.length; i++)
// ch1.add(first[i]);
}
//Get Applet information

public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info

public String[][] getParameterInfo() {
return null;
}

void button1_mouseClicked(MouseEvent e) {
if (current)
{
ch1.removeAll();
current = false;
for (int i = 0; i < first.length; i++)
ch1.add(first[i]);
}
else
{
ch1.removeAll();
current = true;
for (int i = 0; i < second.length; i++)
ch1.add(second[i]);
}

}

void button2_mouseClicked(MouseEvent e) {
if (!f1.isShowing())
f1.show();

}
}




and here comes the frame: in this, the problem arises...


import java.awt.*;
import com.sun.java.swing.JFrame;
import borland.jbcl.layout.*;
import java.awt.event.*;

public class Frame1 extends JFrame {
Choice ch1 = new Choice();
String[] first = {"first", "second", "third"};
String[] second = {"Changedfirst", "Changedsecond", "Changedthird", "and the last one"};
boolean current = true;
Button button1 = new Button();
GridBagLayout gridBagLayout1 = new GridBagLayout();



public Frame1() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().setLayout(gridBagLayout1);
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button1_mouseClicked(e);
}
});
button1.setLabel("change the values");
this.getContentPane().add(ch1, new GridBagConstraints2(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(82, 110, 140, 0), 127, 0));
this.getContentPane().add(button1, new GridBagConstraints2(0, 0, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 0, 0, 0), 278, 0));
this.resize(450,300);
}

void button1_mouseClicked(MouseEvent e) {
if (current)
{
ch1.removeAll();
current = false;
for (int i = 0; i < first.length; i++)
ch1.add(first[i]);
}
else
{
ch1.removeAll();
current = true;
for (int i = 0; i < second.length; i++)
ch1.add(second[i]);
}
}
}

DAVID DONG
August 5th, 1999, 11:23 AM
Had seen your code, but I don't install the Swing support on my IE5.0
And I did not got the borland,jbcl.layout Package.
So I write a simple code too, but it works. Then I thind the problem
you encounter may be one of the follow reasons :
1.you use the JApplet and JFrame
2.you use the borland.jbcl.layout.*;
3.the IE version difference (or may be you use other kind Broswer).
////the follow is the my applet code

import java.applet.*;
import java.awt.*;
public class Applet3 extends Applet implements java.awt.event.ActionListener {
private Button ivjButton1 = null;
private Button ivjButton2 = null;
private Choice ivjChoice1 = null;
private TextField ivjTextField1 = null;
private Button ivjButton3 = null;
private Frame3 ivjf3 = null;
public void actionPerformed(java.awt.event.ActionEvent e) {
if ((e.getSource() == getButton1()) ) {
connEtoM1(e);
}
if ((e.getSource() == getButton2()) ) {
connEtoM2(e);
}
if ((e.getSource() == getButton3()) ) {
connEtoM3(e);
}
}
private void connEtoM1(java.awt.event.ActionEvent arg1) {
try {
getChoice1().add(getTextField1().getText());
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoM2(java.awt.event.ActionEvent arg1) {
try {
getChoice1().removeAll();
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoM3(java.awt.event.ActionEvent arg1) {
try {
getf3().show();
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
public String getAppletInfo() {
return "Applet3 Java.";
}
private Button getButton1() {
if (ivjButton1 == null) {
try {
ivjButton1 = new java.awt.Button();
ivjButton1.setName("Button1");
ivjButton1.setBounds(309, 44, 56, 23);
ivjButton1.setLabel("add");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjButton1;
}
private Button getButton2() {
if (ivjButton2 == null) {
try {
ivjButton2 = new java.awt.Button();
ivjButton2.setName("Button2");
ivjButton2.setBounds(312, 122, 56, 23);
ivjButton2.setLabel("reset");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjButton2;
}
private Button getButton3() {
if (ivjButton3 == null) {
try {
ivjButton3 = new java.awt.Button();
ivjButton3.setName("Button3");
ivjButton3.setBounds(313, 188, 56, 23);
ivjButton3.setLabel("frame");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjButton3;
}
private Choice getChoice1() {
if (ivjChoice1 == null) {
try {
ivjChoice1 = new java.awt.Choice();
ivjChoice1.setName("Choice1");
ivjChoice1.setBounds(51, 87, 182, 95);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjChoice1;
}
private Frame3 getf3() {
if (ivjf3 == null) {
try {
ivjf3 = new Frame3();
ivjf3.setName("f3");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjf3;
}
private TextField getTextField1() {
if (ivjTextField1 == null) {
try {
ivjTextField1 = new java.awt.TextField();
ivjTextField1.setName("TextField1");
ivjTextField1.setBounds(42, 28, 209, 23);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjTextField1;
}
private void handleException(Throwable exception) {

}
public void init() {
super.init();
try {
setName("Applet3");
setLayout(null);
setSize(426, 240);
add(getButton1(), getButton1().getName());
add(getChoice1(), getChoice1().getName());
add(getTextField1(), getTextField1().getName());
add(getButton2(), getButton2().getName());
add(getButton3(), getButton3().getName());
initConnections();
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void initConnections() {
getButton1().addActionListener(this);
getButton2().addActionListener(this);
getButton3().addActionListener(this);
}
}



/////The follow is the my frame code:

class Frame3 extends java.awt.Frame implements java.awt.event.ActionListener, java.awt.event.WindowListener {
private java.awt.Button ivjButton1 = null;
private java.awt.Button ivjButton2 = null;
private java.awt.Choice ivjChoice1 = null;
private java.awt.Panel ivjContentsPane = null;
private java.awt.TextField ivjTextField1 = null;
public Frame3() {
super();
initialize();
}
public Frame3(String title) {
super(title);
}
public void actionPerformed(java.awt.event.ActionEvent e) {
if ((e.getSource() == getButton1()) ) {
connEtoM1(e);
}
if ((e.getSource() == getButton2()) ) {
connEtoM2(e);
}
}
private void connEtoC1(java.awt.event.WindowEvent arg1) {
try {
this.dispose();
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoM1(java.awt.event.ActionEvent arg1) {
try {
getChoice1().add(getTextField1().getText());
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private void connEtoM2(java.awt.event.ActionEvent arg1) {
try {
getChoice1().removeAll();
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private java.awt.Button getButton1() {
if (ivjButton1 == null) {
try {
ivjButton1 = new java.awt.Button();
ivjButton1.setName("Button1");
ivjButton1.setBounds(320, 54, 56, 23);
ivjButton1.setLabel("add");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjButton1;
}
private java.awt.Button getButton2() {
if (ivjButton2 == null) {
try {
ivjButton2 = new java.awt.Button();
ivjButton2.setName("Button2");
ivjButton2.setBounds(321, 130, 56, 23);
ivjButton2.setLabel("reset");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjButton2;
}
private java.awt.Choice getChoice1() {
if (ivjChoice1 == null) {
try {
ivjChoice1 = new java.awt.Choice();
ivjChoice1.setName("Choice1");
ivjChoice1.setBounds(58, 94, 156, 92);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjChoice1;
}
private java.awt.Panel getContentsPane() {
if (ivjContentsPane == null) {
try {
ivjContentsPane = new java.awt.Panel();
ivjContentsPane.setName("ContentsPane");
ivjContentsPane.setLayout(null);
getContentsPane().add(getButton1(), getButton1().getName());
getContentsPane().add(getButton2(), getButton2().getName());
getContentsPane().add(getTextField1(), getTextField1().getName());
getContentsPane().add(getChoice1(), getChoice1().getName());
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjContentsPane;
}
private java.awt.TextField getTextField1() {
if (ivjTextField1 == null) {
try {
ivjTextField1 = new java.awt.TextField();
ivjTextField1.setName("TextField1");
ivjTextField1.setBounds(55, 54, 159, 23);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
};
return ivjTextField1;
}
private void handleException(Throwable exception) {

}
private void initConnections() {
this.addWindowListener(this);
getButton1().addActionListener(this);
getButton2().addActionListener(this);
}
private void initialize() {
setName("Frame3");
setLayout(new java.awt.BorderLayout());
setSize(426, 240);
add(getContentsPane(), "Center");
initConnections();
}
public void windowActivated(java.awt.event.WindowEvent e) {
}
public void windowClosed(java.awt.event.WindowEvent e) {
}
public void windowClosing(java.awt.event.WindowEvent e) {
if ((e.getSource() == this) ) {
connEtoC1(e);
}
}
public void windowDeactivated(java.awt.event.WindowEvent e) {
}
public void windowDeiconified(java.awt.event.WindowEvent e) {
}
public void windowIconified(java.awt.event.WindowEvent e) {
}
public void windowOpened(java.awt.event.WindowEvent e) {
}
}




This is from david dong.