Dynamically Add Components to a Jdialog

Dynamically Add Components to a JDialog

When dynamically adding/removing components from a container, it's necessary to invoke revalidate()/validate() and repaint() afterward. The former will force the container to layout its components again and the latter will remove any visual "artifacts".

Dynamically add components to a JDialog on button click

If you add it to the main panel it will work, you were adding it to the content pane of the frame which it seems it does not show up anywhere.

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {

JPanel _panel = new JPanel();
_panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS));
_panel.add(Box.createHorizontalGlue());
_panel.add(createLabel("Added!"));
mainPanel.add(_panel);

mainPanel.validate();
mainPanel.repaint();
owner.pack();
}
})

How to add components to JDialog

1) first create a Jpanel

JPanel pan=new JPanel();
pan.setLayout(new FlowLayout());

2) add the components to that JPanel

pan.add(new JLabel("label"));
pan.add(new JButton("button"));

3) create JDialog

JDialog jd=new JDialog();

4) add the JPanel to JDialog

jd.add(pan);

Add components to JDialog

I think it might be because you say "public void ColourDialog()" this is an invalid constructor. Try getting rid of the "void" and try again.

NetBeans & Swing - dynamically add JPanel to JDialog

basicall there are two ways

1) add a new JComponent by holding JDialog size (in pixels) on the screen, all JCompoenets
or part of them could be shrinked

2) resize JDialog by calling pack(), then JDialog will be resized

both my a.m. rulles works by using Standard LayoutManagers (excepting AbsoluteLayout)

JDialog - line break in between components

  1. Nest JPanels, each using its own layout manager
  2. Call setLocationRelativeTo(parent) after calling pack(). You need to position the window after it has been rendered.

For example:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleGuiPanel extends JPanel {
private static final String TITLE = "This is my Dialog Title";

public SimpleGuiPanel() {
JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 16f));

JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
buttonPanel.add(new JButton("Button 1"));
buttonPanel.add(new JButton("Button 2"));

setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(titleLabel, BorderLayout.PAGE_START);
add(buttonPanel, BorderLayout.CENTER);
}

private static void createAndShowGui() {
JPanel mainFramePanel = new JPanel();
mainFramePanel.setPreferredSize(new Dimension(500, 400));
final JFrame mainFrame = new JFrame("Main Frame");
mainFrame.add(mainFramePanel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SimpleGuiPanel simpleGuiPanel = new SimpleGuiPanel();
final JDialog myDialog = new JDialog(mainFrame, "Dialog", ModalityType.APPLICATION_MODAL);
myDialog.getContentPane().add(simpleGuiPanel);
myDialog.pack();

mainFramePanel.add(new JButton(new AbstractAction("Show Dialog") {

@Override
public void actionPerformed(ActionEvent e) {
myDialog.setLocationRelativeTo(mainFrame);
myDialog.setVisible(true);
}
}));

mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

JDialog components won't show up

You call setVisible(true) before createView(). But as it is a JDialog which is modal, calling setVisible() actually blocks until the dialog is dismissed. So, createView() will only be called once the dialog got closed.

Just change the order of the two method calls, first createView() and next setVisible(true), and it should work.

Dynamically change the width of JDialog

  1. getPreferredSize for JLabel - basically you have to get textLength from JLabel in pixels, there are 3 correct ways, but I love:

    SwingUtilities.computeStringWidth(FontMetrics fm, String str)
  2. Now you are able to setPreferredSize for JLabel correctly (please by defalut is there needed to add +5 - +10 to the Integer that returns SwingUtilities.computeStringWidth)

  3. Call pack(); on the top level container (the JDialog).


Related Topics



Leave a reply



Submit