suprapto45
asked on
JFrame resize problem
I am creating the function when the user resizes the JFrame, I would like the few components to auto-locate to fit the size.
Well, it works fine the first time the user resized the JFrame, it fit correctly. However, the second time they resized the JFrame, seems like all the components get back to the original width and height again (before my 1st resize) so my 2nd resize becomes inaccurate.
I used NetBeans and my codes look like below.
private void formComponentResized(java. awt.event. ComponentE vent evt) {
int newHeight = evt.getComponent().getHeig ht();
int newWidth = evt.getComponent().getWidt h();
int diffHeight = newHeight - originalHeight;
int diffWidth = newWidth - originalWidth;
System.out.println("jPanel 1.getHeigh t() = " + jPanel1.getHeight());
if (diffHeight >= 0) {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() + diffHeight);
}
else {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() - diffHeight);
}
if (diffWidth >= 0) {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth() + diffWidth, jPanel1.getHeight());
}
else {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth() - diffWidth, jPanel1.getHeight());
}
System.out.println("jPanel 1.getHeigh t() 2 = " + jPanel1.getHeight());
originalHeight = newHeight;
originalWidth = newWidth;
}
My output is
jPanel1.getHeight() = 550
jPanel1.getHeight() 2 = 750 (CORRECT UP TO HERE)
when I did my 2nd resize....
jPanel1.getHeight() = 550 (it is back to original height)
jPanel1.getHeight() 2 = 642
Any idea?
Well, it works fine the first time the user resized the JFrame, it fit correctly. However, the second time they resized the JFrame, seems like all the components get back to the original width and height again (before my 1st resize) so my 2nd resize becomes inaccurate.
I used NetBeans and my codes look like below.
private void formComponentResized(java.
int newHeight = evt.getComponent().getHeig
int newWidth = evt.getComponent().getWidt
int diffHeight = newHeight - originalHeight;
int diffWidth = newWidth - originalWidth;
System.out.println("jPanel
if (diffHeight >= 0) {
jPanel1.setBounds(jPanel1.
}
else {
jPanel1.setBounds(jPanel1.
}
if (diffWidth >= 0) {
jPanel1.setBounds(jPanel1.
}
else {
jPanel1.setBounds(jPanel1.
}
System.out.println("jPanel
originalHeight = newHeight;
originalWidth = newWidth;
}
My output is
jPanel1.getHeight() = 550
jPanel1.getHeight() 2 = 750 (CORRECT UP TO HERE)
when I did my 2nd resize....
jPanel1.getHeight() = 550 (it is back to original height)
jPanel1.getHeight() 2 = 642
Any idea?
>> I would like the few components to auto-locate to fit the size.
Isn't that what a layout manager (automatically) takes care of?
Isn't that what a layout manager (automatically) takes care of?
ASKER
Yep,
However, I think it uses either Null layout or AbsoluteLayout (from NetBeans). I think that this layout would not be able to control those auto-resize.
Am I right?
Thanks
David
However, I think it uses either Null layout or AbsoluteLayout (from NetBeans). I think that this layout would not be able to control those auto-resize.
Am I right?
Thanks
David
ASKER
I fixed the bugs.
if (diffHeight >= 0) {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() + diffHeight);
}
else {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() - diffHeight);
}
If I resize the form and the diffHeight becomes -100 (for example), then
else {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() - diffHeight);
}
will be inaccurate. It becomes jPanel1.getHeight() - (-100) and in the end, it is jPanel1.getHeight() + 100.
I solved the problem using
else {
jPanel1.setBounds(jPanel1. getX(), jPanel1.getY(), jPanel1.getWidth(), jPanel1.getHeight() - Math.abs(diffHeight));
}
if (diffHeight >= 0) {
jPanel1.setBounds(jPanel1.
}
else {
jPanel1.setBounds(jPanel1.
}
If I resize the form and the diffHeight becomes -100 (for example), then
else {
jPanel1.setBounds(jPanel1.
}
will be inaccurate. It becomes jPanel1.getHeight() - (-100) and in the end, it is jPanel1.getHeight() + 100.
I solved the problem using
else {
jPanel1.setBounds(jPanel1.
}
ASKER
Thanks zzynx for replying anyway.
That is my mistake.
David
That is my mistake.
David
>> Null layout or AbsoluteLayout.
>> I think that this layout would not be able to control those auto-resize
That's right
>> Thanks zzynx for replying anyway.
You're welcome
>> I solved the problem
>> That is my mistake.
So, no more questions left?
>> I think that this layout would not be able to control those auto-resize
That's right
>> Thanks zzynx for replying anyway.
You're welcome
>> I solved the problem
>> That is my mistake.
So, no more questions left?
ASKER
>>"So, no more questions left?"
Yes :). No more. I think that I am going to delete this question.
Any objection?
Thanks
David
Yes :). No more. I think that I am going to delete this question.
Any objection?
Thanks
David
OK for me.
ASKER
Hi zzynx,
Instead of deleting the question, I have another question. Well, out of curiosity, I tried to use BorderLayout with expectation that it will automatically help me to do it.
However, I put my JScrollPane with JTable inside it on the BorderLayout.CENTER and the button on the BorderLayout.SOUTH. However, upon resizing my frame, my JScrollPane do not grow automatically, instead it keeps its current size.
What can I do to make the JScrollPane to resize itself once I resize my JFrame?
Thanks
David
Instead of deleting the question, I have another question. Well, out of curiosity, I tried to use BorderLayout with expectation that it will automatically help me to do it.
However, I put my JScrollPane with JTable inside it on the BorderLayout.CENTER and the button on the BorderLayout.SOUTH. However, upon resizing my frame, my JScrollPane do not grow automatically, instead it keeps its current size.
What can I do to make the JScrollPane to resize itself once I resize my JFrame?
Thanks
David
ASKER
And please bare with me as SWING (J2SE) is not my expertise ;)
David
David
>> my JScrollPane do not grow automatically
It simply should!
You probably apply setPreferredSize() or something like that on your scrollpane/JTable.
You shouldn't do that if you give the layouting in hands of a layout manager.
So remove all kind of setSize(), setPreferredSize(), .... calls.
It simply should!
You probably apply setPreferredSize() or something like that on your scrollpane/JTable.
You shouldn't do that if you give the layouting in hands of a layout manager.
So remove all kind of setSize(), setPreferredSize(), .... calls.
ASKER
Ok,
Give me a sec....
Give me a sec....
ASKER
It still does not work. For example, when I tried to maximize the JFrame, the JScrollPane is not growing to fill up the empty space.
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tblAllBand = new javax.swing.JTable();
ts.setTableHeader(tblAllBa nd.getTabl eHeader()) ;
jPanel2 = new javax.swing.JPanel();
cmdBack = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
setDefaultCloseOperation(j avax.swing .WindowCon stants.EXI T_ON_CLOSE );
setTitle("View All Bands");
jPanel1.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.LEFT)) ;
tblAllBand.setModel(ts);
tblAllBand.setAutoResizeMo de(javax.s wing.JTabl e.AUTO_RES IZE_OFF);
jScrollPane1.setViewportVi ew(tblAllB and);
jPanel1.add(jScrollPane1);
getContentPane().add(jPane l1, java.awt.BorderLayout.CENT ER);
jPanel2.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.RIGHT) );
cmdBack.setFont(new java.awt.Font("Arial", 0, 12));
cmdBack.setText("Back");
cmdBack.addActionListener( new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
cmdBackActionPerformed(evt );
}
});
jPanel2.add(cmdBack);
btnEdit.setFont(new java.awt.Font("Arial", 0, 12));
btnEdit.setText("Edit");
btnEdit.addActionListener( new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
btnEditActionPerformed(evt );
}
});
jPanel2.add(btnEdit);
getContentPane().add(jPane l2, java.awt.BorderLayout.SOUT H);
pack();
}
I could not find any setPreferredSize() or setSize().
Let me keep looking....please let me know if you find anything suspicious.
Thanks.
David
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tblAllBand = new javax.swing.JTable();
ts.setTableHeader(tblAllBa
jPanel2 = new javax.swing.JPanel();
cmdBack = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
setDefaultCloseOperation(j
setTitle("View All Bands");
jPanel1.setLayout(new java.awt.FlowLayout(java.a
tblAllBand.setModel(ts);
tblAllBand.setAutoResizeMo
jScrollPane1.setViewportVi
jPanel1.add(jScrollPane1);
getContentPane().add(jPane
jPanel2.setLayout(new java.awt.FlowLayout(java.a
cmdBack.setFont(new java.awt.Font("Arial", 0, 12));
cmdBack.setText("Back");
cmdBack.addActionListener(
public void actionPerformed(java.awt.e
cmdBackActionPerformed(evt
}
});
jPanel2.add(cmdBack);
btnEdit.setFont(new java.awt.Font("Arial", 0, 12));
btnEdit.setText("Edit");
btnEdit.addActionListener(
public void actionPerformed(java.awt.e
btnEditActionPerformed(evt
}
});
jPanel2.add(btnEdit);
getContentPane().add(jPane
pack();
}
I could not find any setPreferredSize() or setSize().
Let me keep looking....please let me know if you find anything suspicious.
Thanks.
David
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So, your jPanel1 didn't have the BorderLayout but the FlowLayout.
In that case the behaviour you experienced was normal.
In that case the behaviour you experienced was normal.
ASKER
You know what zzynx,
That works like a charm. You deserve more points. I should learn this SWING more ;).
Thanks again. Really appreciate it.
David
That works like a charm. You deserve more points. I should learn this SWING more ;).
Thanks again. Really appreciate it.
David
>> That works like a charm.
:°)
>> You deserve more points.
Well, thanks
>> I should learn this SWING more ;)
Yeah. Once you know it good, it's not that hard anymore.
>> Thanks again. Really appreciate it.
You're welcome. Glad to help.
:°)
>> You deserve more points.
Well, thanks
>> I should learn this SWING more ;)
Yeah. Once you know it good, it's not that hard anymore.
>> Thanks again. Really appreciate it.
You're welcome. Glad to help.
ASKER