Example usage for javax.swing JPanel remove

List of usage examples for javax.swing JPanel remove

Introduction

In this page you can find the example usage for javax.swing JPanel remove.

Prototype

public void remove(int index) 

Source Link

Document

Removes the component, specified by index , from this container.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.RED));
    BoxLayout mgr = new BoxLayout(panel, BoxLayout.Y_AXIS);
    panel.setLayout(mgr);/*www  .ja  v  a2s .c  o  m*/
    for (int i = 0; i < 5; i++) {
        JButton button = new JButton("Remove Hello World " + (i + 1));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(e -> {
            panel.remove(button);
            panel.revalidate();
            panel.repaint();
        });
        panel.add(button);
    }
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;/* w w w . ja  v a 2  s  .  c  o m*/
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    JPanel parentPanel = new JPanel();
    parentPanel.setLayout(new BorderLayout(10, 10));

    JPanel childPanel1 = new JPanel();
    childPanel1.setBackground(Color.red);
    childPanel1.setPreferredSize(new Dimension(300, 40));

    JPanel childPanel2 = new JPanel();
    childPanel2.setBackground(Color.blue);
    childPanel2.setPreferredSize(new Dimension(800, 600));

    JButton myButton = new JButton("Add Component ");
    myButton.addActionListener(e -> {
        parentPanel.remove(childPanel1);
        parentPanel.add(childPanel2, BorderLayout.CENTER);
        parentPanel.revalidate();//w  ww.java 2  s. c o m
        parentPanel.repaint();
        pack();
    });
    setLocation(10, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    parentPanel.add(childPanel1, BorderLayout.CENTER);
    parentPanel.add(myButton, BorderLayout.SOUTH);
    add(parentPanel);
    pack();
    setVisible(true);
}

From source file:com.aw.swing.mvp.grid.GridManager.java

private void configureGridTitleColor() {
    JPanel pnlGrid = ipView.getPnlGrid(gridIndex);
    pnlGrid.setBorder(BorderFactory.createLineBorder(new Color(131, 172, 219)));

    JPanel pnlTitGrid = ipView.getPnlTitGrid(gridIndex);
    pnlTitGrid.setBackground(new Color(131, 172, 219));
    pnlTitGrid.setLayout(/*from  w ww .j  av  a 2  s  .  c  o m*/
            new FormLayout("fill:16dlu:noGrow,left:4dlu:noGrow,fill:d:grow,left:4dlu:noGrow,right:pref:grow",
                    "center:16dlu:noGrow"));
    JLabel lblTitGrid = ipView.getLblTitGrid(gridIndex);
    lblTitGrid.setFont(new Font(lblTitGrid.getFont().getName(), Font.BOLD, 14));
    lblTitGrid.setForeground(Color.black);

    numRecords = new JLabel("");
    numRecords.setFont(new Font(lblTitGrid.getFont().getName(), Font.BOLD, 14));
    numRecords.setForeground(Color.black);
    numRecords.setVisible(showTotalRegistros);

    pnlTitGrid.remove(lblTitGrid);
    CellConstraints cc = new CellConstraints();
    pnlTitGrid.add(lblTitGrid, cc.xy(3, 1));
    pnlTitGrid.add(numRecords, cc.xy(5, 1));

}

From source file:com.anrisoftware.prefdialog.simpledialog.SimpleDialog.java

private void removeRestoreButton() {
    JPanel buttonsPanel = dialogPanel.getButtonsPanel();
    JButton restoreButton = getRestoreButton();
    buttonsPanel.remove(restoreButton);
    buttonsPanel.remove(dialogPanel.getRestoreStrut());
    restoreButton.setVisible(false);//from www. j  av  a  2s  .  c  om
    buttonsPanel.validate();
}

From source file:com.anrisoftware.prefdialog.simpledialog.SimpleDialog.java

private void removeApproveButton() {
    JPanel buttonsPanel = dialogPanel.getButtonsPanel();
    JButton approveButton = getApproveButton();
    buttonsPanel.remove(approveButton);
    buttonsPanel.remove(dialogPanel.getApproveStrut());
    approveButton.setVisible(false);//  www  .j  ava  2s  .co m
    buttonsPanel.validate();
}

From source file:com.anrisoftware.prefdialog.simpledialog.SimpleDialog.java

private void readdRestoreButton() {
    JButton cancelButton = getCancelButton();
    JButton restoreButton = getRestoreButton();
    JPanel buttonsPanel = dialogPanel.getButtonsPanel();
    restoreAction.setDialog(this);
    restoreAction.setTexts(texts);/*from  www .  ja v a2  s.co  m*/
    restoreButton.setVisible(true);
    restoreButton.setAction(restoreAction);
    buttonsPanel.remove(cancelButton);
    buttonsPanel.add(restoreButton);
    buttonsPanel.add(dialogPanel.getRestoreStrut());
    buttonsPanel.add(cancelButton);
    buttonsPanel.validate();
}

