I'm trying to write an apartment management program with a TabbedPane, I created a class extending JPanel with GroupLayout and added it to my TabbedPane. I have two JTextAreas in this class and I put them in JScrollPanes.
When I write them anything long, their ScrollPanes are growing horizontally, how can I prevent it.
I tried to add textarea.setLineWrap(true);
line, it solves my problem but it generates a new problem; I cannot resize them automatically my ScrollPanes. So they becomes in fixed size.
JTextArea diger = new JTextArea();
JScrollPane digerS = new JScrollPane(diger);
JTextArea rapor = new JTextArea();
JScrollPane raporS = new JScrollPane(rapor);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGap(5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(kisiBilgileri)
.addComponent(daireBilgileri)
.addComponent(iletisimBilgileri)
.addComponent(_diger, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(digerS, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE) //textarea1's scrollpane
)
.addGap(5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(ara, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(daireSec, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(kaydet, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(sil, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(_rapor, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(raporS, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE) //textarea2's scrollpane
.addGroup(layout.createSequentialGroup()
.addComponent(aidatAy)
.addGap(5)
.addComponent(aidatEvDurumu)
)
.addComponent(aidatTuru)
.addGroup(layout.createSequentialGroup()
.addComponent(aidatMiktar)
.addGap(5)
.addComponent(aidatOde)
)
)
.addGap(5)
);
JTextArea diger = new JTextArea();
When you create a JTextArea like above the text area doesn't know how to size itself so it may change in size as text is added/removed (depending on the layout manager used, I don't know how GroupLayout works).
Instead you should use something like:
JTextArea diger = new JTextArea(5, 20);
to suggest the number of row and columns for the text area. Now the text area can determine its own preferred size and the layout manager can use this information.
Note, the same applies for a JTextField, except you can only specify the columns.