0

i have the following code, where i try to make some jLabels/jComboBox visible/invisible and move the location of those that are visible on jRadioButton click.

the issue is that the location is updated only on second click.

    private void SingleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

   this.serverLabel.setVisible(true);
   this.serverList.setVisible(true);


   this.serverLabel.setLocation(this.hostGroupLabel.getLocation().x, this.cpuCountSpinner.getLocation().y);
   this.serverList.setLocation(this.cpuCountSpinner.getLocation().x, this.cpuCountSpinner.getLocation().y);

   this.jXDatePickerStartDate.setLocation(153, jXDatePickerStartDate.getLocation().y);

   this.requestedRamLabel.setVisible(false);
   this.ramText.setVisible(false);
   this.cpuLabel.setVisible(false);
   this.cpuCountSpinner.setVisible(false);

}  
caza27
  • 1
  • you might want to have a look at [this answer](https://stackoverflow.com/a/1424762/7109162) even if it might not solve your issue. also with only the action we can't really help you. post more code – XtremeBaumer Jul 12 '17 at 06:51
  • i am Detecting JRadioButton state change,but as i mention only if i click the button again after he is already click the location is changing. – caza27 Jul 12 '17 at 06:59
  • 2
    1) You probably need to repaint your frame. 2) Never use setLocation method. Use an appropriate layout manager instead. – Sergiy Medvynskyy Jul 12 '17 at 07:00
  • Have you tried calling `revalidate` to trigger a new layout pass and `repaint` to trigger a new paint pass – MadProgrammer Jul 12 '17 at 07:00
  • when i use revalidate and then repaint it doesn't work at all. – caza27 Jul 12 '17 at 07:07
  • stick to the convention, method name should be camelCasedStartingWithLowerLetteer – Antoniossss Jul 12 '17 at 07:35
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 12 '17 at 08:33
  • [Here's one idea](https://stackoverflow.com/questions/27974966/moving-jpasswordfield-to-absolute-position/27975101#27975101) for placing components in radnome locations – MadProgrammer Jul 12 '17 at 08:33
  • [Here's another take on the idea](https://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) – MadProgrammer Jul 12 '17 at 08:37

1 Answers1

0

I dont know why it is not working for you, mayby you are modifying somewhere else your GUI, or event is not fired at all. Here is working example. You should pase the code where you actually append the listener to the button. You did that right? It should be something like:

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            singleButtonActionPerformed(e)
        }
    });

or Java8 variant using Lambdas

button.addActionListener(this::singleButtonActionPerformed)

but using this depends on context. It should be the object hta holds given method.

Here is working example with button and radio button (as suggested)

public class SettingLocation {

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    f.setContentPane(p);

    p.setLayout(new FlowLayout());

    JButton b = new JButton("Click me");
    b.setBounds(150, 150,100,100);
    p.add(b);

    JRadioButton rb=new JRadioButton("Exmaple radio");
    p.add(rb);
    rb.addActionListener(new ActionListener() {
        Random r = new Random();

        @Override
        public void actionPerformed(ActionEvent e) {
            rb.setLocation(r.nextInt(300), r.nextInt(300));
        }
    });

    b.addActionListener(new ActionListener() {
        Random r = new Random();

        @Override
        public void actionPerformed(ActionEvent e) {
            b.setLocation(r.nextInt(300), r.nextInt(300));
        }
    });
    f.setVisible(true);

}
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • make the example with `JRadioButton` as that seems to be the problem – XtremeBaumer Jul 12 '17 at 07:36
  • @XtremeBaumer ezpz - done, no trobules with that - radio button added an changing location. I bet OP is not attaching action listener nowhere as he only showed SOME METHOD of his that suppose to handle the event, but he didnt show how he attaches it. – Antoniossss Jul 12 '17 at 07:41
  • 1
    Since `p` is using a `FlowLayout`, any modifications you make to the location will be null and void when the container is revalidated - try resizing the window and see what happens – MadProgrammer Jul 12 '17 at 08:32
  • @MadProgrammer it works as it is. Setting layout manager to null works as well - ofc with setting proper bounds to both components beforehand. – Antoniossss Jul 12 '17 at 08:35
  • Revalidate the container, either by resizing the window calling `revalidate`, the components will be repositioned according to the desires of the layout manager. Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jul 12 '17 at 08:36