Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
modal JDialog not responding to jbutton actionlistener?
Andre Tow
Ranch Hand
Posts: 158
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Am i missing something here? i dont understand why
it works in nonmodal,but not modal.
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; public class SearchFrame extends JDialog { private String origin; private String destination; private Container contentPane; private JLabel originLabel; private JLabel destinationLabel; private JButton okButton; private JButton cancelButton; private JTextField originField; private JTextField destinationField; public SearchFrame(Frame parent) { super(parent,"Search!",true); contentPane = this.getContentPane(); this.setSize(200, 100); // techniquie for centering screen Dimension frameSize = this.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); JPanel left = new JPanel(); left.setLayout(new GridLayout(2, 1)); originLabel = new JLabel("Origin airport: "); destinationLabel = new JLabel("Destination airport: "); left.add(originLabel); left.add(destinationLabel); JPanel right = new JPanel(); right.setLayout(new GridLayout(2, 1)); originField = new JTextField(3); destinationField = new JTextField(3); right.add(originField); right.add(destinationField); JPanel south = new JPanel(); south.setLayout(new FlowLayout()); okButton = new JButton("OK"); south.add(okButton); contentPane.add(left, BorderLayout.WEST); contentPane.add(right, BorderLayout.CENTER); contentPane.add(south, BorderLayout.SOUTH); this.setVisible(true); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("inside action performed"); origin = new String(originField.getText()).trim(); destination = new String(destinationField.getText()).trim(); SearchFrame.this.setVisible(false); } }); } public String getOrigin() { return origin; } public String getDestination() { return destination; } public static void main(String[] args) { final SearchFrame sd=new SearchFrame(new JFrame()); System.out.println(sd.getOrigin()+sd.getDestination()); } }
Paul Stevens
Ranch Hand
Posts: 2823
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Try moving your anonymous listener code right after you create the button. Right now it is after setVisible().
Lasagna is spaghetti flvored cake. Just like this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Moving a picture down a bit
NX: URLy Bird 1.3.1 Suncertify.properties
Highlighter Issues
Layout question
ActonListener
More...