Swing/AWT/SWT :: Program Doesn't Close Through JDialog Box
Mar 30, 2014
I have a practice exercise here wherein I will add a JOptionPane to a program. When the close button is clicked, the JOptionPane will appear asking the user to exit the program. If the user clicks "Yes," it'll, of course, close the entire program and not just the JOptionPane, but if the user clicks "No," it will return to the program. Please refer to my code below:
try
{
output = new DataOutputStream(new FileOutputStream("ProjSixExe4.dat"));
}
catch(IOException ex) {
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING))
}
final JOptionPane optionpane = new JOptionPane("Are you sure you want to quit
this program?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
The exercise said it must be placed before the EXIT_ON_CLOSE portion.
I use the right term, release resources because after clicking the close button, the DOS prompts becomes unresponsive. So I have to close the DOS prompt, then reopen it, then type in the path again for the Java folders. Can you check where did I go wrong with my code? Is my placement of the WindowListener and WindowAdapter incorrect? Please refer to my code below:
I have other programs where the placement of the WindowListener and WindowAdapter are as exactly like this (within the class constructor block) where they close correctly and some that behave like this, that renders the DOS prompt unresponsive.
One of the four JDialog class would look something like this without the comments.
package client; import javax.swing.JDialog; @SuppressWarnings("serial") public class AddCustomerDialog extends JDialog implements Dialog{ public AddCustomerDialog () { //Some stuff goes here to set the settings for JDialog instance
Lets say the JDialog opens 5x5. The user is allowed to modify its size to be any size larger than 5x5 but is not allowed to make it smaller. Is there such a property or will this have to be regulated via code?
With the MigLayout for Swing, I'd like to see the JDialog looking like the standard windows Dialog,where to have the OK and cancel buttons at the lower right corner.
Following is the code on the click of a button, id like to know how do i close the current jframe on which the jButton4 is currently placed. I know how to send it to the next Jframe i.e JobCard. But need to close the current one. I tried using this.setVisible(true); But it does not work.
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); JobCard jobCard = new JobCard(); jobCard.setVisible(true); }
I didn't know about right method for correct close operation for JFrame component or kill the object. I found out few, which of them you are using usually???
I have added a windowListener to my JFrame but for some reason it is not being called when I click X to close the window. I need to send a message to a server to notify it that the client window is closed. Some code snippets below (some lines excluded for brevity):
public class ChatGUI extends javax.swing.JFrame { .... setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); // I have all the window constants here but has no effect other than closing the window without calling the listener... addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } }); .... private void formWindowClosed(java.awt.event.WindowEvent evt) {
I am making a java swing program. I have a main window that opens a JForm window when the user clicks a menu option. Then I have a button on the JForm that closes the window on click. However, I would also somehow like to make the JForm return values to the main window (according to the states of the selector components) when it is closed. How would I go about this? Currently my jForm is a seperate class called "NewGame", and inside the menu item mouse clicked method, I have the following code:
//open new game jform window NewGame newGameWindow = new NewGame(); newGameWindow.setVisible(true); newGameWindow.setLocationRelativeTo(null); //center window
Is there something I can add here to get the object's values upon the window's close? Or is there a way I can add that into the button clicked method on the actual jForm?
I am having an issue where the code does something very strange when run. Everything seems to be ok in the code but I don't know what is wrong as no error is given.
Here is the code:
import java.util.Scanner; public class Pizza { private static int pizzaSmall = 10; private static int pizzaMedium = 12; private static int pizzaLarge = 14; private static int costPerTopping = 2; /**@return The total cost of the pizza when given the pizza size and the toppings.*/
[Code] ....
And here is the console output:
Please enter the number of Cheese toppings that you would like: 1 Please enter the number of Pepperoni toppings that you would like: 1 Please enter the number of Ham toppings that you would like: 1 Please enter Small for a Small Pizza, Medium for a Meddium Pizza, and Large for a Large Pizza: The number of Cheese toppings is 1. The number of Pepperoni toppings is 1. The number of Ham toppings is 1.
I wish to display a progress bar during a lengthy file translation process. When I created a ProgressBar class to do this and tested it with a short test program, it seemed to work fine. But when I add my ProgressBar class to a package containing various other classes, put that package in a jar file, and then execute the class using the progress bar from a menu in my main program, the frame of the progress bar shows up with the appropriate caption but the JLabel, JProgressBar, and JButton are not displayed. I have gone over my code numerous times and checked that the ProgressBar class constructor is getting the proper arguments.
// package gov.nasa.jpl.ephemeris; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Progress bar with message text above the bar and a Cancel button below it */ public class ProgressBar extends JFrame { private boolean _cancelled = false; private JProgressBar _progressBar = null;
[Code] ....
I have also been wondering whether I need a separate instance of the border for each panel, or whether I can just create one instance and use it for all three panels.
I have a JList<Object> in my code (it actually takes JLabels) called imageList, and I've given it a custom CellRenderer using the following code:
cellRenderer = new DefaultListCellRenderer() { private static final long serialVersionUID = 1L; @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
[Code] .....
However, when I click anywhere on the JList, nothing gets printed to the console, which suggests that the mousePressed() method isn't being invoked. I think it's a problem with the CellRenderer, because I have similar code elsewhere that works, the only difference being I don't use a custom CellRenderer.
I have a JPanel that's using a simple GridBagLayout.
JPanel panel = new JPanel(); GridBagLayout qPanelLayout = new GridBagLayout(); qPanelLayout.columnWidths = new int[] { 0 }; qPanelLayout.rowHeights = new int[] { 0 }; qPanelLayout.columnWeights = new double[] { Double.MIN_VALUE }; qPanelLayout.rowWeights = new double[] { 0.0 }; panel.setLayout(qPanelLayout); componentCount = 0;
Based on user input I am adding sub-panels to the panel. These sub-panels may vary in height. They take up the full width of the Panel.
public void add(Component comp) { GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(2,2,2,2); gbc.gridx = 0; gbc.gridy = componentCount++; panel.add(comp, gbc_questionPane1); }
The odd behaviour I'm seeing is that if the height of the new sub-panel I'm adding is less than the height of the largest sub-panel currently displayed then the main panel does not repaint. If its height is equal or greater then the Panel does repaint, and any sub-panels that were added but not visible before are now painted. What have I missed?
I'm working on a simple text editor, and I'm currently saving the contents of my JTextPane in a file using an HTMLEditorKit (text is a JTextPane):
private void save() throws IOException { int returnVal = fc.showSaveDialog(window); if (returnVal == JFileChooser.APPROVE_OPTION) { StyledDocument doc = (StyledDocument)text.getDocument(); HTMLEditorKit kit = new HTMLEditorKit(); BufferedOutputStream out;
[Code] ....
The problem I'm having is that after opening a file that I saved, it does not display (if I disable text/html, it displays the entire html code, but when I re-enable it, nothing displays at all.) Am I loading it wrong, or am I setting the JTextPane's text incorrectly? Or is it, perhaps, another error that I didn't catch?
We are asked to create a JDialog showing our database. I am able to show the database but i can't seem to position the heading panel, table and back button. My back button is also not showing up. Here is part of my code...
I have a program that using one JFrame which opens one of two JDialog windows depending on which button is pressed.
How do I assign listeners to the buttons and fields on the JDialog window? I added listeners on the View end, but how do I process them? I tried adding the '..implements ActionListener" class in the main Controller but it does not recognize/hear anything.