Shortcut to main panel via JmenuItem

1

I'm working on eclipse Mars 2.0 , where I use the windowBuilder , and I'm not able to link the shortcut: Consultar -> Dia (mntmDia) to main page panelConsultarDia , or either when people want to change the day they are entering events, you have to go back to the main page via the shortcut on the menu.

I have tried several things among which to remove jscrollpane but it starts to give errors in jdatepicker .

If you can see what I can change in the code, thank you.

Follow the code:

package net.ruimendes;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.CardLayout;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import javax.swing.JSpinner.DateEditor;
import javax.swing.JSpinner;

import org.jdatepicker.impl.JDatePanelImpl;

import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;
import org.jdatepicker.util.JDatePickerUtil;
import javax.swing.JTextPane;
import javax.swing.DropMode;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Properties;
import java.awt.event.ActionEvent;
import java.awt.Container;
import java.awt.Panel;
import java.awt.Button;
import java.awt.ScrollPane;

public class Tentativa {

    private JFrame frame;
    private JTextField textEvento;
    private JTextField textTipo;
    private JTextField textHoraInicio;
    private JTextField textDuracao;
    private JTextField textLocal;
    private JTable tableEventos;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Tentativa window = new Tentativa();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Tentativa() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, "name_7610722565189");

        JPanel panelEventos = new JPanel();
        frame.getContentPane().add(panelEventos, "panelEventos");
        GridBagLayout gbl_panelEventos = new GridBagLayout();
        gbl_panelEventos.columnWidths = new int[]{442, 0};
        gbl_panelEventos.rowHeights = new int[]{230, 23, 0};
        gbl_panelEventos.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panelEventos.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
        panelEventos.setLayout(gbl_panelEventos);

        JScrollPane scrollPane_1 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
        gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_1.gridx = 0;
        gbc_scrollPane_1.gridy = 0;
        panelEventos.add(scrollPane_1, gbc_scrollPane_1);

        tableEventos = new JTable();
        scrollPane_1.setViewportView(tableEventos);

        Panel panelBotoes = new Panel();
        GridBagConstraints gbc_panelBotoes = new GridBagConstraints();
        gbc_panelBotoes.anchor = GridBagConstraints.NORTH;
        gbc_panelBotoes.fill = GridBagConstraints.HORIZONTAL;
        gbc_panelBotoes.gridx = 0;
        gbc_panelBotoes.gridy = 1;
        panelEventos.add(panelBotoes, gbc_panelBotoes);
        GridBagLayout gbl_panelBotoes = new GridBagLayout();
        gbl_panelBotoes.columnWidths = new int[]{153, 67, 63, 0};
        gbl_panelBotoes.rowHeights = new int[]{23, 0};
        gbl_panelBotoes.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_panelBotoes.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panelBotoes.setLayout(gbl_panelBotoes);

        JButton btnApagar = new JButton("Apagar");
        GridBagConstraints gbc_btnApagar = new GridBagConstraints();
        gbc_btnApagar.anchor = GridBagConstraints.NORTHWEST;
        gbc_btnApagar.insets = new Insets(0, 0, 0, 5);
        gbc_btnApagar.gridx = 1;
        gbc_btnApagar.gridy = 0;
        panelBotoes.add(btnApagar, gbc_btnApagar);

        JButton btnInserir = new JButton("Inserir");
        GridBagConstraints gbc_btnInserir = new GridBagConstraints();
        gbc_btnInserir.anchor = GridBagConstraints.NORTHWEST;
        gbc_btnInserir.gridx = 2;
        gbc_btnInserir.gridy = 0;
        panelBotoes.add(btnInserir, gbc_btnInserir);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnConsultar = new JMenu("Consultar");
        menuBar.add(mnConsultar);

        JMenuItem mntmDia = new JMenuItem("Dia");
        mntmDia.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout card = (CardLayout) frame.getContentPane().getLayout();
                card.show(frame.getContentPane(), "panelConsultarDIa");

            }
        });
        mnConsultar.add(mntmDia);

        JMenu mnEvento = new JMenu("Evento");
        menuBar.add(mnEvento);


        JMenuItem mntmEditar = new JMenuItem("Editar");
        mntmEditar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                CardLayout card=(CardLayout)frame.getContentPane().getLayout();
                card.show(frame.getContentPane(), "panelEditar");


            }
        });

        mnEvento.add(mntmEditar);


        JMenuItem mntmApagar = new JMenuItem("Apagar");
        mnEvento.add(mntmApagar);



        JMenuItem mntmInserir = new JMenuItem("Inserir");
        mntmInserir.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 CardLayout card=(CardLayout)frame.getContentPane().getLayout();

                 card.show(frame.getContentPane(), "panelEvento");
             }
        });
        //  mntmInserir.addActionListener(new ActionListener() {
