I can not figure out one problem, I have a JTextArea form in my application and a JScrollPane attached to it. But I don’t know how to make a working scrolling, when the JtextArea field is completely filled with text, it starts to increase its vertical size and enters the progress bar and buttons located below. I would like to correct this situation.

Here is the code

package main.java.view; import javax.swing.*; import java.awt.*; public class ShopQueueGUIApp { public static final int W_WIDTH = 540; public static final int W_HEIGHT = 500; public static void main(String[] args) { JFrame mainForm = new JFrame("Queue Shop v0.3 alpha"); mainForm.setSize(W_WIDTH,W_HEIGHT); // Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΎΠΊΠ½Π° mainForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Π·Π°ΠΊΡ€Ρ‹Π²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ΠΌ крСстика mainForm.setLocationRelativeTo(null); // Ρ€Π°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚ΡŒ ΠΎΠΊΠ½ΠΎ ΠΏΠΎ Ρ†Π΅Π½Ρ‚Ρ€Ρƒ экрана mainForm.setResizable(false); // Π·Π°ΠΏΡ€Π΅Ρ‚ΠΈΡ‚ΡŒ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠ² ΠΎΠΊΠ½Π° mainForm.setLayout(new GridBagLayout()); // Π‘Π»ΠΎΠΉ // ВСкстовая информация ΠΎ Ρ€Π°Π±ΠΎΡ‚Π΅ прилоТСния //-------------------------------------------------------------- JLabel infoLabel = new JLabel("Данная ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ° ΠΌΠΎΠ΄Π΅Π»ΠΈΡ€ΡƒΠ΅Ρ‚"); JLabel infoLabel2 = new JLabel("ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ ΠΏΠΎΠΊΡƒΠΏΠ°Ρ‚Π΅Π»Π΅ΠΉ Π² кассы"); JLabel infoLabel3 = new JLabel("ΠΌΠ°Π³Π°Π·ΠΈΠ½Π°."); JLabel infoLabel4 = new JLabel("ΠšΠ°ΠΆΠ΄ΡƒΡŽ ΠΌΠΈΠ½ΡƒΡ‚Ρƒ Π·Π°Ρ…ΠΎΠ΄ΠΈΡ‚ ΠΈΠ»ΠΈ Π½Π΅"); JLabel infoLabel5 = new JLabel("Π·Π°Ρ…ΠΎΠ΄ΠΈΡ‚ ΠΎΠ΄ΠΈΠ½ ΠΏΠΎΠΊΡƒΠΏΠ°Ρ‚Π΅Π»ΡŒ, это"); JLabel infoLabel6 = new JLabel("опрСдСляСтся случайным ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ."); JLabel infoLabel7 = new JLabel("ΠŸΠΎΠΊΡƒΠΏΠ°Ρ‚Π΅Π»ΡŒ Ρ‚Π°ΠΊΠΆΠ΅ случайно Π²Ρ‹Π±ΠΈΡ€Π°Π΅Ρ‚"); JLabel infoLabel8 = new JLabel("ΠΎΠ΄Π½Ρƒ ΠΈΠ· пяти касс ΠΌΠ°Π³Π°Π·ΠΈΠ½Π°,"); JLabel infoLabel9 = new JLabel("обслуТиваСтся Π½Π° протяТСнии"); JLabel infoLabel10 = new JLabel("ΠΎΡ‚ ΠΎΠ΄Π½ΠΎΠΉ Π΄ΠΎ Ρ‚Ρ€Π΅Ρ… ΠΌΠΈΠ½ΡƒΡ‚, ΠΈ Π·Π°Ρ‚Π΅ΠΌ"); JLabel infoLabel11 = new JLabel("ΠΏΠΎΠΊΠΈΠ΄Π°Π΅Ρ‚ ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ."); JLabel infoLabel12 = new JLabel("Π”Π°Π½Π½Ρ‹ΠΉ процСсс пошагово"); JLabel infoLabel13 = new JLabel("отобраТаСтся Π² ΠΎΠΊΠ½Π΅ справа."); JLabel infoLabel14 = new JLabel("Π—Π°Π΄Π°ΠΉΡ‚Π΅ врСмя модСлирования"); JLabel infoLabel15 = new JLabel("(ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - 30 ΠΌΠΈΠ½ΡƒΡ‚)"); JLabel infoLabel16 = new JLabel("НаТмитС ΠΊΠ½ΠΎΠΏΠΊΡƒ \"+1 ΠΌΠΈΠ½ΡƒΡ‚Π°\""); JLabel infoLabel17 = new JLabel("Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΡ‚ΡŒ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ врСмя"); JLabel infoLabel18 = new JLabel("Π½Π° 1 ΠΌΠΈΠ½ΡƒΡ‚Ρƒ."); JLabel infoLabel19 = new JLabel("НаТмитС ΠΊΠ½ΠΎΠΏΠΊΡƒ \"Π‘Ρ‚ΠΎΠΏ\""); JLabel infoLabel20 = new JLabel("Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ процСсс"); JLabel infoLabel21 = new JLabel("модСлирования."); //-------------------------------------------------------------- // РасполоТСниС тСкстовой ΠΈΠ½Ρ„Ρ‹ //-------------------------------------------------------------- mainForm.add(infoLabel,new GridBagConstraints(0,0,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel2,new GridBagConstraints(0,1,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel3,new GridBagConstraints(0,2,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel4,new GridBagConstraints(0,3,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel5,new GridBagConstraints(0,4,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel6,new GridBagConstraints(0,5,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel7,new GridBagConstraints(0,6,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel8,new GridBagConstraints(0,7,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel9,new GridBagConstraints(0,8,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel10,new GridBagConstraints(0,9,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel11,new GridBagConstraints(0,10,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel12,new GridBagConstraints(0,11,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel13,new GridBagConstraints(0,12,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel14,new GridBagConstraints(0,13,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel15,new GridBagConstraints(0,14,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel16,new GridBagConstraints(0,15,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel17,new GridBagConstraints(0,16,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel18,new GridBagConstraints(0,17,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel19,new GridBagConstraints(0,18,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel20,new GridBagConstraints(0,19,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(infoLabel21,new GridBagConstraints(0,20,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); //-------------------------------------------------------------- // ПолС Π²Π²ΠΎΠ΄Π° Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ ΠΈ ΠΊΠ½ΠΎΠΏΠΊΠ° для Π΅Π³ΠΎ установки //-------------------------------------------------------------- JTextField endTimeLineField = new JTextField(); JButton setTimeButton = new JButton("Π—Π°Π΄Π°Ρ‚ΡŒ врСмя"); // ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‹ΡˆΠ΅ΠΎΠΏΠΈΡΠ°Π½Π½Ρ‹Ρ… элСмСнтов mainForm.add(endTimeLineField,new GridBagConstraints(0,21,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(setTimeButton,new GridBagConstraints(0,22,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); //-------------------------------------------------------------- // Консоль Π²Ρ‹Π²ΠΎΠ΄Π° //-------------------------------------------------------------- JTextArea console = new JTextArea(20,1); //console.setEditable(false); console.setWrapStyleWord(true); console.setLineWrap(true); JScrollPane scroll = new JScrollPane(console); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); JLabel consLabel = new JLabel("Консоль Π²Ρ‹Π²ΠΎΠ΄Π°:"); // ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‹ΡˆΠ΅ΠΎΠΏΠΈΡΠ°Π½Π½Ρ‹Ρ… элСмСнтов mainForm.add(console,new GridBagConstraints(1,1,1,24,0.0,0.0,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(consLabel,new GridBagConstraints(1,0,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(scroll,new GridBagConstraints(2,1,1,20,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.VERTICAL,new Insets(0,0,0,0),0,0)); //-------------------------------------------------------------- // Кнопки управлСния роСссом модСлирования //-------------------------------------------------------------- JButton stepButton = new JButton("+1 ΠœΠΈΠ½ΡƒΡ‚Π°"); JButton stopButton = new JButton("ΠžΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ ΠΌΠΎΠ΄Π΅Π»ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅"); // ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‹ΡˆΠ΅ΠΎΠΏΠΈΡΠ°Π½Π½Ρ‹Ρ… элСмСнтов mainForm.add(stopButton,new GridBagConstraints(1,23,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); mainForm.add(stepButton,new GridBagConstraints(0,23,1,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); //-------------------------------------------------------------- // ΠŸΡ€ΠΎΠ³Ρ€Π΅ΡΡ Π±Π°Ρ€ //-------------------------------------------------------------- JProgressBar timeLineBar = new JProgressBar(); timeLineBar.setStringPainted(true); timeLineBar.setMinimum(0); timeLineBar.setMaximum(100); timeLineBar.setValue(25); // ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‹ΡˆΠ΅ΠΎΠΏΠΈΡΠ°Π½Π½Ρ‹Ρ… элСмСнтов mainForm.add(timeLineBar,new GridBagConstraints(1,22,2,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(3,1,1,1),0,0)); // ΠœΠ΅Ρ‚ΠΊΠ° для прогрСсс-Π±Π°Ρ€Π° JLabel progressLabel = new JLabel("ΠŸΡ€ΠΎΠ³Ρ€Π΅ΡΡ:"); // ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Ρ‹ΡˆΠ΅ΠΎΠΏΠΈΡΠ°Π½Π½Ρ‹Ρ… элСмСнтов mainForm.add(progressLabel,new GridBagConstraints(1,21,2,1,0.0,0.9,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(1,70,1,1),0,0)); //-------------------------------------------------------------- mainForm.pack(); mainForm.setVisible(true); // Π²ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ Π²ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ ΠΎΠΊΠ½Π° } } 

Here's what it looks like: enter image description here

    1 answer 1

    The problem is solved, it turns out that after:

     JTextArea console = new JTextArea(20,20); JScrollPane scroll = new JScrollPane(console); 

    Only scroll should be added to the form. Ie remove the line:

     mainForm.add(console,new GridBagConstraints(1,1,1,24,0.0,0.0,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));