简体   繁体   中英

Dynamic button placement issue in a Swing application

My Aim is that when i click button b1("SHOW"),button b2("BUTTON 2") should appear at 10px to the right of button b1.When i'm clicking button b1 for the 1st time,it works as intended but from second times the button b2 is getting placed at (0,0) position.why?

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import java.awt.event.*;
import javax.swing.*;

class DynamicSpring implements ActionListener
{
  JPanel jp;
  JFrame jf;
  JButton b1,b2;
  SpringLayout sl;
  DynamicSpring()
  {
     jf=new JFrame("DynamicSpring");
     jf.setSize(500,500);
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     sl=new SpringLayout();
     jp=new JPanel(sl);

     b1=new JButton("SHOW");
     b1.setSize(100,100);
     sl.putConstraint(SpringLayout.WEST,b1,10,SpringLayout.WEST,jp);     
      sl.putConstraint(SpringLayout.NORTH,b1,10,SpringLayout.NORTH,jp);  
     b1.addActionListener(this);
     jp.add(b1); 

     b2=new JButton("Button 2");
     b2.setSize(100,100);


    jf.setContentPane(jp);
    jf.setVisible(true);



  }

  public void actionPerformed(ActionEvent ae)
  {
      sl.putConstraint(SpringLayout.WEST,b2,10,SpringLayout.EAST,b1);
      jp.add(b2);

      jp.repaint();
       jp.validate();
  }

  public static void main(String... s)
  {
     new DynamicSpring();
  }

}

The problem seems to be that when you press the button more than once, you add the same button multiple times to the panel and the SpringLayout cannot handle it.

You can avoid this by removing b1 before doing anything else in your actionPerformed :

public void actionPerformed(ActionEvent ae)
{
      jp.remove(b2);  /* remove the button first! */
      sl.putConstraint(SpringLayout.WEST,b2,10,SpringLayout.EAST,b1);
      jp.add(b2);
      jp.repaint();
      jp.validate();
}

This should not cause any problems the first time as removing a non-existent element has no effect!

Use MigLayout instead of SpringLayout. With MigLayout you can use the hidemode constraints:

hidemode : Sets the hide mode for the component. If the hide mode has been specified in the This hide mode can be overridden by the component constraint. The hide mode specified how the layout manager should handle a component that isn't visible. The modes are:

0 -Default. Means that invisible components will be handled exactly as if they were visible.

1 -The size of the component (if invisible) will be set to 0, 0.

2 -The size of the component (if invisible) will be set to 0, 0 and the gaps will also be set to 0 around it.

3 -Invisible components will not participate in the layout at all and it will for instance not take up a grid cell.

Your code will become:

DynamicString() {
    jp=new JPanel(sl);
    b1=new JButton("SHOW");
    b1.addActionListener(this);
    jp.add(b1,"newline, w 100!, h 100!"); 

    b2=new JButton("Button 2");
    jp.add(b2,"w 100!, h 100!, hidemode 0"); // use whatever hidemode mode that's the more convenient for you
    b2.setVisible(false);

    jf.setContentPane(jp);
    jf.setVisible(true);

    }

public void actionPerformed(ActionEvent ae) {
    b2.setVisible(true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM