Question

Hi i am trying to make java desk top application where i am using 2 jpanel in that 2 jpanel i am drawing jlabel

i am having button next i want when i click button next jlabel data should change how can i do this

here is my code

public Second() {
          this.getContentPane().setBackground(new java.awt.Color(255, 140, 0));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);

            initComponents();


             JPanel panel = createPanel(); 
        jPanel1.setLayout(new GridBagLayout());
//         jPanel1.setLayout(null);
     jPanel1.add(panel);

     JPanel panel1 = createPanel(); 
        jPanel2.setLayout(new GridBagLayout());
//  jPanel2.setLayout(null);
     jPanel2.add(panel1);


      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
   //get current date time with Date()
   Date date = new Date();
   //System.out.println(dateFormat.format(date)); don't print it, but save it!
   String yourDate = dateFormat.format(date);
       jButton7.setText(yourDate);
         jButton6.setText(yourDate);
       jButton5.setText(yourDate);
       jButton1.setText(yourDate);

    }


     private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5));
        EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10);
       panel.setBorder(panelBorder);
 panel.setBackground(new java.awt.Color(255, 153, 51));
        panel.setOpaque(true);
        EmptyBorder border1 = new EmptyBorder(15, 20, 15, 18);
       // LineBorder line = new LineBorder(Color.blue, 2, true);
     //   CompoundBorder compound = new CompoundBorder(line, border);
          Border border = BorderFactory.createLineBorder(Color.BLUE, 2);
        for (int i = 0; i <11; i++) {
            JLabel label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>");
  label.setFont(new java.awt.Font("Times New Roman", 1, 18));
           label.setBorder(border);

           label.setBorder(border1);

           label.setBackground(Color.GRAY);
             label.setForeground(new java.awt.Color(255, 255,255 ));
            label.setOpaque(true);
            panel.add(label);
        }
        return panel;
    }

How can i achieve my desired output

my updated code

 public Second() {
              this.getContentPane().setBackground(new java.awt.Color(255, 140, 0));
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setUndecorated(true);

                initComponents();


                 JPanel panel = createPanel(); 
            jPanel1.setLayout(new GridBagLayout());
    //         jPanel1.setLayout(null);
         jPanel1.add(panel);

         JPanel panel1 = createPanel(); 
            jPanel2.setLayout(new GridBagLayout());
    //  jPanel2.setLayout(null);
         jPanel2.add(panel1);


          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
       //get current date time with Date()
       Date date = new Date();
       //System.out.println(dateFormat.format(date)); don't print it, but save it!
       String yourDate = dateFormat.format(date);
           jButton7.setText(yourDate);
             jButton6.setText(yourDate);
           jButton5.setText(yourDate);
           jButton1.setText(yourDate);

        }


         private JPanel createPanel() {
            JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5));
            EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10);
           panel.setBorder(panelBorder);
     panel.setBackground(new java.awt.Color(255, 153, 51));
            panel.setOpaque(true);
            EmptyBorder border1 = new EmptyBorder(15, 20, 15, 18);
           // LineBorder line = new LineBorder(Color.blue, 2, true);
         //   CompoundBorder compound = new CompoundBorder(line, border);
              Border border = BorderFactory.createLineBorder(Color.BLUE, 2);
            for (int i = 0; i <11; i++) {
                JLabel label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>");
      label.setFont(new java.awt.Font("Times New Roman", 1, 18));
               label.setBorder(border);

               label.setBorder(border1);

               label.setBackground(Color.GRAY);
                 label.setForeground(new java.awt.Color(255, 255,255 ));
                label.setOpaque(true);
                panel.add(label);
            }
            return panel;
        }


private void NextActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:

    label.setText("new text");
}                                    

on every click of button i want

1 click "hello";
2 click "sharma"
3 click " not  bad";
4 click " not good"

and so on

Thanks in advance

Was it helpful?

Solution

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText("new text");
        }
    });

Will change the text of label to new text when button is clicked.

    button.addActionListener(new ActionListener() {
        int clicks = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            switch (++clicks) {
                case 1:
                    label.setText("hello");
                break;

                case 2:
                    label.setText("sharma");
                break;

                //continue doing it like this
            }
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top