JMenuItem

5 respostas
brunajeniferf

Oi Gente tudo bem?

Eu estou com o seguinte problema, eu tenho um JMenuItem, e ao clicar nele eu quero que abra um arquivo URL
o codigo que faz esse arquivo abrir é o seguinte:

URL url2 = new URL("file:/I:/dadosBruna/saude_enlaces.shp");
         FileDataStore store2 = FileDataStoreFinder.getDataStore(url2);
         FeatureSource featureSource2 = store2.getFeatureSource();

          Style style2 = SLD.createLineStyle(Color.BLUE, 1F);
          map.addLayer(featureSource2, style2);

Como que eu faço um actionListener resolve?
Ou preciso criar outro tipo de evento?

obrigada

5 Respostas

RenanRosa

brunajeniferf:
Oi Gente tudo bem?
Como que eu faço um actionListener resolve?
Ou preciso criar outro tipo de evento?
obrigada

como assim ?

apenas adiciona o actionlistener no menuitem.

menuItem.addActionListener(new ActionListener() {			
     @Override
     public void actionPerformed(ActionEvent arg0) {
          //faz o que tem que fazer
     }
});
brunajeniferf

Eu fiz isso ai adicionei o código, ai ele diz que tem erro e pede pra colocar try e catch … ai quando eu coloco isso e mando executa o programa e selecionou o MenuItem não acontece nada.

RenanRosa

pode postar seu código ?

brunajeniferf
class MyTableModel extends AbstractTableModel {

    private boolean DEBUG = false;
    private String[] ColumnNames = {"CODIGO", "NOME", "REGIONAL", "CODREG",
        "REGIONAL 2", "CODREG 2"};
    private Object[][] data = {
        {new Integer(21), "Cajuru", "A.R. Cajuru", new Float(4.0), " ",
            new Float(0.0)},
        {new Integer(75), "Cic", "A.R. Santa felicidade", new Float(5.0),
            "A.R. Cic", new Float(9.0)},
        {new Integer(70), "Caximba", "A.R. Pinheirinho", new Float(7.0), " ",
            new Float(0.0)},
        {new Integer(56), "Boqueirao", "A.R. Boqueirao", new Float(6.0), " ",
            new Float(0.0)},
        {new Integer(22), "Jardim das Americas", "A.R. Cajuru",
            new Float(4.0), " ", new Float(0.0)},
        {new Integer(18), "Jardim Social", "A.R. Matriz", new Float(1.0),
            " ", new Float(0.0)}};

    public int getColumnCount() {
        return ColumnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnNames(int col) {
        return ColumnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        if (col < 2) {
            return false;
        } else {
            return true;
        }



    }

    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value
                    + " (an instance of "
                    + value.getClass() + ")");
        }

        data[row][col] = value;
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i = 0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j = 0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

public class App {

    private boolean DEBUG = false;