From source file:com.anrisoftware.prefdialog.simpledialog.SimpleDialog.java

private void readdApproveButton() {
    JButton cancelButton = getCancelButton();
    JButton approveButton = getApproveButton();
    JPanel buttonsPanel = dialogPanel.getButtonsPanel();
    approveAction.setDialog(this);
    approveAction.setTexts(texts);//from w  w  w . j a v  a2  s. co m
    approveButton.setVisible(true);
    approveButton.setAction(approveAction);
    buttonsPanel.remove(cancelButton);
    buttonsPanel.add(approveButton);
    buttonsPanel.add(dialogPanel.getApproveStrut());
    buttonsPanel.add(cancelButton);
    buttonsPanel.validate();
}

From source file:dbseer.gui.panel.DBSeerLiveMonitorPanel.java

@Override
public synchronized void actionPerformed(ActionEvent event) {
    for (int i = 0; i < transactionRenameButtons.size(); ++i) {
        if (event.getSource() == transactionRenameButtons.get(i)) {
            String newName = (String) JOptionPane.showInputDialog(this,
                    "Enter the new name for this transaction type", "New Dataset", JOptionPane.PLAIN_MESSAGE,
                    null, null, transactionNames.get(i));

            if (newName == null || newName.trim().isEmpty()) {
                return;
            } else {
                newName = newName.trim();
                transactionNames.set(i, newName);
                transactionLabels.get(i).setText(newName);

                DefaultTableModel model = (DefaultTableModel) monitorTable.getModel();
                model.setValueAt(String.format("Current TPS of '%s' transactions", transactionNames.get(i)),
                        2 + (i * ROW_PER_TX_TYPE), 0);
                model.setValueAt(/* w  w  w.  j av a2  s .  co m*/
                        String.format("Current average latency of '%s' transactions", transactionNames.get(i)),
                        2 + (i * ROW_PER_TX_TYPE) + 1, 0);

                //               TimeSeriesCollection collection = (TimeSeriesCollection) throughputChartPanel.getChart().getXYPlot().getDataset();
                throughputCollection.getSeries(i).setKey(newName);
                latencyCollection.getSeries(i).setKey(newName);

                //               if (DBSeerGUI.currentDataset != null)
                //               {
                //                  DBSeerGUI.currentDataset.setTransactionTypeName(i, newName);
                //               }

                for (DBSeerDataSet dataset : DBSeerGUI.liveDatasets) {
                    dataset.setTransactionTypeName(i, newName);
                }

                return;
            }
        }
    }

    for (int i = 0; i < transactionViewSampleButtons.size(); ++i) {
        if (event.getSource() == transactionViewSampleButtons.get(i)) {
            final int type = i;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    DBSeerShowTransactionExampleFrame sampleFrame = new DBSeerShowTransactionExampleFrame(type);
                    sampleFrame.pack();
                    sampleFrame.setLocationRelativeTo(DBSeerGUI.mainFrame);
                    sampleFrame.setVisible(true);
                }
            });
        }
    }

    for (int i = 0; i < transactionEnableDisableButtons.size(); ++i) {
        if (event.getSource() == transactionEnableDisableButtons.get(i)) {
            final XYItemRenderer throughputRenderer = throughputChartPanel.getChart().getXYPlot().getRenderer();
            final XYItemRenderer latencyRenderer = latencyChartPanel.getChart().getXYPlot().getRenderer();
            final int type = i;
            final JButton button = transactionEnableDisableButtons.get(i);
            final DBSeerDataSet dataset = DBSeerGUI.liveDataset;

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    if (button.getText() == "Disable") {
                        dataset.disableTransaction(type);
                        throughputRenderer.setSeriesVisible(type, false);
                        latencyRenderer.setSeriesVisible(type, false);
                        button.setText("Enable");
                    } else if (button.getText() == "Enable") {
                        dataset.enableTransaction(type);
                        throughputRenderer.setSeriesVisible(type, true);
                        latencyRenderer.setSeriesVisible(type, true);
                        button.setText("Disable");
                    }
                }
            });
        }
    }

    for (int i = 0; i < transactionDeleteButtons.size(); ++i) {
        if (event.getSource() == transactionDeleteButtons.get(i)) {
            synchronized (LiveMonitorInfo.LOCK) {
                try {
                    DBSeerGUI.middlewareSocket.removeTransactionType(i);
                } catch (IOException e) {
                    DBSeerExceptionHandler.handleException(e);
                }

                throughputCollection.removeSeries(i);
                latencyCollection.removeSeries(i);

                DefaultTableModel model = (DefaultTableModel) monitorTable.getModel();
                int newTxSize = transactionNames.size() - 1;
                for (int j = 0; j < transactionNames.size(); ++j) {
                    model.setValueAt(String.format("Current TPS of '%s' transactions", transactionNames.get(j)),
                            2 + (j * ROW_PER_TX_TYPE), 0);
                    model.setValueAt(String.format("Current average latency of '%s' transactions",
                            transactionNames.get(j)), 2 + (j * ROW_PER_TX_TYPE) + 1, 0);
                    model.setValueAt("", 2 + (j * ROW_PER_TX_TYPE), 1);
                    model.setValueAt("", 2 + (j * ROW_PER_TX_TYPE) + 1, 1);
                }
                model.setValueAt("", 2 + (newTxSize * ROW_PER_TX_TYPE), 0);
                model.setValueAt("", 2 + (newTxSize * ROW_PER_TX_TYPE), 1);
                model.setValueAt("", 2 + (newTxSize * ROW_PER_TX_TYPE) + 1, 0);
                model.setValueAt("", 2 + (newTxSize * ROW_PER_TX_TYPE) + 1, 1);

                final JPanel panel = transactionTypesPanel;
                final JLabel label = transactionLabels.remove(i);
                final JButton renameButton = transactionRenameButtons.remove(i);
                final JButton exampleButton = transactionViewSampleButtons.remove(i);
                final JButton deleteButton = transactionDeleteButtons.remove(i);
                transactionNames.remove(i);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        panel.remove(label);
                        panel.remove(renameButton);
                        panel.remove(exampleButton);
                        panel.remove(deleteButton);

                        panel.revalidate();
                        panel.repaint();
                    }
                });
            }
            break;
        }
    }

}