//          public void actionPerformed(ActionEvent e) {
    //          GridBagLayout panel=(GridBagLayout)frame.getContentPane().getLayout();
 //                  panel.add(frame.getContentPane(), "panelEvento");
        //  }
        //});
        /*JMenuItem mntmConsultar = new JMenuItem("Consultar");
        mntmConsultar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                preencheTabelaMovimentos();
                CardLayout card=(CardLayout)frame.getContentPane().getLayout();
                card.show(frame.getContentPane(), "panelConsultar");
            }
        });

*/
        mnEvento.add(mntmInserir);


        JPanel panelConsultarDia = new JPanel();
        //frame.getContentPane().add(panelConsultarDia, "panelConsultarDia");
        panelConsultarDia.setBackground(new Color(245, 255, 250));
        scrollPane.setViewportView(panelConsultarDia);
        GridBagLayout gbl_panelConsultarDia = new GridBagLayout();
        gbl_panelConsultarDia.columnWidths = new int[]{0, 0, 0, 0, 53, 102, 55, 0};
        gbl_panelConsultarDia.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
        gbl_panelConsultarDia.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_panelConsultarDia.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panelConsultarDia.setLayout(gbl_panelConsultarDia);


        UtilDateModel model = new UtilDateModel();
        LocalDate hoje=LocalDate.now();
        model.setDate(hoje.getYear(), hoje.getMonthValue()-1, hoje.getDayOfMonth());
        Properties p = new Properties();
        p.put("text.today", "Hoje");
        p.put("text.month", "Mês");
        p.put("text.year", "Ano");
        JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter2());
        GridBagConstraints gbc_datePicker = new GridBagConstraints();
        gbc_datePicker.insets = new Insets(0, 0, 5, 5);
        gbc_datePicker.gridx = 1;
        gbc_datePicker.gridy = 0;
        panelConsultarDia.add(datePicker, gbc_datePicker);

        JButton btnSelecionar = new JButton("Selecionar");
        btnSelecionar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Date selectedDate = (Date) datePicker.getModel().getValue();
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                String reportDate = df.format(selectedDate);
            }
        });
        GridBagConstraints gbc_btnSelecionar = new GridBagConstraints();
        gbc_btnSelecionar.insets = new Insets(0, 0, 5, 5);
        gbc_btnSelecionar.gridx = 2;
        gbc_btnSelecionar.gridy = 0;
        panelConsultarDia.add(btnSelecionar, gbc_btnSelecionar);

        JPanel panelInserir = new JPanel();
        panelInserir.setBackground(Color.WHITE);
        frame.getContentPane().add(panelInserir, "panelEvento");
        GridBagLayout gbl_panelInserir = new GridBagLayout();
        gbl_panelInserir.columnWidths = new int[]{0, 92, 0, 0};
        gbl_panelInserir.rowHeights = new int[]{0, 0, 27, 27, 32, 28, 0, 0, 0, 0};
        gbl_panelInserir.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_panelInserir.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        panelInserir.setLayout(gbl_panelInserir);

        JLabel lblEvento = new JLabel("Evento");
        lblEvento.setBackground(Color.LIGHT_GRAY);
        GridBagConstraints gbc_lblEvento = new GridBagConstraints();
        gbc_lblEvento.insets = new Insets(0, 0, 5, 5);
        gbc_lblEvento.gridx = 1;
        gbc_lblEvento.gridy = 1;
        panelInserir.add(lblEvento, gbc_lblEvento);

        textEvento = new JTextField();
        GridBagConstraints gbc_textEvento = new GridBagConstraints();
        gbc_textEvento.insets = new Insets(0, 0, 5, 0);
        gbc_textEvento.fill = GridBagConstraints.HORIZONTAL;
        gbc_textEvento.gridx = 2;
        gbc_textEvento.gridy = 1;
        panelInserir.add(textEvento, gbc_textEvento);
        textEvento.setColumns(10);

        JLabel lblTipo = new JLabel("Tipo");
        GridBagConstraints gbc_lblTipo = new GridBagConstraints();
        gbc_lblTipo.insets = new Insets(0, 0, 5, 5);
        gbc_lblTipo.gridx = 1;
        gbc_lblTipo.gridy = 2;
        panelInserir.add(lblTipo, gbc_lblTipo);

        textTipo = new JTextField();
        GridBagConstraints gbc_textTipo = new GridBagConstraints();
        gbc_textTipo.insets = new Insets(0, 0, 5, 0);
        gbc_textTipo.fill = GridBagConstraints.HORIZONTAL;
        gbc_textTipo.gridx = 2;
        gbc_textTipo.gridy = 2;
        panelInserir.add(textTipo, gbc_textTipo);
        textTipo.setColumns(10);

        JLabel lblHoraDeInicio = new JLabel("Hora de Inicio");
        GridBagConstraints gbc_lblHoraDeInicio = new GridBagConstraints();
        gbc_lblHoraDeInicio.insets = new Insets(0, 0, 5, 5);
        gbc_lblHoraDeInicio.gridx = 1;
        gbc_lblHoraDeInicio.gridy = 3;
        panelInserir.add(lblHoraDeInicio, gbc_lblHoraDeInicio);

        textHoraInicio = new JTextField();
        GridBagConstraints gbc_textHoraInicio = new GridBagConstraints();
        gbc_textHoraInicio.insets = new Insets(0, 0, 5, 0);
        gbc_textHoraInicio.fill = GridBagConstraints.HORIZONTAL;
        gbc_textHoraInicio.gridx = 2;
        gbc_textHoraInicio.gridy = 3;
        panelInserir.add(textHoraInicio, gbc_textHoraInicio);
        textHoraInicio.setColumns(10);

        JLabel lblDurao = new JLabel("Dura\u00E7\u00E3o");
        GridBagConstraints gbc_lblDurao = new GridBagConstraints();
        gbc_lblDurao.insets = new Insets(0, 0, 5, 5);
        gbc_lblDurao.gridx = 1;
        gbc_lblDurao.gridy = 4;
        panelInserir.add(lblDurao, gbc_lblDurao);

        textDuracao = new JTextField();
        GridBagConstraints gbc_textDuracao = new GridBagConstraints();
        gbc_textDuracao.insets = new Insets(0, 0, 5, 0);
        gbc_textDuracao.fill = GridBagConstraints.HORIZONTAL;
        gbc_textDuracao.gridx = 2;
        gbc_textDuracao.gridy = 4;
        panelInserir.add(textDuracao, gbc_textDuracao);
        textDuracao.setColumns(10);

        JLabel lblLocal = new JLabel("Local");
        GridBagConstraints gbc_lblLocal = new GridBagConstraints();
        gbc_lblLocal.insets = new Insets(0, 0, 5, 5);
        gbc_lblLocal.gridx = 1;
        gbc_lblLocal.gridy = 5;
        panelInserir.add(lblLocal, gbc_lblLocal);

        textLocal = new JTextField();
        GridBagConstraints gbc_textLocal = new GridBagConstraints();
        gbc_textLocal.insets = new Insets(0, 0, 5, 0);
        gbc_textLocal.fill = GridBagConstraints.HORIZONTAL;
        gbc_textLocal.gridx = 2;
        gbc_textLocal.gridy = 5;
        panelInserir.add(textLocal, gbc_textLocal);
        textLocal.setColumns(10);

        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 2;
        gbc_panel.gridy = 8;
        panelInserir.add(panel, gbc_panel);

        JButton btnGuardar = new JButton("Guardar");
        panel.add(btnGuardar);

        JButton btnCancelar = new JButton("Cancelar");
        btnCancelar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout card = (CardLayout) frame.getContentPane().getLayout();
                card.show(frame.getContentPane(), "panelEventos");
            }
        });
        panel.add(btnCancelar);


    }

}
    
asked by anonymous 19.04.2016 / 18:48

1 answer

1

You are trying to display a component (O JPanel painelConsultarDia ) that was not referenced in CardLayout , but rather added in another container ( Scrollpane ) that is within your CardLayout .

In:

mntmEditar.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        CardLayout card=(CardLayout)frame.getContentPane().getLayout();
        card.show(frame.getContentPane(), "panelEditar");
    }
});

Change to:

mntmEditar.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        CardLayout card=(CardLayout)frame.getContentPane().getLayout();
        card.show(frame.getContentPane(), "name_7610722565189");
    }
});
    
21.04.2016 / 15:58