Results 1 to 6 of 6
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default The GUI with Filechooser and button

    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.BorderLayout;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JFileChooser; 
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.io.File; 
    public class MainGUI extends JFrame{
    
    JLabel label = new JLabel("Vyberit fayl z koordynatamy");
    static JFileChooser fileopen = new JFileChooser();
    
    public MainGUI() {
          JPanel panel = new JPanel();
          this.setTitle("Main");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(500, 700);
    		this.setLocationRelativeTo(null);
    		this.setLayout(new GridBagLayout());
    		this.setVisible(true);
          panel.add(label);
          panel.add(fileopen);
          this.add(fileopen);
          this.add(panel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
    				GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(
    						2, 2, 2, 2), 0, 0));}  
    
    public static void gui () {
    FileFilter filter = new FileNameExtensionFilter("text files", "txt"); 
    fileopen.addChoosableFileFilter(filter); 
    int ret = fileopen.showDialog(null, "Select file"); 
    if (ret == JFileChooser.APPROVE_OPTION) { 
    File file= fileopen.getSelectedFile(); 
    String fname = file.getName();
    Mistax.fname=fname;
    System.out.println(Mistax.fname);
    }
    }
    
    static class ChooserListener implements  ActionListener {
    public void actionPerformed(ActionEvent actionEvent) { 
    JFileChooser fileopen=(JFileChooser)actionEvent.getSource();
    gui();
    }
    }
         
    public static void main(String[] args) {
    MainGUI b=new MainGUI();
    b.fileopen.addActionListener(new ChooserListener());
    //b.gui();
    }
    }
    --here I cannot to have filechooser to work with ActionPerformed (i.e. with ActionListener)
    despite I pass there the gui () method that works fine if
    I use
    Java Code:
    public static void main(String[] args) {
    MainGUI b=new MainGUI();
    //b.fileopen.addActionListener(new ChooserListener());
    b.gui();
    }
    }
    --that is it chose just text files and pass the name of its file, and also
    link attache the FileChooser inside Frame.
    Without it trying to use ChooseListener (maybe static context do not allow it)--I cannot filter by txt files, and
    when pressing OK button I do not get the reaction--so here Filechooser do not works. As well the filechooser window is separated from frame window, unlike in the first case.

  2. #2
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: The GUI with Filechooser and button

    Moreover when using ActionPerformed method I could select any extension for example doc file when I try to select it (it did not succede),
    I get a new filechooser windows where I can select just txt files, but when trying to open (select)--I got the stacktrace of exceptions.
    So here is some 2 tier system--but actionlistener di not work, but probably due to
    some inconsistency.
    By removing --
    Java Code:
     JFileChooser fileopen=(JFileChooser)actionEvent.getSource()
    -- I did not resolve the issue.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: The GUI with Filechooser and button

    I'm really not sure what you're asking. Sounds like you need to debug your program. Try stepping through this with a debugger, or at least add some print statements, to determine when the program's execution differs from your expectations.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: The GUI with Filechooser and button

    Thanks, the simple GUI with 2 labels(taht maybe not nessesary), and 2 buttons, one for launching FileChooser (so after passing the button the filechooserlistener the issue disappered-unlike addig such listener to fielchooser itself--as the well as issue of framing the filechhoser to the mainframe), and another one for showing Map.
    So issue is how to allign it strictly vertically, and to set some visible appropriate intervals between this
    buttons.
    The second point is -- returning to the previous topics--
    if I alunch tteh map by button, I would like to launch the map with all roads, and other one--just with minimal ones.
    So I would lije to make 2 buttons for map. But one-the simplest way is to link this button with listener to two different
    files(classes) that would draw full and "miimal" map.
    Although
    I use
    Java Code:
    pblic void addButtonListener() {
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    new Graf();
    }
    });}
    But what to do in http://www.java-forums.org/java-2d/9...ap-java-2.html
    this example that the paint would draw full map, but with some repaint() method --but probably there is no other one--would paint
    the minimal ways. But how to use repaint nethod in this example?

  5. #5
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: The GUI with Filechooser and button

    Peole why do yo cannot help?AMybe I need to use the update () method as simple repaint() do not help?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: The GUI with Filechooser and button

    To draw a map in Java you would need to override paintComponent in a JPanel and then proceed to draw lines from point to point until the map is drawn. If the map points are in a different format (e.g. long/lat) then you need to convert them to something meaningful within the bounds of your JPanel. I presume you would be drawing a Mercator projection of the map.

    Another way might be to have images of parts of the map. But then you need to place them together properly. For example, I could have pictures of the 48 contiguous states of the US. But "pasting" them together requires some data to describe where they are in relation to each other.

    Additional detail such as cities and roads require similar data.

    Edit: I forgot to mention that once you override paintComponent, you need to invoke repaint() to have it paint the map.

    Regards,
    Jim
    Last edited by jim829; 04-26-2015 at 04:28 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with fileChooser
    By sportinus in forum AWT / Swing
    Replies: 1
    Last Post: 05-25-2011, 08:43 PM
  2. FileChooser
    By Unsub in forum New To Java
    Replies: 7
    Last Post: 03-10-2010, 03:54 AM
  3. FileChooser
    By jperson in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 04:43 PM
  4. Filechooser
    By Joe2003 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 07:51 AM
  5. FileChooser UI
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 11-27-2008, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •