Changing JDialog from MODELESS to APPLICATION_MODAL opens JDialog twice [closed] - java

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a pretty strange behavior which I can only conclude must be a Java bug somewhere.
I changed one line in my constructor
super(parent, "Production Plan Export", ModalityType.MODELESS);
to
super(parent, "Production Plan Export", ModalityType.APPLICATION_MODAL);
and suddenly when I click on the button to open my JDialog, it opens it twice, the first time, its not responsive at all. I need to click the X button to close the window, and then when I do, then the same JDialog appears, and suddenly all my buttons opens.
Has anyone seen this behavior before?
I am using Java 1.6.0_33
Edit
What is very very strange, is when I try to debug it in eclipse and setting a break point at my constuctor, and I go to next line, then it suddenly jumps to my variable in the class and starts to go through my variables instead of the next line in the constructor.
I have tried to restart my computer and eclipse, but that didn't work.
I will see if I can create a small test case
Edit2:
Ok, so I have created a small application that can reproduce it for me.
Please note that I have tried to remove the part of the code that is not relevant, so there is alot of code that isn't valid for this test case.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class ProductionPlanExportDialog extends JDialog {
private JProgressBar progressbar;
private JLabel message;
private JButton exportButton;
private JButton helpButton;
private int state = JOptionPane.CANCEL_OPTION;
private final Action closeAction = new CloseAction();
private final Action enableExport = new EnableExport();
private boolean updating = false;
private JButton closeButton;
public ProductionPlanExportDialog(Window parent) {
super(parent, "Test", ModalityType.APPLICATION_MODAL); //The JDialog is not centered, and the close button doesn't work
// super(parent, "Test", ModalityType.MODELESS); //The close button works and the jdialog is centered
initGUI();
bindModel();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
close();
}
});
}
public static void main(String args[]) {
new ProductionPlanExportDialog(null);
}
public void initGUI() {
JTabbedPane mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel yAxisPanel = new JPanel();
yAxisPanel.setLayout(new BoxLayout(yAxisPanel, BoxLayout.Y_AXIS));
JPanel progressPanel = new JPanel(new FlowLayout());
progressPanel.add(getProgressbar());
yAxisPanel.add(progressPanel);
yAxisPanel.add(getMessage());
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(getExportButton());
closeButton = new JButton("Close");
buttonPanel.add(closeButton);
buttonPanel.add(getHelpButton());
yAxisPanel.add(buttonPanel);
setSize(937, 605);
getContentPane().add(yAxisPanel);
setVisible(true);
setLocationRelativeTo(null);
}
private void bindModel() {
closeButton.setAction(closeAction);
}
public int getExitStatus() {
return state;
}
private JProgressBar getProgressbar() {
if (progressbar == null) {
progressbar = new JProgressBar();
progressbar.setPreferredSize(new Dimension(1000, 22));
progressbar.setName("progressbar");
progressbar.setStringPainted(true);
progressbar.setVisible(false);
}
return progressbar;
}
private JLabel getMessage() {
if (message == null) {
message = new JLabel();
message.setName("message");
}
return message;
}
private void setUIState() {
updating = true;
assert SwingUtilities.isEventDispatchThread();
try {
closeButton.setEnabled(false);
} finally {
updating = false;
}
}
private void setTextFieldValue(JFormattedTextField textField, long value) {
if(value == Long.MAX_VALUE || value == -Long.MAX_VALUE) {
textField.setText("");
} else {
textField.setValue(value);
}
}
public JButton getExportButton() {
if (exportButton == null) {
exportButton = new JButton();
exportButton.setToolTipText("Preview production plan");
exportButton.setName("exportButton");
exportButton.setText("Preview");
exportButton.setBounds(305, 640, 72, 21);
exportButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
}
return exportButton;
}
private JButton getHelpButton() {
if (helpButton == null) {
helpButton = new JButton();
helpButton.setBounds(470, 640, 72, 21);
helpButton.setName("helpButton");
helpButton.setText("Help");
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String helpText = "This dialog is used to preview and export a production plan.<br/>" +
"<h1>Preview</h1>"
+ "After pressing 'Export', the production plan will be generated. This can take a few minutes.";
JEditorPane helpEditorPane = new JEditorPane("text/html", helpText);
helpEditorPane.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
helpEditorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(helpEditorPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
helpEditorPane.setCaretPosition(0);
scrollPane.setPreferredSize(new Dimension(640,445));
JOptionPane.showMessageDialog(helpButton, scrollPane, "Production Plan Export Dialog Help", JOptionPane.INFORMATION_MESSAGE);
}
});
}
return helpButton;
}
private final class EnableExport extends AbstractAction {
#Override
public void actionPerformed(final ActionEvent e) {
if(isAllCheckboxSelected()) {
getExportButton().setText("Export");
getExportButton().setToolTipText("Export production plan");
} else {
getExportButton().setText("Preview");
getExportButton().setToolTipText("Preview production plan");
}
}
}
boolean enableExport() {
return isAllCheckboxSelected();
}
boolean isAllCheckboxSelected() {
return false;
}
private final class CloseAction extends AbstractAction {
public CloseAction() {
super("Close");
}
#Override
public void actionPerformed(final ActionEvent e) {
close();
}
}
private void close() {
state = JOptionPane.OK_OPTION;
setVisible(false);
dispose();
}
}

