JScrollPane Resize Allowing JFrame Size


This code is show how to allow JScrollPane allowing size of parent size with GridBagLayout.
package myjavaexample; 
 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.border.EmptyBorder; 
 
public class MyJavaExample extends JFrame 
{ 
    public void setup() 
    { 
        this.setSize(800,600); 
        this.setLocation(200,200); 
        this.setTitle("JScrollPane Resize Allowing JFrame Size"); 
        this.setLayout(new GridBagLayout()); 
        GridBagConstraints con = new GridBagConstraints(); 
        con.fill = GridBagConstraints.BOTH; 
        con.weightx = 1; 
        con.weighty = 1; 
        con.gridx = 0; 
        con.gridy = 0; 

        JPanel panel=new JPanel(); 
        panel.setLayout(new GridBagLayout()); 
        panel.setBorder(new EmptyBorder(5, 10, 5, 10)); 
        GridBagConstraints con2 = new GridBagConstraints(); 
        con2.fill = GridBagConstraints.BOTH; 
        con2.weightx = 1; 
        con2.weighty = 1; 
        con2.gridx = 0; 
        con2.gridy = 0; 

        JTextArea textArea1=new JTextArea(); 
        textArea1.setLineWrap(true); 
        JTextArea textArea2=new JTextArea(); 
        textArea2.setLineWrap(true); 

        JScrollPane areaScrollPane = new JScrollPane(textArea1); 
        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
        areaScrollPane.setPreferredSize(new Dimension(300,400)); 
        panel.add(areaScrollPane,con2); 

        areaScrollPane = new JScrollPane(textArea2); 
        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
        areaScrollPane.setPreferredSize(new Dimension(300,400)); 
        con2.gridx = 1; 
        panel.add(areaScrollPane,con2); 

        this.setMinimumSize(new Dimension(300,300)); 
        this.add(panel,con); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
        this.pack(); 
        this.setVisible(true); 
    } 

    public static void main(String[] args) { 
        MyJavaExample mainwin=new MyJavaExample(); 
        mainwin.setup(); 
        mainwin.setVisible(true); 
    } 
} 

Comments



Popular posts from this blog

Simple Java Code HTML Highlighter

How to Use Extends and Implements

Fibonacci Algorithm in Java Array