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.
Related
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.
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.
I have few issues with my swing app which looks like this app preview
This Frame is defined in Form class.
Form class is building in main class, code below
public class Main {
public static void main(String[] args) {
Form form = new Form();
checkIfRunning();
}
"New doctor" button has it's own listener which check is this object is already exist. Listener is defined in Form class.
newDoctorButton.addActionListener(new ActionListener() {
private NDoctor dc = null;
#Override
public void actionPerformed(ActionEvent e) {
if (dc == null){
dc = new NDoctor();}
else dc.toFront();
}
});
Everything works fine, new window is opening as i want. second window preview
"Ok" button listener below
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
NDoctor.super.dispose();
}
});
When i close it i cant open it once more.
Im sure the problem is in the main method. The "New Doctor"listener works but in run only once, when i close window it wont start until i call main method again, but this will create next Frame.
I hope you understand my problem. I will be grateful for any advice.
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...
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;
}
}