使用RelativeLayout摆动层叠面板

时间:2016-09-03 16:43:02

标签: swing dynamic

使用Swing我尝试维护一个动态列表。为了显示问题,我将案例分解为一个简单的示例,并在此处附加代码。 JFrame DynamicDisplaySubPanelMain 实现JPanel DynamicSubPanel2 。 此JPanel逐行显示List的内容。最后,另一行允许将String添加到列表中。然后JPanel将自动显示更长的列表并提供一个新行来添加另一个String。 添加字符串工作正常 - 但不显示更改的列表。任何人都可以说出原因可能是什么。

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class DynamicSubPanelMain extends JFrame  {

    private static final long serialVersionUID = 1L;

    public DynamicSubPanelMain() {
        super("My Sample dynamic List"); 

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 500);

        DynamicSubPanel2 myPane = new DynamicSubPanel2();

        getContentPane().add(myPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) throws Exception {
        DynamicSubPanelMain menu = new DynamicSubPanelMain();
        menu.setVisible(true);
    }
}

这里是JPanel:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.brunchboy.util.swing.relativelayout.AttributeConstraint;
import com.brunchboy.util.swing.relativelayout.AttributeType;
import com.brunchboy.util.swing.relativelayout.DependencyManager;
import com.brunchboy.util.swing.relativelayout.RelativeLayout;

import de.gombers.tools.helpers.Tools;

public class DynamicSubPanel2 extends JPanel {
    private static final long serialVersionUID = 1L;
    static final Logger LOGGER = LoggerFactory.getLogger(Tools.getSimpleClassName());

    private RelativeLayout relativeLayout = new RelativeLayout();
    private int nextIndex=-1;

    private int ARRAY_SIZE=100;
    private JButton[] actionJB    = new JButton[ARRAY_SIZE];
    protected JTextField[] workspaceTF    = new JTextField[ARRAY_SIZE];

    private List<String> list = new ArrayList<>();


    public DynamicSubPanel2() {
        nextIndex=-1;
        list.add("My first object");
        list.add("My second object");
        rebuildPanel(); 
    }

    private void rebuildPanel() {
        relativeLayout = new RelativeLayout();
        this.setLayout(relativeLayout);
        String thisItem;
        String aboveItem=DependencyManager.ROOT_NAME;
        String prevItem=DependencyManager.ROOT_NAME;

        int linespace= 0;
        int boundary = 5;
        int gap = 5;
        int buttonWidth = 110;


        for (String text : list) {
            nextIndex++;

            actionJB[nextIndex]= new JButton("Edit");
            thisItem="editJB"+nextIndex;
            actionJB[nextIndex].setActionCommand("Edit");
            actionJB[nextIndex].addActionListener(new EditListener(text, nextIndex));
            relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                    new AttributeConstraint(aboveItem, AttributeType.TOP, linespace));
            relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                    new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, boundary));
            relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                    new AttributeConstraint(thisItem, AttributeType.LEFT, buttonWidth));
            this.add(actionJB[nextIndex], thisItem) ;
            prevItem=thisItem;
            aboveItem=thisItem;

            workspaceTF[nextIndex]= new JTextField(text);
            workspaceTF[nextIndex].setEditable(false);
            thisItem="workspaceTF"+nextIndex;
            workspaceTF[nextIndex].setEditable(false);
            relativeLayout.addConstraint(thisItem, AttributeType.BOTTOM,
                    new AttributeConstraint(prevItem, AttributeType.BOTTOM));
            relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                    new AttributeConstraint(prevItem, AttributeType.RIGHT, gap));
            relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                    new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.RIGHT, -boundary));
            this.add(workspaceTF[nextIndex], thisItem) ;
            prevItem=thisItem;

            linespace= 30;
        }

        nextIndex++;
        actionJB[nextIndex] = new JButton("Add");
        thisItem="addJB"+nextIndex;
        actionJB[nextIndex].setActionCommand("Add");
        actionJB[nextIndex].addActionListener(new AddListener(nextIndex));
        relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(aboveItem, AttributeType.TOP, linespace));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, boundary));
        relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(thisItem, AttributeType.LEFT, buttonWidth));
        this.add(actionJB[nextIndex], thisItem) ;
        prevItem=thisItem;
        aboveItem=thisItem;

        LOGGER.info("{}", list.size());
        workspaceTF[nextIndex]= new JTextField(list.size());
        thisItem="workspaceTF"+nextIndex;
        relativeLayout.addConstraint(thisItem, AttributeType.BOTTOM,
                new AttributeConstraint(prevItem, AttributeType.BOTTOM));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(prevItem, AttributeType.RIGHT, gap));
        relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.RIGHT, -boundary));
        this.add(workspaceTF[nextIndex], thisItem) ;
        prevItem=thisItem;

    }

    public JPanel getPanel() {
        return this;
    }



    class EditListener implements ActionListener {
        private String text;
        private int tfIndex;
        public EditListener(String text, int tfIndex) {
            this.text = text;
            this.tfIndex = tfIndex;
        }
        @Override
        public void actionPerformed(ActionEvent event) {
            if (event.getActionCommand().equals("Edit")) {
                EditWorker worker = new EditWorker(tfIndex);
                worker.execute();
            }
            else if (event.getActionCommand().equals("Save")) {
                SaveWorker worker = new SaveWorker(text, tfIndex);
                worker.execute();
            }
        }
    }

    class EditWorker extends SwingWorker {
        private int tfIndex;
        public EditWorker(int tfIndex) {
            this.tfIndex = tfIndex;
        }
        public Object doInBackground() throws InterruptedException, IOException {
            return null;
        }

        public void done() {
            workspaceTF[tfIndex].setBackground(Color.WHITE);
            workspaceTF[tfIndex].setEditable(true);
            actionJB[tfIndex].setText("Save");
            actionJB[tfIndex].setActionCommand("Save");
        }
    }

    class SaveWorker extends SwingWorker {
        private String text;
        private int tfIndex;
        public SaveWorker(String text, int tfIndex) {
            this.text = text;
            this.tfIndex = tfIndex;
        }
        public Object doInBackground() throws InterruptedException, IOException {
            list.remove(text);
            list.add(workspaceTF[tfIndex].getText());
            return null;
        }

        public void done() {
            workspaceTF[tfIndex].setEditable(false);
            workspaceTF[tfIndex].setBackground(Color.LIGHT_GRAY);
            actionJB[tfIndex].setText("Edit");
            actionJB[tfIndex].setActionCommand("Edit");
        }
    }



    class AddListener implements ActionListener {
        private int tfIndex;
        public AddListener(int tfIndex) {
            this.tfIndex = tfIndex;
        }
        @Override
        public void actionPerformed(ActionEvent event) {
            AddWorker worker = new AddWorker(tfIndex, this);
            worker.execute();
        }
    }

    class AddWorker extends SwingWorker {
        private  int tfIndex;
        private boolean success;
        private AddListener listener;
        public AddWorker(int tfIndex, AddListener listener) {
            this.tfIndex  = tfIndex;
            this.listener = listener;
        }
        public Object doInBackground() throws InterruptedException, IOException {
            list.add(workspaceTF[tfIndex].getText());
            return null;
        }

        public void done() {
                rebuildPanel();
                getPanel().repaint();
        }
    }
}

0 个答案:

没有答案