1

I am building a Java Swing app, which I want it will work this way:

When you resize the window, the JScrollPane object (which is within a JSplitPane) should redimension its minimum, maximum and preferred size taking as reference the current window width.

I fixed this settings on the method componentResized() of ComponentAdapter class, however, it doesn't work as suppose must do.

This is the code. I wish you test it on your pc and can help me.

Thanks a lot for your time payed.

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


public class ComVisor extends JFrame
{
private JList imagesList;
private JPanel imagePanel;
private JSplitPane mainPanel;
private JScrollPane scrollPanelRight;
private int width;

public ComVisor(String nameApplication)
{
    setFrame(nameApplication);
    setComponents();
}

private void setFrame(String nameApplication)
{
    setLayout(new BorderLayout(1, 3));
    setTitle(nameApplication);
    setMinimumSize(new Dimension(400, 200));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
    setLocationRelativeTo(null);

    this.addWindowListener(
        new WindowAdapter() 
        {
            @Override
            public void windowClosing(WindowEvent e) 
            { 
                JOptionPane.showMessageDialog(ComVisor.this, nameApplication + "-Salida"); 

                return;
            }
        }
    );
    this.addComponentListener(
        new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent E)
            {   
                width = getWidth();

                scrollPanelRight.setPreferredSize(new Dimension(width / 3, 0));
                scrollPanelRight.setMinimumSize(new Dimension(width / 7, 0));
                scrollPanelRight.setMaximumSize(new Dimension(width / 5 * 4, 0));

                return;         
            }
        }
    );  
}

private void setComponents()
{
    String[] a = {"dsdsdsd", "dsdsdkkhskj", "dskhkjdhskjdhksdh", "sdskdhskjhds"};
    JButton b = new JButton("Soy un boton xD");
    JPanel p = new JPanel();
    imagesList = new JList(a);
    p.add(b);
    imagesList.setVisibleRowCount(100);
    scrollPanelRight = new JScrollPane(imagesList);
    mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPanelRight, p);
    add(mainPanel, BorderLayout.CENTER);

    return;
}

private class Manejador implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent listener)
    {
        return;
    }
}
}

and this is the main class, which calls a Comvisor object

import static javax.swing.SwingUtilities.invokeLater;

public class Principal
{
public static void main(String[] args)
{
    invokeLater(
        new Runnable()
        {
            @Override
            public void run()
            {
                new ComVisor("ComVisor").setVisible(true);

                return;
            }
        }
    );

    return;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
EmmanCanVaz_95
  • 123
  • 1
  • 8
  • 1
    just FYI: You can omit the return statement in your main method as well as in the runnable's run method. Any void method has an implicit return statement at the end of it, you don't need to write it down. – user3237736 Sep 12 '16 at 00:34
  • 1
    *"When you resize the window, the JScrollPane object (which is within a JSplitPane) should redimension its minimum, maximum and preferred size taking as reference the current window width."* Why? What is it you're actually trying to achieve by doing so? Note 1) that with the current layout, the `scrollPanelRight` expands and contracts according to the available space. 2) While the size changes, you should not be messing with either the preferred or maximum sizes. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) – Andrew Thompson Sep 12 '16 at 01:14
  • 1
    Is it really that the **divider of the split pane** should move on window resizing? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) And if so, use something like `mainPanel.setResizeWeight(.3);` – Andrew Thompson Sep 12 '16 at 01:15
  • Thaks a lot for help me! – EmmanCanVaz_95 Sep 22 '16 at 02:49

0 Answers0