"Do not show again" checkbox to hide a JFrame - java

I've written a little program that generates a "Welcome Email" for the Helpdesk Team in my office, that is sent out to new starters. I've recently made some changes, and created a changelog JFrame that pops-up when the program is run. I've included a "Don't show again" checkbox, but can't figure out how to stop this JFrame from popping up after the checkbox is ticked.
Here's the code:
Main class calls this to bring up the changelog JFrame:
private ChangeLog aFrame;
aFrame = new ChangeLog();
aFrame.setVisible(aFrame.logCheckBox);
This is the code from ChangeLog class:
protected boolean logCheckBox;
private final ChangeLogButtonEvent changeLogEvent;
public ChangeLog() {
initComponents();
setLocationRelativeTo(this);
logCheckBox = true;
changeLogEvent = new ChangeLogButtonEvent();
jCheckBox1.addItemListener(changeLogEvent);
}
private final class ChangeLogButtonEvent implements ItemListener{
#Override
public void itemStateChanged (ItemEvent e){
if(jCheckBox1.isSelected())
{
logCheckBox = false;
}
}
}
I know that the problem is with the fact that logCheckBox is being set to true each time the program is run, but I'm not sure how to do it otherwise.
Thanks in advance.
Tom.

Related

Calling a JFrame method from another JFrame on Button Click

I searched on stack overflow for the similar answers for my question, but neither of them helped me.
So my problem is the following:
I have a main JFrame called Main_Window, on which I have a JTable and a JButton. After clicking the Button another JFrame (Update_Window) opens from Which I can update the table. The Update_Window JFrame has two TextFields and a SUBMITButton.
Briefly, I want to update my JTable in the Main_Window from the Update_Window JFrame. After I type something in the TextFields and Submit with the Button, the data should appear in the Main_Window's JTable, but it is not working.
This is my Main_Window JFrame:
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
Update_Window newWindow = new Update_Window();
newWindow.setVisible(true);
newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void putDataIntoTable(Integer data, int row, int col) {
jTable1.setValueAt(data,row,col);
}
This is my Update_Window JFrame:
private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {
quantity = Integer.parseInt(quantityTextField.getText());
price = Integer.parseInt(priceTextField.getText());
Main_Window mw = new Main_Window();
mw.putDataIntoTable(price,3,2);
}
I think my problem is here Main_Window mw = new Main_Window();, because this creates a new Instance and it doesn't add the data to the correct window, or something like that.
Yes, you are right. The line Main_Window mw = new Main_Window(); is definitely wrong.
Better solution is:
public class UpdateWindow extends JFrame {
private final MainWindow mainWindow;
public UpdateWindow(MainWindow mainWin) {
mainWindow = mainWin;
}
private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {
quantity = Integer.parseInt(quantityTextField.getText());
price = Integer.parseInt(priceTextField.getText());
mainWindow.putDataIntoTable(price,3,2);
}
}
Also you need to correct the call of constructor for UpdateWindow
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
UpdateWindow newWindow = new UpdateWindow(this);
newWindow.setVisible(true);
newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
Please note: I've corrected your class names as it proposed by Java naming convention. Main_Window -> MainWindow, Update_Window -> UpdateWindow.
When my suggestion don't solve your problems, please provide a [mcve] so we can better identify your problems.

Netbeans Module: open JFrame on button click

I am coding a module for Netbeans where I have a button that when clicked will open a JFrame.
This is the action listener class of the button:
// ... (package and imports)
#ActionID(
category = "File",
id = "org.myorg.readabilitychecker.ReadabilityActionListener"
)
#ActionRegistration(
iconBase = "org/myorg/readabilitychecker/google.png",
displayName = "#CTL_ReadabilityActionListener"
)
#ActionReference(path = "Toolbars/File", position = 0)
#Messages("CTL_ReadabilityActionListener=Readability")
public final class ReadabilityActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JFrame readabilityFrame = new ReadabilityFrame();
readabilityFrame.setVisible(true);
}
}
In the JFrame I basically have:
public static void main(String args[]) {
* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ReadabilityFrame().setVisible(true);
}
});
}
It also has some other automatically generated code, but nothing important.
When I run the application, the button appears in the toolbar, but when I click it, the JFrame doesn't open.
I tried checking if a print inside the actionPerformed() method would show in the output terminal and it does, so I guess that I am missing something while calling the JFrame.
Can anyone give me a hint on where the problem is?
I think the issue is with the object creation of your frame. Try
ReadabilityFrame readabilityFrame = new ReadabilityFrame();
readabilityFrame.setVisible(true);
Hope it helps.
I found where was the problem.
The method initComponents() automatically generated had the line setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); and it was always throwing an exception.
I just changed EXIT_ON_CLOSE to DISPOSE_ON_CLOSE, defined the frame in a different way and now, the problem disappeared.

How to create an onclick event handler for MainNavBar from the SpiffyUI framework

I'm trying to figure out how to make it so that my navigation menu, when clicked, will open appropriate panels within my GWT page.
Here's a part of my code:
#Override
public void onModuleLoad()
{
MainNavBar nb = new MainNavbar();
NavItem i = new NavItem("1", "TestNavItem");
nb.add(i);
i = new NavItem("2", "TestNavItem2");
nb.add(i);
}
So when I run the project, I see that I have a menu on the test site:
So my question is, how can I have an event handler such that when either one of those are clicked, the panel to the right will be changed to something else?
Thanks!
create an actionListener class,
public class listen implements actionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == ObjectName){
// Your code goes here....
}
}
}
then create an object of this class e.g
listen listener = new listen();
YourObjectName.addActionListener(listener);
Don't forget to make the imports, hope this helps...

removing focus from JDialog if main application is minimised

I have a JDialog which popup whenever my main application recieves an event.
The problem I'm facing is that the dialog pops up evenif the main window is minimised. A similar question was asked here but no answer was given as to how to solve this except a link to sun's guide on focus handling Hide JDialog window when the window lost focus.
Suppose I have the function createandshowDialog() such as
public void createAndShowDialog(boolean manualLaunch) {
if (manualLaunch || shouldShowMessage()) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
if (dialog == null) {
dialog = new xyzDialog(getPendingList());
}
GUIHelper.flash(PLAFHelper.getMainFrame(), true, false);
dialog.setVisible(true);
}
});
}
}
And the xyzDialog class is defined as :
public class xyzDialog extends SimpleStandardDialog
{
protected final static Log logger = LogFactory.getLog(xyzDialog.class);
private xyzPanel panel;
public xyzDialog(ObjectArrayList list) {
super(PLAFHelper.getMainFrame(), "Pending Cancel/Replace");
initializeLocalVars();
panel = new xyzPanel(list, mediator);
super.setSize(750,600);
setResizable(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setModal(false);//todo: for testing...
if(PUMAGUIOptions.cFocusOnxyzPopup){
setFocusableWindowState(true);
validate();
}
else{
setFocusableWindowState(false);
validate();
}
}
The default behaviour should be such that it should not popup if main window is minimised or we explicitly set cFocusOnxyzPopup as false to force this default behaviour (which is the case when it is open on say secondary monitor and we are working on primary monitor or application is maximised or is in background i.e. is not the focusOwner.
I have set focusableWindowState as false so that it would not satisy the condition for gaining focus and return isFocusable as false if invoked as given in java-docs. But this is not working. Any suggestions?
USe JFrame's method
public synchronized int getExtendedState()
e.g. PLAFHelper.getMainFrame().getExtendedState()
if it's JFrame.ICONIFIED skip the dialog opening.
I have meet the same problem and search the stackoverflow ! finally, I find out the
corrent answer answered by Markus Lausberg in
JDialog lets main application lose focus
just calling the following method during dialog create:
setFocusableWindowState(false);
setFocusable(false);

With Java Swing App, how to get values from a dialog panel into the main app?

In my Swing app, users can click a button to open a dialog panel and enter some values, then they can click "Ok" on that panel to close it and return to the main program, but how can I pass the values they enter to the main program without saving them to a file first ?
There are a couple things you could do:
You could create an Observer/Observable relationship between your app and the dialog, and make the dialog publish an event when the dialog closes with the values in the event.
You could maintain a handle to your dialog in your app, and when you call setVisible(false) on dialog, you can query the dialog for its data.
The UI should usually be layered upon the underlying application. (IMO, the UI should itself be split into layers, but that another rant.) Pass in a relevant part of your application to the creation of your UI. When you are ready for values to go from the UI to the application (which could be an OK button on a multi-page dialog, down to every keystroke), push the data to the application component.
Rather than using just the poor Java Observable/Observer API, I'd rather advise you take a look at an "Event Bus", which should be particularly suited for your situation.
Two implementations of Event Buses:
EventBus very general event bus,
can be used in any situation
GUTS Event Bus specific to Guice dependency injection library
One of these should help you with your current problem.
Just define an interface with a single method (like: "returnToCaller(returntype rt)") in your dialog class.
The Constructor of the dialog takes an instance of this interface as input, and uses it to return results on exit/ok.
The mainmodule (window or whatever) simply instantiates the dialog and thus makes annonymous use of the interface, defininng the return method, sort of like a delegate (c#).
The call then being:
MyDialog md = new MyDialog(new MyDialog.ReturnToCaller() {
public void returnToCaller(ReturnValues rv) {
// Handle the values here
}
});
md.setModal(true);
md.setVisible(true);
I would suggest MVC(Model-view-controller) design. So dialog will be you view and possibly controller. But have to have a domain class which will be your model. For example, if the dialog is created to edit personal data, then you can have class called Person which will hold the data. The dialog should be designed in the way so you can set and get a Person from it.
The class implementing your dialog panel must have a link to your main program, and your main program must provide a method with parameters that will be the values to transmit.
Then your dialog panel class listen to the Ok button, and on the button click, it retrieve the values and use them with the aforementionned method.
class Main {
//...
private Dialog myDialog ;
public Main(){
//...
myDialog = new Dialog(this);
//...
}
//...
public void onDialogOk(String valueA, int valueB)
{
//...
}
}
class Dialog implement ActionListener{
//...
private Main myMain ;
public setMain(Main main){
myMain = main;
}
public Dialog(Main main){
//...
setMain(main) ;
//...
JButton ok = new JButton("ok") ;
ok.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// retrieve form values
String valueA = ... ;
int valueB = Integer.parse(...);
myMain.onDialogOK(valueA, valueB) ; //DONE
}
}
May be you would like to try this solution:
class MyDialog {
private static String[] returnValues = new String[10]
private static MyDialog dialog;
private MyDialog() {
initDialog()
}
private void closeDialog()
{
dispose();
}
private initDialog()
{
//....
btnOk = new JButton("OK");
jTextField1 = new JTextField();
...
jTextField10 = new JTextField();
...
ActionListener btnOK_click = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
returnValues[0] = jTextField1.getText();
...
returnValues[9] = jTextField10.getText();
closeDialog();
}
}
btnOk.addActionListener(btnOk_click);
}
public static String[] showMyDialog() {
dialog = new MyDialog();
dialog.setVisible(true);
return returnValues;
}
}

Categories