user1375288
user1375288

Reputation: 41

Java Swing Button performing have of the function

I've written a java app that does a few different things, using Netbeans 7.0.1 and its build in gui builder. The problem I'm having is that on screen there are some text fields for user input. Initially one set of fields and a button to add up to 10. I have also got a remove button which removes a field. So basically the buttons add and remove jTextFields and jLabels to/from the panel.

There seems to be a lag when I'd click the buttons so I added some System.out.prints and found that sometimes the button would be pressed, the system would print what it was supposed to but just ignore the adding/removing of components.

Is this a known issue?(I can't find anything as yet, although I'm not 100% sure how to be wording my searches) Or am I doing something wrong?

Sample of code: Note: current components is a map with each component and its value

private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {                                       
    if(hiddenValue != 81) {
        currentComponents.get(hiddenValue).setVisible(true);
        currentComponents.get(hiddenValue + 1).setVisible(true);
        currentComponents.get(hiddenValue + 2).setVisible(true);
        currentComponents.get(hiddenValue + 3).setVisible(true);
        currentComponents.get(hiddenValue + 4).setVisible(true);
        currentComponents.get(hiddenValue + 5).setVisible(true);
        currentComponents.get(hiddenValue + 6).setVisible(true);
        currentComponents.get(hiddenValue + 7).setVisible(true);
        currentComponents.get(hiddenValue + 8).setVisible(true);
        hiddenValue =  hiddenValue + 10;
        numEntries++;
        removeButton.setVisible(true);
        removeButton.setEnabled(true);
        System.out.println(hiddenValue);
    }
    else {
        currentComponents.get(hiddenValue).setVisible(true);
        currentComponents.get(hiddenValue + 1).setVisible(true);
        currentComponents.get(hiddenValue + 2).setVisible(true);
        currentComponents.get(hiddenValue + 3).setVisible(true);
        currentComponents.get(hiddenValue + 4).setVisible(true);
        currentComponents.get(hiddenValue + 5).setVisible(true);
        currentComponents.get(hiddenValue + 6).setVisible(true);
        currentComponents.get(hiddenValue + 7).setVisible(true);
        currentComponents.get(hiddenValue + 8).setVisible(true);
        hiddenValue =  hiddenValue + 10;
        numEntries++;
        addButton.setVisible(false);
        addButton.setEnabled(false);
        System.out.println(hiddenValue);
    }
    }    

Upvotes: 1

Views: 233

Answers (3)

brimborium
brimborium

Reputation: 9512

If all you want is to hide/show some Components during runtime, I reccomend using MigLayout. It is a LayoutManager that allows you to define what happens if a component is invisible.

  • hidemode 0: invisible components takes up exactly the same amount of space as if it was visible.
  • hidemode 1: invisible components take up no space, but consume their grid cell.
  • hidemode 2: invisible components take up no space, consume their grid cell but the cells gaps are set to size 0.
  • hidemode 3: invisible components will not participate in the layout at all (they will not consume their grid cell).

It works a little different than the standard LayoutManagers, but they have good code samples and a great cheat sheet.

Upvotes: 2

Guillaume Polet
Guillaume Polet

Reputation: 47607

I have no issues hiding/showing components:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestVisibility {

    private class Pair<P1, P2> {
        public final P1 first;
        public final P2 second;

        public Pair(P1 first, P2 second) {
            this.first = first;
            this.second = second;
        }
    }

    private List<Pair<JLabel, JTextField>> pairs = new ArrayList<TestVisibility.Pair<JLabel, JTextField>>();

    protected void initUI() {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbcLabel = new GridBagConstraints();
        gbcLabel.gridx = 0;
        gbcLabel.weightx = 0.0;
        gbcLabel.weighty = 0.0;
        gbcLabel.insets = new Insets(3, 3, 3, 3);
        GridBagConstraints gbcTF = new GridBagConstraints();
        gbcTF.gridx = 1;
        gbcTF.fill = GridBagConstraints.HORIZONTAL;
        gbcTF.weightx = 1.0;
        gbcTF.weighty = 1.0;
        gbcTF.insets = new Insets(3, 3, 3, 3);
        for (int i = 0; i < 10; i++) {
            gbcLabel.gridy = i;
            gbcTF.gridy = i;
            JLabel label = new JLabel("Field " + (i + 1) + ":");
            JTextField textField = new JTextField(50);
            label.setLabelFor(textField);
            Pair<JLabel, JTextField> pair = new Pair<JLabel, JTextField>(label, textField);
            pairs.add(pair);
            frame.add(label, gbcLabel);
            frame.add(textField, gbcTF);
        }
        JButton button = new JButton("Toggle");
        button.addActionListener(new ActionListener() {

            private boolean visible = true;

            @Override
            public void actionPerformed(ActionEvent e) {
                visible = !visible;
                boolean isOdd = false;
                for (Pair<JLabel, JTextField> pair : pairs) {
                    boolean oddVisible = isOdd || visible;
                    pair.first.setVisible(oddVisible);
                    pair.second.setVisible(oddVisible);
                    isOdd = !isOdd;
                }
            }
        });
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 10;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        frame.add(button, gbc);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestVisibility().initUI();
            }
        });
    }
}

You should probably consider updating your post with an SSCCE

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

Really not good idea to call JComponents by their index(es), nor why to reinvent the wheel, please use (to avoiding any of XxxExceptions too)

for (Component c : currentComponents.getComponents()) {
    //if (c instanceof JButton) {
       c.setVisible(true);
    //}
}

similair example about JButtons ActionPerfomed with setEnabled(true / false) for all JComponents inside the JPanel

Upvotes: 2

Related Questions