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
- Nest JPanels, each using its own layout manager
- Call
setLocationRelativeTo(parent)
after callingpack()
. 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
getPreferredSize
forJLabel
- basically you have to gettextLength
fromJLabel
in pixels, there are 3 correct ways, but I love:SwingUtilities.computeStringWidth(FontMetrics fm, String str)
Now you are able to
setPreferredSize
forJLabel
correctly (please by defalut is there needed to add +5 - +10 to theInteger
that returnsSwingUtilities.computeStringWidth
)- Call
pack();
on the top level container (theJDialog
).
Related Topics
How to Convert Hashmap to JSON Object in Java
Java Maximum Memory on Windows Xp
Java Event-Dispatching Thread Explanation
Differences Between Oracle Jdk and Openjdk
Implementing Singleton with an Enum (In Java)
How to Construct a Relative Path in Java from Two Absolute Paths (Or Urls)
How Does a Arraylist's Contains() Method Evaluate Objects
Why Is Creating a Thread Said to Be Expensive
How to Count the Number of Occurrences of an Element in a List
Is There a Performance Difference Between a for Loop and a For-Each Loop
Difference Between File.Separator and Slash in Paths
How to Make the Division of 2 Ints Produce a Float Instead of Another Int
Differencebetween Instanceof and Class.Isassignablefrom(...)