JTable e JButton

4 respostas
brunajeniferf

Oi Por favor, alguém pode me passar um exemplo de uma tabela que é mostrada em um JPanel, mas somente quando

se clica em um botão??

Por favor, já tentei de tudo não sei o que está errado no meu código. :?:

4 Respostas

lina

Oi,

Mande seu código para analisarmos!

Tchauzin!

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);


        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);


        // Dados para botão Teste
        final JTable table = new JTable();


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



        // 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);
        // }
        // }

        // 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);
        // }
        // }
        // 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);
        // }
        // }
        // 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);
        // }
        // }



        // Implementa o botão Teste
        Teste.addActionListener(
                new ActionListener() {

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

                        JScrollPane ScrollPane = new JScrollPane(table);

                       

                      
                       

                        jPanelRodape.add(ScrollPane);

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

                        JOptionPane.showMessageDialog(null, "Oi");

                    }
                });

        // Implementa o Menu "Health"
        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) {
                            JOptionPane.showMessageDialog(null, " Não importou o arquivo");
                        }
                        FileDataStore store2 = null;
                        try {
                            store2 = FileDataStoreFinder.getDataStore(url2);
                        } catch (IOException ex) {
                            JOptionPane.showMessageDialog(null, "Store2 = null");
                        }
                        try {
                            FeatureSource featureSource2 = store2.getFeatureSource();
                        } catch (IOException ex) {
                            JOptionPane.showMessageDialog(null, " Não realizado");
                        }

                        URL url3 = null;
                        try {
                            url3 = new URL("file:/dadosBruna/pontos_saude.shp");
                        } catch (MalformedURLException ex) {
                            JOptionPane.showMessageDialog(null, " Não realizado");
                        }
                        FileDataStore store3 = null;

                        try {
                            FeatureSource featureSource3 = store3.getFeatureSource();
                        } catch (IOException ex) {
                            JOptionPane.showMessageDialog(null, " Não realizado");
                        }
                    }
                });

        // Implementa Menu chamado "Labor"
        jMenuItem22.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                    }
                });




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

    }

    private JTable inicializaTabela(JTable table) {
        table = new JTable(new MyTableModel());
        return table;
    }
}

Mas o jeito que está no botão Teste , a tabela não está sendo exibida

lina

Oi,

A principio faltou definir um tamanho em sua JScroll e um repaint.

Tchauzin!

brunajeniferf

:?
Eu incrementei essas funções mas nada ocorreu…

Obrigada

Criado 2 de maio de 2011
Ultima resposta 3 de mai. de 2011
Respostas 4
Participantes 2