user1300830
user1300830

Reputation: 83

Java, JFC, Swing

I am having trouble with JscrollPane. Trying to add few buttons in a row, and continue with a set of rows, it shows up fine initially, but as soon as I click on the scroll bar, all the buttons get sort of word wrapped in the frame. I would like to use setBounds (without any layout) as I have to extend/reduce the rows depending on the number of elements.

What am I doing wrong?

Pic appears here: https://drive.google.com/drive/u/0/folders/0B-m6SCJULOTRdHZ6cUwxX041SHM

public class JFrameWindow extends JFrame {
private int num = 0;
private JPanel container = null;
private JScrollPane jsp = null;

public JFrameWindow( String framName ) {
    super( framName );
    container = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            //g2.setPaint(Paint);
            //g2.fillOval(0, 0, getWidth(), getHeight());
            g2.dispose();
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1200, 800);
        };
    };
    //container.setLayout(mgr);

    jsp = new JScrollPane();
    jsp.setViewportView(container);
    jsp.setPreferredSize(new Dimension(1200, 800));
    container.setPreferredSize(new Dimension(1200, 800));
    //jsp.setBorder(new EtchedBorder(Color.BLACK, Color.GREEN));
    //jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.add( jsp );
    setSize(1200, 800);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);        
}

public void addButtons(String id, int a, int b) {
    JButton button1 = new JButton(id);
    button1.setBounds(10, num*30 + 10, 100, 20);
    container.add(button1);

    JButton stateButton = new JButton("ABC");
    stateButton.setBounds(120, num*30 + 10, 100, 20);
    container.add(stateButton);

    JButton custButton = new JButton("DEF");
    custButton.setBounds(230, num*30 + 10, 100, 20);
    container.add(custButton);

    JButton int1Button = new JButton("" + a);
    int1Button.setBounds(340, num*30 + 10, 100, 20);
    container.add(int1Button);

    JButton int2Button = new JButton("" + b);
    int2Button.setBounds(450, num*30 + 10, 100, 20);
    container.add(int2Button);

    num++;
    container.repaint();
    jsp.repaint();
    this.repaint();
}

public static void main( String [] args ) {
    JFrameWindow window = new JFrameWindow ( "Test" );
    window.addButtons("Test1", 1, 2);
    window.addButtons("Test2", 1, 2);
    window.addButtons("Test3", 1, 2);
    window.addButtons("Test4", 1, 2);
    window.addButtons("Test5", 1, 2);
    window.addButtons("Test6", 1, 2);
    window.addButtons("Test7", 1, 2);
    window.addButtons("Test8", 1, 2);
    window.addButtons("Test9", 1, 2);
    window.addButtons("Test10", 1, 2);
    window.addButtons("Test11", 1, 2);
    window.addButtons("Test12", 1, 2);
    window.addButtons("Test13", 1, 2);
    window.addButtons("Test14", 1, 2);
    window.addButtons("Test15", 1, 2);
    window.addButtons("Test16", 1, 2);
    window.addButtons("Test17", 1, 2);
    window.addButtons("Test18", 1, 2);
    window.addButtons("Test19", 1, 2);
    window.addButtons("Test20", 1, 2);
    window.addButtons("Test21", 1, 2);
    window.addButtons("Test22", 1, 2);
    window.addButtons("Test23", 1, 2);
    window.addButtons("Test24", 1, 2);
    window.addButtons("Test25", 1, 2);
    window.addButtons("Test26", 1, 2);
    window.addButtons("Test27", 1, 2);
    window.addButtons("Test28", 1, 2);
    window.addButtons("Test29", 1, 2);
    window.addButtons("Test30", 1, 2);
}

}

Upvotes: 0

Views: 65

Answers (1)

camickr
camickr

Reputation: 324147

, but as soon as I click on the scroll bar, all the buttons get sort of word wrapped in the frame.

The panel is revalidated when you start scrolling) which will cause the layout manager to be invoked and the layout manager will then determine the size and location of all the components. The default layout manager for a JPanel is the FlowLayout so the components will wrap to a new line when the current line is full.

Don't attempt to use setBounds(...). Instead you can use layout managers the way there are designed to be used.

I would like to use setBounds (without any layout) as I have to extend/reduce the rows depending on the number of elements.

That is not a reason to not use a layout manager.

For example you might have a main panel with a vertical BoxLayout. Then every time you need to add a row of components, you create a child panel that uses a FlowLayout. You add the components to the child panel and then add the child panel to the main panel.

Then the scrollbar and scrollpane will work properly.

container.setPreferredSize(new Dimension(1200, 800));

Don't attempt to control the preferred size of the panel. Again the is the job of the layout manager. The scrollpane won't work properly with an artificial preferred size.

Upvotes: 2

Related Questions