not able to ...., can you please (descriptions about Bug) to test
import java.awt.Cursor;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(new Dimension(500, 500));
final JDialog dialog = new JDialog(frame, "Production Plan Export",
ModalityType.MODELESS);
dialog.setSize(300, 300);
final JDialog dialog1 = new JDialog(dialog, "Production Plan Export",
ModalityType.APPLICATION_MODAL);
dialog1.setSize(200, 200);
frame.add(new JButton(new AbstractAction("Dialog") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Runnable doRun = new Runnable() {
public void run() {
dialog.setVisible(true);
dialog1.setVisible(true);
}
};
SwingUtilities.invokeLater(doRun);
}
}));
frame.setVisible(true);
}
}

I found the problem.
It was because I had setVisible(true) twice. Once in my initGUI() method, and once where I initialize my JDialog.
Also thanks to MKorbel, I moved the setAction call in my initGUI() which made the button work when having APPLICATION_MODAL

Related

Swing window not opening

I am creating a NotePad app in Java Swing but when I am trying to open a popup to set a title, it is not showing up.
The class that calls the popup:
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class NewFile implements ActionListener{
public static String title;
public void actionPerformed(ActionEvent e){
PopupFileName popup = new PopupFileName();
/*try{
Thread.sleep(30000);
}catch (InterruptedException o){
o.printStackTrace();
}*/
JTextArea titl = popup.title;
title = titl.getText();
try{
File writer = new File(title+".txt");
if(writer.createNewFile()){
System.out.println("file created");
}else{
System.out.println("file exists");
}
}catch (IOException i) {
System.out.println("An error occurred.");
i.printStackTrace();
}
}
}
The popup class that is supposed to open:
import javax.swing.*;
public class PopupFileName{
static JFrame popup = new JFrame("File Title");
static JLabel titlel = new JLabel("Title:");
static public JTextArea title = new JTextArea();
public static void main(String[] args){
popup.setSize(200,300);
popup.setVisible(true);
popup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
popup.add(titlel);
popup.add(title);
}
}
Is there any way I can make it visible and make it able to get the text before it is created?
Start by taking a look at:
Creating a GUI With Swing
How to Write an Action Listener
How to Use Scroll Panes
How to Use Buttons, Check Boxes, and Radio Buttons
How to Make Dialogs
You're running in an event driven environment, this means, something happens and then you respond to it.
The problem with your ActionListener is, it's trying to present a window and then, immediately, trying to get some result from it. The problem is, the window probably isn't even present on the screen yet.
What you need is some way to "stop" the code execution until after the user responds. This is where a modal dialog comes in.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Test");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String title = PopupFileName.getTitle(TestPane.this);
System.out.println(title);
}
});
add(btn);
}
}
public static class PopupFileName extends JPanel {
private JLabel titlel = new JLabel("Title:");
private JTextArea title = new JTextArea(20, 40);
public PopupFileName() {
setLayout(new BorderLayout());
add(titlel, BorderLayout.NORTH);
add(new JScrollPane(title));
}
public String getTitle() {
return title.getText();
}
public static String getTitle(Component parent) {
PopupFileName popupFileName = new PopupFileName();
int response = JOptionPane.showConfirmDialog(parent, popupFileName, "Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
switch (response) {
case JOptionPane.OK_OPTION:
return popupFileName.getTitle();
default: return null;
}
}
}
}

how to set semi-transparent jframe when "submit" button is clicked?

