Reputation: 761
i have been having a large problem with my GUI. my first problem was that when i press a button that causes my JTextArea to display a string, he text area changes and thus pushes around all of the buttons and everything else in my GUI. i tried many solutions but nothing worked.
I put my text area in a scroll panel which may or may not have worked. i dont know because no text is displaying in the scroll panel. can someone look at my code and tell me what i am doing wrong?
thanks
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myClass extends JFrame implements ActionListener{
public JButton picture = new JButton();
public JTextArea description = new JTextArea(10,30);
public JButton B1 = new JButton();
public JTextArea C1 = new JTextArea();
public myClass (String STP){
super(STP);
makeGUI();
}
public static void main(String[] args){
myClass test = new myClass("story");
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if (source==B1){
description.setText("hello world");
C1.setText("hello world");
}
}
public void makeGUI(){
JScrollPane scroll = new JScrollPane(description);
JPanel pane = new JPanel(new GridBagLayout());
Container con = this.getContentPane();
setBounds(50,50,600,600); //(x,-y,w,h) from north west
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// picture
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill=GridBagConstraints.NONE;
gbc.ipadx=260;
gbc.ipady=310;
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=3;
pane.add(picture,gbc);
// button 1
B1.addActionListener(this);
gbc.fill=GridBagConstraints.NONE;
gbc.ipadx=0;
gbc.ipady=10;
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=1;
gbc.gridheight=1;
pane.add(B1,gbc);
//caption 1
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.ipadx=0;
gbc.ipady=30;
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=1;
gbc.gridy=3;
gbc.gridwidth=3;
gbc.gridheight=1;
C1.setEditable(false);
C1.setLineWrap(true);
C1.setWrapStyleWord(true);
pane.add(C1,gbc);
// description !this is the part im having a problem with!
gbc.fill=GridBagConstraints.BOTH;
gbc.ipadx=100;
gbc.ipady=170;
gbc.insets=new Insets(10,10,10,0);
gbc.gridx=2;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=1;
description.setEditable(false);
description.setLineWrap(true);
description.setWrapStyleWord(true);
scroll.add(description);
pane.add(scroll,gbc);
con.add(pane);
setVisible(true);
}
}
try pressing the small button in the bottom left corner. you should see text in both text areas, but only one works.
Upvotes: 2
Views: 1137
Reputation: 54639
This is a common mistake. So to elaborate the solution by Reimeus a bit:
The JScollPane
is a complex component that contains several sub-components. It may contain the Viewport
and possibly JScrollBar
s, as shown in this image from the How to use Scroll Panes tutorial. These components are arranged with a special layout manager (particularly, with a ScrollPaneLayout
).
When you just call scrollPane.add(componentThatShouldBeScrolled)
, then the new component may replace one of existing components, or screw up the whole layout unpredictably.
Instead of manually adding the contents for the scrollpane, you usually pass it to the JScollPane
constructor:
JScrollPane scrollPane = new JScrollPane(componentThatShouldBeScrolled);
Alternatively, if you have to replace the "component that should be scrolled", you could call
scrollPane.setViewportView(componentThatShouldBeScrolled);
Upvotes: 5
Reputation: 159784
Remove this statement which is replacing the ViewPortView
of the JScrollPane
component.
scroll.add(description);
The JTextArea
description
has already been set as the viewport view.
Upvotes: 5