JTextArea truncated to JFrame borders

When printing up to 363 lines in JTextArea, TextArea will be truncated to approximately line 43 (frame frame). I already set JTextArea in JScrollPane and set ScrollPolicy to ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS .... How do I get JTextArea to scroll when I write content out of bounds?

public static double principalAmt, intRate, balance, monthlyIntRate, monthlyPayment, monthlyIntPayment, monthlyPrincipalPayment; public static int termLength, months; static Box infoBox = Box.createVerticalBox(); static Box graphBox = Box.createVerticalBox(); public static void main(String[] args) { new AmortizationTable(); } public AmortizationTable() { // User input principalAmt = 150000; termLength = 15; intRate = 0.05; // Calculated values balance = principalAmt; monthlyIntRate = intRate / 1200; months = termLength * 12; monthlyPayment = principalAmt * (monthlyIntRate * Math.pow(1 + monthlyIntRate, months) / (Math.pow(1 + monthlyIntRate, months) - 1)); JFrame amortFrame = new JFrame(); JPanel amortPanel = new JPanel(); Box amortBox = Box.createVerticalBox(); JTextArea amortText = new JTextArea(); amortText.setEditable(false); String[] amortArray = new String[months]; JLabel amortTitle1 = new JLabel("---------------------------------------------------" + "-------------------------------------------"); JLabel amortTitle2 = new JLabel("Payment\tMonthly Payment\tInterest Paid\t\tPrincipal Paid\t\tBalance"); JLabel amortTitle3 = new JLabel("-------------------------------------------------" + "---------------------------------------------"); amortText.setText(amortText.getText() + amortTitle1.getText() + "\n"); amortText.setText(amortText.getText() + amortTitle2.getText() + "\n"); amortText.setText(amortText.getText() + amortTitle3.getText() + "\n"); for(int i=0; i<months; i++) { monthlyIntPayment = balance * monthlyIntRate; monthlyPrincipalPayment = monthlyPayment - monthlyIntPayment; balance -= monthlyPrincipalPayment; if(balance<0) balance = -balance; amortArray[i] = ("" + (i+1) + "\t" + new DecimalFormat("$0.00").format(monthlyPayment) + "\t\t" + new DecimalFormat("$0.00").format(monthlyIntPayment) + "\t\t" + new DecimalFormat("$0.00").format(monthlyPrincipalPayment) + "\t\t" + new DecimalFormat("$0.00").format(balance) + "\t\n"); amortText.setText(amortText.getText() + amortArray[i]); } JScrollPane amortScroll = new JScrollPane(amortText); amortScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); amortBox.add(amortScroll); amortPanel.add(amortBox); amortFrame.add(amortPanel); amortFrame.setVisible(true); } 
+4
source share
1 answer

You should always call frame.pack() or frame.setSize(...) before calling frame.setVisible (true). Generally, it is best to use frame.pack () so that the Swing components are colored according to their preferred sizes.

So, you really need to give the text area a suggestion about what its preferred size should be. This is done using:

 JTextArea amortText = new JTextArea(25, 70); 

Finally, JTable will be the best Swing component since it supports tabular data. JTextArea should not be used for formatted data.

+3
source

Source: https://habr.com/ru/post/1499525/


All Articles