loadingLab=new JLabel("The name is being saved..");
loadPanel.add(loadingLab);
submitBttn=new JButton("Submit");
submitBttn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Submit Button Clicked!!");
try {
//something is wrong in here as it throws an exception
//what is wrong?
frame.setUndecorated(false);
frame.setOpacity(0.55f);
//when above both lines are commented, the code works fine
//but doesnt have transparency
frame.add(loadPanel,BorderLayout.SOUTH);
frame.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
I am trying to display transparent JFrame when "submit" button is clicked which displays panel with a JLabel...
I have tried using setOpacity(0.55f), but it throws exception.. what am i doing wrong?
Unfortunately I think there's no way to keep the system window decoration, you will probably have to go with the default one. Since I'm not 100% sure if you want to toggle the opacity of the whole frame or just the frame's background, I've included both functions in my example. (mKorbels answer help you more if you don't want to have a decoration)
Code:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class TransparentExample extends JFrame {
public TransparentExample() {
super("TransparentExample");
Color defaultBackground = getBackground();
float defaultOpacity = getOpacity();
JToggleButton button1 = new JToggleButton("Toggle background transparency");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (button1.isSelected()) {
setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(),
defaultBackground.getBlue(), 150));
} else {
setBackground(defaultBackground);
}
}
});
JToggleButton button2 = new JToggleButton("Toggle opacity of whole frame");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dispose();
if (button2.isSelected()) {
setOpacity(0.55f);
} else {
setOpacity(defaultOpacity);
}
setVisible(true);
}
});
getContentPane().setLayout(new FlowLayout());
getContentPane().add(button1);
getContentPane().add(button2);
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
TransparentExample frame = new TransparentExample();
frame.setVisible(true);
}
});
}
}
Picture of frame with no togglebutton selected:
Picture of frame with the first togglebutton selected:
Picture of frame with the second togglebutton selected:
#Programmer007 wrote - the exception is "
java.awt.IllegalComponentStateException: The frame is displayable."
please where I can't see any, for more info about the possible exceptions to read,
as mentioned no idea, everything is about your effort, transformed to the SSCCE / MCVE, short, runnable, compilable
.
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class GenericForm extends JDialog {
private static final long serialVersionUID = 1L;
private Timer timer;
private JDialog dialog = new JDialog();
private int count = 0;
public GenericForm() {
dialog.setSize(400, 300);
dialog.setUndecorated(true);
dialog.setOpacity(0.5f);
dialog.setName("Toggling with opacity");
dialog.getContentPane().setBackground(Color.RED);
dialog.setLocation(150, 150);
dialog.setVisible(true);
timer = new javax.swing.Timer(1500, updateCol());
timer.setRepeats(true);
timer.start();
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
boolean bol = dialog.getOpacity() < 0.55f;
count += 1;
if (count < 10) {
if (bol) {
dialog.setOpacity(1.0f);
dialog.getContentPane().setBackground(Color.WHITE);
} else {
dialog.setOpacity(0.5f);
dialog.getContentPane().setBackground(Color.RED);
}
} else {
System.exit(0);
}
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GenericForm();
}
});
}
}

How do I make my SwingWorker example work properly?

I've made my own SwingWorker example to get familiar with how it works.
What I'm wanting to do is the following:
When the button is clicked I want a progress bar appear until the task is done I want to simply remove the progress bar and add a string to the dialog.
When the button is clicked, the progress bar comes up but never goes away. (never removes the progress bar after 10 seconds and never places the label up)
Here is an SSCCE:
package swingtesting;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class SwingTesting {
/**
* Creates a frame that will hold a simple button to make use of SwingWorker
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GuiWorker().execute();
}
});
button.setText("Test Me");
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
}
class GuiWorker extends SwingWorker<Integer, Integer> {
/*
* This should just create a frame that will hold a progress bar until the
* work is done. Once done, it should remove the progress bar from the dialog
* and add a label saying the task complete.
*/
private JFrame frame = new JFrame();
private JDialog dialog = new JDialog(frame, "Swingworker test", true);
private JProgressBar progressBar = new JProgressBar();
public GuiWorker() {
progressBar.setString("Waiting on time");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
dialog.getContentPane().add(progressBar);
dialog.pack();
dialog.setVisible(true);
}
#Override
protected Integer doInBackground() throws Exception {
Thread.sleep(10000);
return 0;
}
#Override
protected void done() {
JLabel label = new JLabel("Task Complete");
dialog.getContentPane().remove(progressBar);
dialog.getContentPane().add(label);
}
}
Here an updated version of your code which works
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class SwingTesting {
public static void main(String[] args) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
JButton button = new JButton();
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new GuiWorker().execute();
}
});
button.setText("Test Me");
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
} );
}
}
class GuiWorker extends SwingWorker<Integer, Integer> {
/*
* This should just create a frame that will hold a progress bar until the
* work is done. Once done, it should remove the progress bar from the dialog
* and add a label saying the task complete.
*/
private JFrame frame = new JFrame();
private JDialog dialog = new JDialog(frame, "Swingworker test", true);
private JProgressBar progressBar = new JProgressBar();
public GuiWorker() {
progressBar.setString("Waiting on time");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
dialog.getContentPane().add(progressBar);
dialog.pack();
dialog.setModal( false );
dialog.setVisible(true);
}
#Override
protected Integer doInBackground() throws Exception {
System.out.println( "GuiWorker.doInBackground" );
Thread.sleep(1000);
return 0;
}
#Override
protected void done() {
System.out.println("done");
JLabel label = new JLabel("Task Complete");
dialog.getContentPane().remove(progressBar);
dialog.getContentPane().add(label);
dialog.getContentPane().validate();
}
}
Key point is that setting a model dialog visible blocks until the dialog is disposed. So making it non-modal fixed it + the validate call on the content pane when you switch components. I also adjusted your main method to run on the EDT, and added some System.out calls. If you remove the setModal( false ) call you will see those statements are not printed until you close the dialog
There's no need to make the dialog non-modal. Simply display the dialog after starting the SwingWorker. This can be done either from the calling class, the one executing the SwingWorker, by first calling execute, and then showing the dialog, or it can be done from the SwingWorker, but if from the latter, you'll have to make your own pseudo-execute method that calls super's execute, and then shows the dialog. Note that you can't override execute() itself since it's final.
For example...
import java.awt.CardLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
#SuppressWarnings("serial")
public class SwingTesting2 {
private static void createAndShowGui() {
final JFrame frame = new JFrame("SwingTesting2");
final JDialog dialog = new JDialog(frame, "Dialog",
ModalityType.APPLICATION_MODAL);
final DialogPanel dialogPanel = new DialogPanel();
dialog.getContentPane().add(dialogPanel.getMainPanel());
dialog.pack();
dialog.setLocationRelativeTo(frame);
JButton button = new JButton(new AbstractAction("Test Me") {
#Override
public void actionPerformed(ActionEvent actEvt) {
final GuiWorker2 guiWorker = new GuiWorker2();
guiWorker.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (pcEvt.getPropertyName().equals("state")) {
if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
try {
dialogPanel.done(guiWorker.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} else if (pcEvt.getPropertyName().equals("progress")) {
dialogPanel.setProgress((Integer)pcEvt.getNewValue());
}
}
});
guiWorker.execute();
dialogPanel.start();
dialog.setVisible(true);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class GuiWorker2 extends SwingWorker<Integer, Integer> {
private static final int MAX_COUNT = 20;
private static final long SLEEP_TIME = 100;
private int count = 0;
#Override
protected Integer doInBackground() throws Exception {
while (count < MAX_COUNT) {
Thread.sleep(SLEEP_TIME);
count++;
setProgress((100 * count) / MAX_COUNT);
}
return count;
}
}
#SuppressWarnings("serial")
class DialogPanel {
public static final String PROGRESS_BAR = "Progress Bar";
public static final String DONE = "Done";
private static final int TIMER_DELAY = 2000;
private CardLayout cardLayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardLayout);
private JLabel doneLabel = new JLabel("Done", JLabel.CENTER);
private JProgressBar progressBar = new JProgressBar();
public DialogPanel() {
progressBar.setString("Waiting on time");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(false);
mainPanel.add(progressBar, PROGRESS_BAR);
mainPanel.add(doneLabel, DONE);
}
public void setProgress(Integer newValue) {
progressBar.setValue(newValue);
}
public void start() {
cardLayout.show(mainPanel, PROGRESS_BAR);
progressBar.setValue(0);
}
public void done(int countValue) {
doneLabel.setText(DONE + ". Count: " + countValue);
cardLayout.show(mainPanel, DONE);
new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.dispose();
}
}){{setRepeats(false);}}.start();
}
public JPanel getMainPanel() {
return mainPanel;
}
}

Center JDialog over JPanel on JTabbedPane