    public static void main(String[] args) throws MalformedURLException, IOException {
        JFrame frame = new JFrame("Social Network Visualization");
        frame.setLayout(new BorderLayout());

        JPanel jPanelCabecalho = new JPanel(new FlowLayout());
        JPanel jPanelMeio = new JPanel(new GridLayout());
        final JPanel jPanelRodape = new JPanel(new FlowLayout());

        jPanelCabecalho.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
        jPanelMeio.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
        jPanelRodape.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));

        frame.getContentPane().add(jPanelCabecalho, BorderLayout.NORTH);
        frame.getContentPane().add(jPanelMeio, BorderLayout.CENTER);
        frame.getContentPane().add(jPanelRodape, BorderLayout.SOUTH);

        JMapPane jMapPane = new JMapPane();
        jPanelMeio.add(jMapPane);

        MapContext map = new DefaultMapContext();

        /* Inclusao dos layers */
        URL url1 = new URL("file:/I:/dadosBruna/bairros.shp");
        FileDataStore store1 = FileDataStoreFinder.getDataStore(url1);
        FeatureSource featureSource1 = store1.getFeatureSource();

        Style style1 = SLD.createPolygonStyle(Color.lightGray, null, 1.0F);
        map.addLayer(featureSource1, style1);

        //URL url2 = new URL("file:/I:/dadosBruna/saude_enlaces.shp");
        //FileDataStore store2 = FileDataStoreFinder.getDataStore(url2);
        //FeatureSource featureSource2 = store2.getFeatureSource();

        //Style style2 = SLD.createLineStyle(Color.BLUE, 1F);
        //map.addLayer(featureSource2, style2);

        //URL url3 = new URL("file:/dadosBruna/pontos_saude.shp");
        //FileDataStore store3 = FileDataStoreFinder.getDataStore(url3);
        //FeatureSource featureSource3 = store3.getFeatureSource();

        //Style style3 = SLD.createPointStyle("Circle", Color.RED, Color.RED, 1, 8, null, null);
        //map.addLayer(featureSource3, style3);



        jMapPane.setMapContext(map);
        jMapPane.setRenderer(new StreamingRenderer());
        jMapPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));

        JMenuBar jMenuBar = new JMenuBar();

        JMenu jMenu = new JMenu("File");
        JMenuItem jMenuItem = new JMenuItem("Exit");
        jMenu.add(jMenuItem);

        JMenu jMenu2 = new JMenu("Network");
        JMenuItem jMenuItem21 = new JMenuItem("Health");
        JMenuItem jMenuItem22 = new JMenuItem("Labor");
        JMenuItem jMenuItem23 = new JMenuItem("Education");
        JMenuItem jMenuItem24 = new JMenuItem("Social Assistance");
        JMenuItem jMenuItem25 = new JMenuItem("Housing");
        JMenuItem jMenuItem26 = new JMenuItem("Food Secutiry");
        JMenuItem jMenuItem27 = new JMenuItem("Complete Network");
        jMenu2.add(jMenuItem21);
        jMenu2.add(jMenuItem22);
        jMenu2.add(jMenuItem23);
        jMenu2.add(jMenuItem24);
        jMenu2.add(jMenuItem25);
        jMenu2.add(jMenuItem26);
        jMenu2.add(jMenuItem27);

        jMenuBar.add(jMenu);
        jMenuBar.add(jMenu2);

        frame.setJMenuBar(jMenuBar);

        URL url4 = new URL("file:/dadosBruna/rede_assistencia_social.PNG");
        BufferedImage bufferedImage = ImageIO.read(url4);
        JLabel jLabel = new JLabel(new ImageIcon(bufferedImage));
        jLabel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));

        jPanelMeio.add(jLabel, BorderLayout.CENTER);



        JButton jButtonZoomIn = new JButton(new ZoomInAction(jMapPane));
        JButton jButtonZoomOut = new JButton(new ZoomOutAction(jMapPane));
        JButton jButtonPan = new JButton(new PanAction(jMapPane));
        JButton jButtonIdentify = new JButton(new InfoAction(jMapPane));
        JButton Teste = new JButton("Teste");



        jPanelCabecalho.add(jButtonZoomIn);
        jPanelCabecalho.add(jButtonZoomOut);
        jPanelCabecalho.add(jButtonPan);
        jPanelCabecalho.add(jButtonIdentify);
        jPanelCabecalho.add(Teste);

        // Teste



        //JTable jTable = new JTable();

        //jTable = new JTable();
        //jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //jTable.setModel(new DefaultTableModel(5, 5));
        //jTable.setPreferredScrollableViewportSize(new Dimension(500, 200));

        //JScrollPane scrollPane = new JScrollPane(jTable);
        //jPanelRodape.add(scrollPane, BorderLayout.CENTER);

        final JTable table = new JTable();


        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(new DefaultTableModel(5, 5));
        table.setPreferredScrollableViewportSize(new Dimension(500, 200));
        JScrollPane ScrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        jPanelRodape.add(ScrollPane, BorderLayout.CENTER);

        Teste.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        JTable table = new JTable(new MyTableModel());

                        JScrollPane ScrollPane = new JScrollPane(table);
                        jPanelRodape.add(ScrollPane, BorderLayout.CENTER);

                        ScrollPane.setVisible(true);
                        jPanelRodape.setVisible(true);

                        JOptionPane.showMessageDialog(null, "Oi");
                    }
                });

        jMenuItem21.addActionListener(
                new ActionListener() {
                @Override
               public void actionPerformed(ActionEvent e) {
                   
                  URL url2 = null;
                try {
                    url2 = new URL("file:/I:/dadosBruna/saude_enlaces.shp");
                } catch (MalformedURLException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }
                  FileDataStore store2 = null;
                try {
                    store2 = FileDataStoreFinder.getDataStore(url2);
                } catch (IOException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    FeatureSource featureSource2 = store2.getFeatureSource();
                } catch (IOException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }
                    }
                });


        // TableColumn column = null;
        //for (int i = 0; i < 6; i++) {
        // column = table.getColumnModel().getColumn(i);
        //if (i == 1) {
        //    column.setPreferredWidth(170);
        // }
        //if (i == 2 || i == 4) {
        //    column.setPreferredWidth(80);
        //} else {
        //  column.setPreferredWidth(70);
        // }
        // }


        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

no button Teste ele também não está exibindo a tabela e eu não sei porque..

obrigada

RenanRosa
Faça apenas assim:
jMenuItem21.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
           
      }
});
Criado 28 de abril de 2011
Ultima resposta 29 de abr. de 2011
Respostas 5
Participantes 2