Re: Help with Swing
GG Sriram wrote:
What follows is not an SSCCE, you are making it hard for people like me
to help you. I can't copy upoir code into my PC and run it to see what
it does. If you need help, try to make it easy for people to help you.
public class T1Panel implements IEEventListener{
What is IEEventListener?
private static final String AddActions="RC_ADDFIRST";
private IEButton AddFirstButton;
What is IEButton?
Why has your variable AddFirstButton got a name that starts with a
capital letter? Sun's Java conventions state that Class names should
start with an upper-case letter but variable names should start with a
lower case letter. This makes your variable names look like class names
to me. This makes it harder for me to understand your code.
private IEButton rowControlButton;
private static final String[] ROW_CONTROL_ACTIONS =
{"RC_UP","RC_DOWN","RC_DELETE"};
OK, But I wonder why you are not using enums.
public void go() {
Some Javadoc style comments might help me understand what invokes go,
when and why.
AddFirstButton=new IEButton("");
AddFirstButton.setBounds(setHeaderRowX_Pixels
+45,row*IEFrame.ROW_HEIGHT+15,40,IEFrame.ROW_HEIGHT+4);
Where did setHeaderRowX_Pixels come from?
The "set" prefix makes it look like a setter method name. I usually use
nouns or noun phrases for variables and verbs or verb phrases for methods.
Why use setBounds on buttons? I always let the Look&Feel and Layout
Manager do that for me.
AddFirstButton.setIcon(new javax.swing.ImageIcon("images/
T1DriverAdd1st.jpg"));
AddFirstButton.setToolTipText("Add 1st row");
AddFirstButton.setActionCommand(AddActions);
panelStats.checkMaxX_bounds(AddFirstButton.getBounds());
AddFirstButton.addActionListener(this.t1Frame);
What is t1Frame?
where is t1Frame's ActionListener?
How does this relate to your buttonClickV2() method?
If this paraphernalia is not relevant to your problem, I would say it is
better to strip itt out of an SSCCE.
aContainer.add(AddFirstButton);
Where did aContainer come from?
int row=0;
row++;
int row=1;
// Not sure If I am doing right here
Not sure I can tell. There's so much missing.
buttonClickV2("AddActions","");
if ("RC_ADDFIRST" .equals(AddFirstButton.getName()))
{
for(int b=0;b<3;b++)
{
rowControlButton = new IEButton("");
rowControlButton.setActionCommand(ROW_CONTROL_ACTIONS
[b]);
Really you should iterate over ROW_CONTROL_ACTIONS. I feel this code
would be a lot simpler using enums. Simpler code is simpler to debug.
rowControlButton.setVisible(false);
if(b == 0) {
Madness, why loop over three items if you are going to unroll the loop
inside itself?
rowControlButton.setIcon(new javax.swing.ImageIcon
("images/ T1DriverUp.jpg"));
Is there really a space in that filename?
rowControlButton.setToolTipText("Add 1st Row Above");
The image file name and tooltip could be part of an enum:
enum ButtonInfo {
UP ("Up", "images/T1DriverUp.jpg", "Add 1st Row Above")
DOWN (...)
DELETE (...)
private String label, image, tip;
ButtonInfo(String label, String image, String tip) {
this.label=label;
this.image = image;
this.tip=tip;
}
public String getLabel() { return label; }
public String getLabel() { return image; }
public String getToolTip() { return tip; }
}
....
for (ButtonInfo bi: ButtonInfo.values()) {
IEButton b = new IEButton(bi.getLabel());
b.setIcon(new ImageIcon(bi.getIcon));
...
aContainer.add(b);
}
Warning: all the above is untested and probably has errors but you
should grasp the idea from it.
}
else if(b == 1) {
rowControlButton.setIcon(new javax.swing.ImageIcon
("images/T1DriverDown.jpg"));
rowControlButton.setToolTipText("Add One Row below");
}
else if(b == 2) {
rowControlButton.setIcon(new javax.swing.ImageIcon
("images/ T1DriverX.jpg"));
rowControlButton.setToolTipText("Delete row");
}
Whenever I see repetition like this in my code, I know I should stop and
rethink then refactor.
rowControlButton.setBounds(setHeaderRowX_Pixels + (45*(b
+2)),row * IEFrame.ROW_HEIGHT,40,IEFrame.ROW_HEIGHT);
panelStats.checkMaxX_bounds(rowControlButton.getBounds
());
rowControlButton.addActionListener(this.t1Frame);
aContainer.add(rowControlButton);
}
}
}
// Button Clicks - events
public void buttonClickV2(java.lang.String buttonLabel,String msg)
{
if(buttonLabel.equals(AddActions)){
System.err.println("Add 1st row to fieldset");
AddFirstButton.setVisible(false); // disables the button
The comment is wrong.
Comments that repeat the method name are useless.
Comments that contradict the method name are confusing.
button.setEnabled(false) disables the button!
button.setVisible(false) makes the button invisible!
this.t1Frame.repaint();
Whether this is necessary depends on the layout manager.
//Incomplete
}
Incomplete - -
Yes, that makes it hard to help. It demotivates helpers.
I tried the setenabled property for rowControlButton
but it returns the last button the delete icon button.
I've no idea what that sentence means. JButton.setEnabled() doesn't
return any value. What is IEButton?
Can anyone help
me here to set the set of rows of rowControlbuttons so as to enable
all of them to turn on when the Add1stbutton is pressed(actionevent is
generated)
Try constructing a new example (a *proper* SSCCE) from scratch using
just JButtons, JFrames etc. If you can make that work then you can most
likely make it work with IEButtons etc.
--
RGB