I have tried all of the suggestions I found here and on other sites.
I can't seem to get this JDialog to be centered over the panel on the JTabbedPane.
Please note, I must have the close button disabled, so I can not use the standard JOptionPane.showDialogXYZ() methods.
Any ideas?
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class CenterDialog extends JFrame
{
public CenterDialog()
{
setResizable(false);
setName(getClass().getSimpleName());
setTitle("My Frame");
setSize(300, 300);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
// Add the panel
tabbedPane.addTab("Button panel", new MyButtonPanel());
add(tabbedPane, BorderLayout.CENTER);
getContentPane().add(tabbedPane);
}
private class MyButtonPanel extends JPanel
{
public MyButtonPanel()
{
JButton btnShowDialog = new JButton("Show Dialog");
btnShowDialog.addActionListener(new BtnShowDialogActionListener());
add(btnShowDialog);
}
private class BtnShowDialogActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// TODO: Figure out how to center this dialog box
final String YES = "Yup";
final String NO = "Nope";
final Object[] options = { YES, NO };
final JOptionPane optionPane = new JOptionPane("Is this centered.", JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, options, NO);
Frame f = JOptionPane.getFrameForComponent(((JButton) e.getSource()).getParent());
final JDialog dialog = new JDialog(f, "Question", ModalityType.APPLICATION_MODAL);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.out.println("Ignoring close button");
}
});
optionPane.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (dialog.isVisible() && (e.getSource() == optionPane))
{
if (prop.equals(JOptionPane.VALUE_PROPERTY))
{
dialog.setVisible(false);
}
}
}
});
dialog.pack();
dialog.setVisible(true);
}
}
}
private static void createAndShowGUI()
{
// Create and set up the window.
CenterDialog frame = new CenterDialog();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
});
}
}
}
The method which centers a dialog relative to a given component (no manual calculation needed, handles component-to-screen coordinate mapping internally):
dialog.setLocationRelativeTo(someComponent);
Choose the component, depending on what exactly you want to achieve:
// center relative to the button
dialog.setLocationRelativeTo((Component) e.getSource());
// center relative to button's parent
dialog.setLocationRelativeTo(((Component) e.getSource()).getParent());
// center relative to the tabbedPane
JTabbedPane tabbed = // walk the parent chain until you reach it
dialog.setLocationRelativeTo(tabbed);
I got slightly closer to what you're after by setting tabbedPane as global and then dialog.setLocationRelativeTo(tabbedPane);
Edit: a more elaborate, and probably visually accurate, solution is to calculate the x, y coordinates of your JDialog, something like this:
int xDiff = (tabbedPane.getWidth() - dialog.getWidth()) / 2;
int x = tabbedPane.getX() + xDiff;
int yDiff = (tabbedPane.getHeight() - dialog.getHeight()) / 2;
int y = tabbedPane.getY() + yDiff;
dialog.setLocation(x, y);
If I'm honest, I didn't get it working perfectly, but there's my theory!

Should open new window while clicking a button?

I know that it is very simple question, but I can't find a solution.
I have a main swing dialog and other swing dialog. Main dialog has a button.
How can I make that after clicking a button the other dialog opens?
EDIT:
When I try this:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
NewJDialog okno = new NewJDialog();
okno.setVisible(true);
}
I get an error:
Cannot find symbol NewJDialog
The second window is named NewJDialog...
You'll surely want to look at How to Make Dialogs and review the JDialog API. Here's a short example to get started. You might compare it with what you're doing now.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class DialogTest extends JDialog implements ActionListener {
private static final String TITLE = "Season Test";
private enum Season {
WINTER("Winter"), SPRING("Spring"), SUMMER("Summer"), FALL("Fall");
private JRadioButton button;
private Season(String title) {
this.button = new JRadioButton(title);
}
}
private DialogTest(JFrame frame, String title) {
super(frame, title);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(0, 1, 8, 8));
ButtonGroup group = new ButtonGroup();
for (Season s : Season.values()) {
group.add(s.button);
radioPanel.add(s.button);
s.button.addActionListener(this);
}
Season.SPRING.button.setSelected(true);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.add(radioPanel);
this.pack();
this.setLocationRelativeTo(frame);
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
JRadioButton b = (JRadioButton) e.getSource();
JOptionPane.showMessageDialog(null, "You chose: " + b.getText());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new DialogTest(null, TITLE);
}
});
}
}

Categories