Reputation: 117
Can you help me with this problem? I write layout to BitTorrent's client and I don't have any idea why JScroll doesn't work in right panel JSplitPane. More code here: https://github.com/niesuch/bittorrentclient/blob/nie_bittorrentclient/src/Bittorrent.java Thanks in advance.
/**
* Initialization information panel
*/
private void _initInfoPanel() {
JPanel formPanel = new JPanel();
formPanel.setBorder(BorderFactory.createTitledBorder("Informations"));
JPanel form = new JPanel(new GridLayout(0, 2));
int i = 0;
for (String formLabel : _formLabels) {
form.add(new JLabel(formLabel));
_textFields[i] = new JTextField(10);
form.add(_textFields[i++]);
}
formPanel.add(form);
_infoPanel.add(formPanel);
_infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER);
}
My minimal example program:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
public class Test extends JFrame
{
private final JPanel _downloadsPanel;
private final JPanel _buttonsPanel;
private final JPanel _infoPanel;
private final JTextField[] _textFields;
private final String[] _formLabels =
{
"Name: ", "Size: ", "% downloaded: ", "Status: ",
"Download: ", "Upload: ", "Time remaining: ", "Pieces: ",
"Peer data including IP addresses: ", "Speed download from them: ",
"Speed upload to them: ", "Port using: ", "Port client: "
};
private JButton _pauseButton;
public Test() {
setTitle("BitTorrent");
setSize(1024, 768);
_downloadsPanel = new JPanel();
_buttonsPanel = new JPanel();
_infoPanel = new JPanel();
_textFields = new JTextField[_formLabels.length];
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, _downloadsPanel, _infoPanel);
splitPane.setResizeWeight(0.7);
_initInfoPanel();
_initButtonsPanel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(_buttonsPanel, BorderLayout.SOUTH);
getContentPane().add(splitPane, BorderLayout.CENTER);
}
private void _initInfoPanel()
{
JPanel formPanel = new JPanel();
formPanel.setBorder(BorderFactory.createTitledBorder("Informations"));
JPanel form = new JPanel(new GridLayout(0, 2));
int i = 0;
for (String formLabel : _formLabels)
{
form.add(new JLabel(formLabel));
_textFields[i] = new JTextField(10);
form.add(_textFields[i++]);
}
formPanel.add(form);
_infoPanel.add(formPanel);
_infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER);
}
private void _initButtonsPanel()
{
_pauseButton = new JButton("Pause");
_pauseButton.setEnabled(false);
_buttonsPanel.add(_pauseButton);
}
public static void main(String[] args)
{
Test bittorrent = new Test();
bittorrent.setVisible(true);
}
}
Upvotes: 2
Views: 66
Reputation: 285405
It's a simple error. You're treating _infoPanel as if it uses BorderLayout, e.g.,
_infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER); //!!
But it doesn't and instead uses JPanel's default FlowLayout:
_infoPanel = new JPanel();
FlowLayout will display components at their preferredSizes, and will not try to shrink or expand them, and so the JScrollPane will not change its size. So make the obvious change:
_infoPanel = new JPanel(new BorderLayout());
Upvotes: 1