开发者

Java (Swing): JScrollPane.setBounds() does not apply?

开发者 https://www.devze.com 2023-02-19 06:56 出处:网络
I\'m trying to create a simple JList with a scrollbar, and therefore i need to have the JList within a JScrollPane. So far, so good. However,开发者_C百科 for some reason i can\'t resize/position the J

I'm trying to create a simple JList with a scrollbar, and therefore i need to have the JList within a JScrollPane. So far, so good. However,开发者_C百科 for some reason i can't resize/position the JScrollPane!? It sounds logic that everything inside it should stretch to 100%, so if i set the JScrollPane to be 300px wide, the elements inside will be as well. Is that correct?

While you're at it, please critisize and give me hints if i should change something or optimize it.

Anyhow, here's the code:

package train;
import java.awt.*;
import javax.swing.*;

public class GUI {

    private DefaultListModel loggerContent = new DefaultListModel();
    private JList logger = new JList(loggerContent);

    GUI() {
        JFrame mainFrame = new JFrame("title");


        this.addToLog("testing testing");
        this.addToLog("another test");


        // Create all elements
        logger = new JList(loggerContent);

        JScrollPane logWrapper = new JScrollPane(logger);
        logWrapper.setBounds(10, 10, 20, 50);


        // Add all elements
        mainFrame.add(logWrapper);


        // Show everything
        mainFrame.setSize(new Dimension(600, 500));
        mainFrame.setVisible(true);
    }

    public void addToLog(String inputString) {
        int size = logger.getModel().getSize();
        loggerContent.add(size, inputString);
    }

}

Thanks in advance, qwerty

EDIT: Here's a screenshot of it running: http://i.stack.imgur.com/sLGgQ.png


The setVisibleRowCount() method of JList is particularly convenient for this, as suggested in the relevant tutorial. ListDemo is a good example.

Addendum:

please critisize and give me hints…

Well, since you ask: Don't invoke public methods in the constructor; make them private or invoke them after the constructor finishes. There's no need to find the last index for add(), when addElement() is available. Also, be sure to construct your GUI on the event dispatch thread .

import java.awt.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5422160 */
public class ListPanel extends JPanel {

    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);

    ListPanel() {
        list.setVisibleRowCount(5);
    }

    public void append(String inputString) {
        model.addElement(inputString);
    }

    private void init() {
        for (int i = 0; i < 10; i++) {
            this.append("String " + String.valueOf(i));
        }
        JFrame mainFrame = new JFrame("GUI");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane jsp = new JScrollPane(list);
        mainFrame.add(jsp);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ListPanel().init();
            }
        });
    }
}


The bounds & size of a component are generally ignored over that of it's preferred size and the constraints of the layout being used by the container.

To solve this problem, learn how to use layouts & apply them appropriately.


Try to put your JScrollPane inside a JPanel and add the panel to the frame.

JPanel panel = new JPanel();
panel.add (logWrapper);
mainFrame.add(panel);

Then set the bounds of the panel instead of the JScrollpane

panel.setBounds(10, 10, 20, 50);

The probles is that Swing uses layout managers to control child bounds property. Adding a JScrollpane directly to the main frame, doesn't allow you to choose right bounds properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号