I am developing a desktop application in Java Swing, which should work like this:
Within the JFrame
, there is a JSplitPane
object that has a JList
within a JSrollPane
and a JPanel
. The first object must modify its minimum, maximum and preferred sizes by its methods setMinimumSize()
, setMaximumSize()
and setPreferredSize()
, which take as reference the width of the frame to be dimensioned. These three methods are within the componentResized()
method of the listening object ComponentAdapter
.
When starting the application, I do not respect the rules that I put in the arguments of the methods, and when it is resized, it does not take as reference the width of the frame.
This is the code. I hope you try it by resizing the frame and the component so that they realize my problem and can help me.
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");
}
}
);
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));zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
}
}
);
}
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);
}
private class Manejador implements ActionListener
{
@Override
public void actionPerformed(ActionEvent listener)
{
}
}
}
And this is the main class:
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);s
}
}
);
}
}