views:

73

answers:

1

I have a JFrame which contains a JPanel. The frame can be resized by the user using the mouse. When the width of the frame is > 400, the jpanel inside's width is set to 10; otherwise 1080.

In between the frame and the panel, there is also a JScrollBar.

Here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import javax.swing.table.*;
/**
 *
 * @author Administrator
 */
public class TableFrame extends javax.swing.JFrame {
public JScrollPane jScrollPane1 = new JScrollPane();
public JTable table;
public JScrollPane getScrollPane(){
    return this.jScrollPane1;
}

JPanel inFrame = new JPanel();
public TableFrame() {
    initComponents();

    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //this.jScrollPane1.setViewportView(inFrame);
    this.getContentPane().setLayout(new BorderLayout());
    this.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
                    setSize();
            }
    });
    this.getContentPane().add(this.jScrollPane1);

    this.jScrollPane1.setViewportView(this.inFrame);

}

public void setSize(){
    int w = this.getWidth();
    int width = 0;
    int height = 1000;
    if(w < 400){
        width = 10;
        this.inFrame.setBackground(Color.blue);
    }else{
        width = 1080;
        this.inFrame.setBackground(Color.red);
    }
    this.inFrame.setPreferredSize(new Dimension(width, height));
    this.jScrollPane1.revalidate();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    TableFrame frame = new TableFrame();

    frame.setVisible(true);


}

// Variables declaration - do not modify
// End of variables declaration

}

However, the JScrollPane's scrollbar doesn't work as expectedly. When I resize the frame from width great than 400 to less than 400, the scrollbar does not update correctly. What I need is that when the frame's width is smaller than a threshold, the panel should resize to a constant value, otherwise another value.

+1  A: 

the scrollbar does not update correctly.

You should be revalidationg the panel, not the scrollpane:

// this.jScrollPane1.revalidate();
this.inFrame.revalidate();
camickr