Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:54:17+00:00 2026-05-16T22:54:17+00:00

I have a JFrame which contains a JPanel . The frame can be resized

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:54:17+00:00Added an answer on May 16, 2026 at 10:54 pm

    the scrollbar does not update
    correctly.

    You should be revalidationg the panel, not the scrollpane:

    // this.jScrollPane1.revalidate();
    this.inFrame.revalidate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame that contains a display JPanel with JTextField and a control
I want to maximize a JPanel inside a JFrame when the user clicks a
I have a JFrame which contains a JApplet. There are shortcut keys that I
I am using swing to create my GUI. J have a JFrame containing one
I'm trying to create an application where there is a JSplitPane which contains the
I have a window that has two layers: a static background and a foreground
Till now, I have developed simple swing applications so there was no need to
I am writing a simple application in Java that does some particle simulation on
I'm wondering about the standard practice with inner classes (in Java but I suppose
Ok, so coming from a background of mostly perl, and mostly writing dirty little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.