1

I'm using JOptionPane. I don't understand why there's a lot of space between my lines on the window... I'm sorry I post a lot of question on JOptionPane...

Somebody knows what I'm doing wrong ??

What should I do ?

public class ControlPannelWindow extends JFrame{
    public void run(){
        EventQueue.invokeLater
        (
                new Runnable() 
                {
                    @Override
                    public void run() {
                        JPanel panel = new JPanel();
                        JPanel subPanelNbActions = new JPanel();
                        JPanel subPanelNbActions2 = new JPanel();
                        Container c = getContentPane();
                        c.setLayout(new FlowLayout());
                        panel.setLayout(new GridLayout(2,1));//+1 for radioButton, +2 for NB_ACTION
                        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} 
                        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){}

                        panel.add(subPanelNbActions);
                        panel.add(subPanelNbActions2);

                        String s = "                                                                                                                                                ";
                        JLabel labelNbAction = new JLabel(s);
                        JLabel labelNbAction2 = new JLabel(s);

                        labelNbAction.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 10));
                        labelNbAction2.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 10));

                        Timer t = new Timer
                                (
                                        500, new ActionListener() //500 -> time in ms -> 0.5 seconds
                                        {
                                            @Override
                                            public void actionPerformed(ActionEvent e) 
                                            {
                                                int nbAction = 5;
                                                labelNbAction.setText("Total action : " + Integer.toString(nbAction));
                                                labelNbAction2.setText("Total action : " + Integer.toString(nbAction+1));
                                            }
                                        }
                                );
                        t.setRepeats(true);
                        t.start();

                        subPanelNbActions.add(labelNbAction);
                        subPanelNbActions2.add(labelNbAction2);
                        JOptionPane.showMessageDialog(null, panel, "What do you want to do ?", JOptionPane.INFORMATION_MESSAGE);
                        t.stop();
                    }
                }
                );
    }
}

1 Answers1

0

Your problem is not the JOptionPane, but the panels you define for the labels!

You can give them borders, to make it visible to you:

panel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
subPanelNbActions.setBorder(BorderFactory.createLineBorder(Color.RED));
subPanelNbActions2.setBorder(BorderFactory.createLineBorder(Color.RED));
labelNbAction.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelNbAction2.setBorder(BorderFactory.createLineBorder(Color.BLACK));


You need to set the layout for the subpanels:

JPanel subPanelNbActions = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
JPanel subPanelNbActions2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

Maybe this helps, for further instructions: How to remove the padding between in the JPanel still using a flow layout?

Community
  • 1
  • 1
Dewey
  • 16
  • 2
  • Ok, this is working ! This is a small example. When I try to the big one I'm having a problem with radio buttons. When I add a new subpannel with radio buttons it brokes everything... having an idea ? – Benjamin Bettan Feb 25 '16 at 15:39
  • What do mean with "it brokes everything"? Do you get an exception? Does it look weird? Maybe you can post a screenshot. - I also tried with JRadioButtons and I had no problem. – Dewey Feb 25 '16 at 16:07
  • I mean I don't have exception but the space problem come back. – Benjamin Bettan Feb 25 '16 at 16:12
  • I tryed JRadioButton b = new JRadioButton("1"); JPanel subPanelRadioButton = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); panel.add(subPanelRadioButton); – Benjamin Bettan Feb 25 '16 at 16:12
  • Make sure to have an empty border set: `radioButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));` – Dewey Feb 25 '16 at 16:13
  • I tryed JRadioButton b = new JRadioButton("1"); JPanel subPanelRadioButton = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); subPanelRadioButton.add(b); panel.add(subPanelRadioButton); – Benjamin Bettan Feb 25 '16 at 16:18
  • I'll try what you said – Benjamin Bettan Feb 25 '16 at 16:18
  • Thanks I solve my problem with your solution. Have a good day ! – Benjamin Bettan Feb 25 '16 at 16:21