Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

panel resize problem  RSS feed

 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I try to resize panel, but it seems not works....
The code like this:
public class ShowChart extends javax.swing.JFrame {
.......
Container f = getContentPane();
JButton change = new JButton("change");
f.add(change,java.awt.BorderLayout.SOUTH);
JPanel chart = new drawtables();
JScrollPane sp = new JScrollPane(chart,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.add(sp,java.awt.BorderLayout.CENTER);
change.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
chart.changeWidthHeight();
chart.repaint();
}
});
.......
}
class drawtables extends JPanel{
int width, height;
public drawtables();
.....
public void changeWidthHeight(){
.......
//do something to change the width and height;
}
public void draw(){
.....
//draw something in the panel;
}
public void paint(Graphics g)
{
this.setPreferredSize(new Dimension(width,height));
draw(); //draw something inside the panel;
}
}
Problem: After click the "change" button, the drawing things in the panel do changed, but the size of the panel does not changed ....
Anybody can give me some suggestions??
thanks in advance
dejie
 
inam paki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U better Pass refernce of the JFrame in which u added JPanel and then resizing it will automatically increase ur JPanel size.
Actually Java's Layouts make some boundaries in preffered size of Components. They always select the best size which fits ur JFrame's size.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually only put minimal code in paint statement as this is executed frequently.
Maybe moving your "setPreferredSize(new Dimension(width,height))" code to changeWidthHeight() method may help.
Could also add "setMinimumSize(new Dimension(width,height));" to help the layout manager.
A call on "validate ()" may help show the new size.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!