From source file:net.daboross.outputtablesclient.gui.OutputInterface.java

@Override
public void onUpdate(final RobotTable table, final String key, final String value, final UpdateAction action) {
    if (!application.getOutput().getNameTable().contains(table.getName())) {
        return;/*from   w  ww .  ja  v a  2 s .c om*/
    }
    if (action == UpdateAction.NEW) {
        //            if (key.equalsIgnoreCase(":RangeGUI")) {
        //                Output.oLog("Range: %s", value);
        //                try {
        //                    application.getCustomInterface().setTo(Double.parseDouble(value));
        //                } catch (NumberFormatException ex) {
        //                    Output.oLog("Invalid range '%s'", value);
        //                }
        //            }
        ensureTableExists(table.getName());
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(new LineBorder(Color.BLACK));
        tableKeyAndKeyToValuePanel.get(table.getName()).put(key, panel);

        JLabel keyLabel = new JLabel(key);
        keyLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
        panel.add(keyLabel, new GBC().fill(GridBagConstraints.VERTICAL).gridy(0));

        JSeparator separator = new JSeparator(JSeparator.VERTICAL);
        separator.setPreferredSize(new Dimension(2, 20));
        panel.add(separator, new GBC().fill(GridBagConstraints.VERTICAL).gridy(0));

        JLabel valueLabel = new JLabel(value);
        valueLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
        panel.add(valueLabel, new GBC().fill(GridBagConstraints.VERTICAL).gridy(0));
        tableKeyAndKeyToValueLabel.get(table.getName()).put(key, valueLabel);

        JPanel parentPanel = tableKeyToTablePanel.get(table.getName());
        parentPanel.add(panel);
        parentPanel.revalidate();
    } else if (action == UpdateAction.UPDATE) {
        //            if (key.equalsIgnoreCase(":RangeGUI")) {
        //                Output.oLog("Range: %s", value);
        //                try {
        //                    application.getCustomInterface().setTo(Double.parseDouble(value));
        //                } catch (NumberFormatException ex) {
        //                    Output.oLog("Invalid range '%s'", value);
        //                }
        //            }
        ensureTableExists(table.getName());
        JLabel valueLabel = tableKeyAndKeyToValueLabel.get(table.getName()).get(key);
        valueLabel.setText(value);
    } else if (action == UpdateAction.DELETE) {
        if (tableKeyToTablePanel.get(table.getName()) == null) {
            return;
        }
        JPanel parentPanel = tableKeyToTablePanel.get(table.getName());
        JPanel valuePanel = tableKeyAndKeyToValuePanel.get(table.getName()).remove(key);
        parentPanel.remove(valuePanel);
        parentPanel.revalidate();
    }
}