Home > Back-end >  Is there any other way to display different images in a new JFrame, depending on the JRadioButton ob
Is there any other way to display different images in a new JFrame, depending on the JRadioButton ob

Time:06-30

I created a Java program that is intended for the following purpose: upon clicking each JRadioButton object representing a chemical element in the periodic table, the corresponding screenshot represented by a JLabel object in a new JFrame would exhibit various properties of each chemical element in the periodic table.

The program has the public classes named PeriodicTable, AtomicNumber and Properties. PeriodicTable contains the main method and instantiates AtomicNumber. AtomicNumber is a JFrame displaying the periodic table, whereas Properties opens up a new JFrame for displaying the png image for properties. All chemical elements are represented by JRadioButton objects. Various properties of each chemical element in the periodic table is a screenshot that has been saved as a png image, instantiated with the JLabel ImageIcon method.

After arranging all the chemical elements in the exact order as in the periodic table, I noticed that there is not enough space in the JFrame for adding the png files for properties. To resolve this issue, I created a new JFrame with the public class named Properties, in which only the png image of the first chemical element Hydrogen could be displayed. While resizing the png images for properties seems to be a feasible approach, the font would be too small to be legible as a result. In order to display different png images for properties, depending on which JRadioButton object representing chemical element is clicked, I have tried to add and remove the png images within ActionEvent in the public class AtomicNumber, but to no avail.

I have considered the possibility of creating more than 100 classes for displaying each window containing the corresponding png file of properties, but it seems to be too much of a hassle. Without creating 100 classes, is there any other way to display each corresponding png file of properties upon clicking a JRadioButton object representing a chemical element? Thank you for the help.

enter image description here enter image description here

My code is as follows:

//PeriodicTable.java
public class PeriodicTable {
  public static void main(String[] args) {
    new AtomicNumber();
  }
}
********************
//AtomicNumber.java
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Font;
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    public class AtomicNumber extends JFrame implements ActionListener {
      private static final long serialVersionUID = 1L;
      static ButtonGroup periodicTable;
      ImageIcon[] elementImage;
      ImageIcon periodicTableIcon;
      static JButton[] button;
      static JLabel[] properties;
      static JPanel panel;
      static JRadioButton[] element;
      JTextField title;
    
  AtomicNumber() {
    elementClip = new Clip[119];
    propertiesClip = new Clip[119];
    elementAudioFile = new File[119];
    propertiesAudioFile = new File[119];
    elementAudioFile[1] = new File("H.wav");

    elementImage = new ImageIcon[119];
    properties = new JLabel[119];
    button = new JButton[2];
    button[0] = new JButton("reset");
    button[0].setBounds(1000, 500, 200, 50);
    button[1] = new JButton("restart");
    button[1].setBounds(1000, 550, 200, 50);
    for (int i = 0; i < 2; i  ) {
      button[i].addActionListener(this);
      button[i].setFocusable(false);
      button[i].setFont(new Font("Times New Roman", Font.BOLD, 30));
      button[i].setPreferredSize(new Dimension(240, 50));
    }
    title = new JTextField("The Periodic Table of the Chemical Elements");
    title.setBounds(0, 0, 1080, 65);
    title.setEditable(false);
    title.setFont(new Font("Arial", Font.BOLD, 50));
    element = new JRadioButton[119];
    element[1] = new JRadioButton("1");
    element[1].setBounds(50, 75, 50, 50);
    elementImage[1] = new ImageIcon(getClass().getClassLoader().getResource("H.png"));
    properties[1] = new JLabel();
    element[2] = new JRadioButton("2");
    element[2].setBounds(925, 75, 50, 50);
    elementImage[2] = new ImageIcon(getClass().getClassLoader().getResource("He.png"));
    properties[2] = new JLabel();
    element[3] = new JRadioButton("3");
    element[3].setBounds(50, 125, 50, 50);
    elementImage[3] = new ImageIcon(getClass().getClassLoader().getResource("Li.png"));
    properties[3] = new JLabel();
    element[4] = new JRadioButton("4");
    element[4].setBounds(100, 125, 50, 50);
    elementImage[4] = new ImageIcon(getClass().getClassLoader().getResource("Be.png"));
    properties[4] = new JLabel();
    element[5] = new JRadioButton("5");
    element[5].setBounds(675, 125, 50, 50);
    elementImage[5] = new ImageIcon(getClass().getClassLoader().getResource("B.png"));
    properties[5] = new JLabel();
    element[6] = new JRadioButton("6");
    element[6].setBounds(725, 125, 50, 50);
    elementImage[6] = new ImageIcon(getClass().getClassLoader().getResource("C.png"));
    properties[6] = new JLabel();
    element[7] = new JRadioButton("7");
    element[7].setBounds(775, 125, 50, 50);
    elementImage[7] = new ImageIcon(getClass().getClassLoader().getResource("N.png"));
    properties[7] = new JLabel();
    element[8] = new JRadioButton("8");
    element[8].setBounds(825, 125, 50, 50);
    elementImage[8] = new ImageIcon(getClass().getClassLoader().getResource("O.png"));
    properties[8] = new JLabel();
    element[9] = new JRadioButton("9");
    element[9].setBounds(875, 125, 50, 50);
    elementImage[9] = new ImageIcon(getClass().getClassLoader().getResource("F.png"));
    properties[9] = new JLabel();
    element[10] = new JRadioButton("10");
    element[10].setBounds(925, 125, 50, 50);
    elementImage[10] = new ImageIcon(getClass().getClassLoader().getResource("Ne.png"));
    properties[10] = new JLabel();
    element[11] = new JRadioButton("11");
    element[11].setBounds(50, 175, 50, 50);
    elementImage[11] = new ImageIcon(getClass().getClassLoader().getResource("Na.png"));
    properties[11] = new JLabel();
    element[12] = new JRadioButton("12");
    element[12].setBounds(100, 175, 50, 50);
    elementImage[12] = new ImageIcon(getClass().getClassLoader().getResource("Mg.png"));
    properties[12] = new JLabel();
    element[13] = new JRadioButton("13");
    element[13].setBounds(675, 175, 50, 50);
    elementImage[13] = new ImageIcon(getClass().getClassLoader().getResource("Al.png"));
    properties[13] = new JLabel();
    element[14] = new JRadioButton("14");
    element[14].setBounds(725, 175, 50, 50);
    elementImage[14] = new ImageIcon(getClass().getClassLoader().getResource("Si.png"));
    properties[14] = new JLabel();
    element[15] = new JRadioButton("15");
    element[15].setBounds(775, 175, 50, 50);
    elementImage[15] = new ImageIcon(getClass().getClassLoader().getResource("P.png"));
    properties[15] = new JLabel();
    element[16] = new JRadioButton("16");
    element[16].setBounds(825, 175, 50, 50);
    elementImage[16] = new ImageIcon(getClass().getClassLoader().getResource("S.png"));
    properties[16] = new JLabel();
    element[17] = new JRadioButton("17");
    element[17].setBounds(875, 175, 50, 50);
    elementImage[17] = new ImageIcon(getClass().getClassLoader().getResource("Cl.png"));
    properties[17] = new JLabel();
    element[18] = new JRadioButton("18");
    element[18].setBounds(925, 175, 50, 50);
    elementImage[18] = new ImageIcon(getClass().getClassLoader().getResource("Ar.png"));
    properties[18] = new JLabel();
    element[19] = new JRadioButton("19");
    element[19].setBounds(50, 225, 50, 50);
    elementImage[19] = new ImageIcon(getClass().getClassLoader().getResource("K.png"));
    properties[19] = new JLabel();
    element[20] = new JRadioButton("20");
    element[20].setBounds(100, 225, 50, 50);
    elementImage[20] = new ImageIcon(getClass().getClassLoader().getResource("Ca.png"));
    properties[20] = new JLabel();
    element[21] = new JRadioButton("21");
    element[21].setBounds(150, 225, 50, 50);
    elementImage[21] = new ImageIcon(getClass().getClassLoader().getResource("Sc.png"));
    properties[21] = new JLabel();
    element[22] = new JRadioButton("22");
    element[22].setBounds(225, 225, 50, 50);
    elementImage[22] = new ImageIcon(getClass().getClassLoader().getResource("Ti.png"));
    properties[22] = new JLabel();
    element[23] = new JRadioButton("23");
    element[23].setBounds(275, 225, 50, 50);
    elementImage[23] = new ImageIcon(getClass().getClassLoader().getResource("V.png"));
    properties[23] = new JLabel();
    element[24] = new JRadioButton("24");
    element[24].setBounds(325, 225, 50, 50);
    elementImage[24] = new ImageIcon(getClass().getClassLoader().getResource("Cr.png"));
    properties[24] = new JLabel();
    element[25] = new JRadioButton("25");
    element[25].setBounds(375, 225, 50, 50);
    elementImage[25] = new ImageIcon(getClass().getClassLoader().getResource("Mn.png"));
    properties[25] = new JLabel();
    element[26] = new JRadioButton("26");
    element[26].setBounds(425, 225, 50, 50);
    elementImage[26] = new ImageIcon(getClass().getClassLoader().getResource("Fe.png"));
    properties[26] = new JLabel();
    element[27] = new JRadioButton("27");
    element[27].setBounds(475, 225, 50, 50);
    elementImage[27] = new ImageIcon(getClass().getClassLoader().getResource("Co.png"));
    properties[27] = new JLabel();
    element[28] = new JRadioButton("28");
    element[28].setBounds(525, 225, 50, 50);
    elementImage[28] = new ImageIcon(getClass().getClassLoader().getResource("Ni.png"));
    properties[28] = new JLabel();
    element[29] = new JRadioButton("29");
    element[29].setBounds(575, 225, 50, 50);
    elementImage[29] = new ImageIcon(getClass().getClassLoader().getResource("Cu.png"));
    properties[29] = new JLabel();
    element[30] = new JRadioButton("30");
    element[30].setBounds(625, 225, 50, 50);
    elementImage[30] = new ImageIcon(getClass().getClassLoader().getResource("Zn.png"));
    properties[30] = new JLabel();
    element[31] = new JRadioButton("31");
    element[31].setBounds(675, 225, 50, 50);
    elementImage[31] = new ImageIcon(getClass().getClassLoader().getResource("Ga.png"));
    properties[31] = new JLabel();
    element[32] = new JRadioButton("32");
    element[32].setBounds(725, 225, 50, 50);
    elementImage[32] = new ImageIcon(getClass().getClassLoader().getResource("Ge.png"));
    properties[32] = new JLabel();
    element[33] = new JRadioButton("33");
    element[33].setBounds(775, 225, 50, 50);
    elementImage[33] = new ImageIcon(getClass().getClassLoader().getResource("As.png"));
    properties[33] = new JLabel();
    element[34] = new JRadioButton("34");
    element[34].setBounds(825, 225, 50, 50);
    elementImage[34] = new ImageIcon(getClass().getClassLoader().getResource("Se.png"));
    properties[34] = new JLabel();
    element[35] = new JRadioButton("35");
    element[35].setBounds(875, 225, 50, 50);
    elementImage[35] = new ImageIcon(getClass().getClassLoader().getResource("Br.png"));
    properties[35] = new JLabel();
    element[36] = new JRadioButton("36");
    element[36].setBounds(925, 225, 50, 50);
    elementImage[36] = new ImageIcon(getClass().getClassLoader().getResource("Kr.png"));
    properties[36] = new JLabel();
    element[37] = new JRadioButton("37");
    element[37].setBounds(50, 275, 50, 50);
    elementImage[37] = new ImageIcon(getClass().getClassLoader().getResource("Rb.png"));
    properties[37] = new JLabel();
    element[38] = new JRadioButton("38");
    element[38].setBounds(100, 275, 50, 50);
    elementImage[38] = new ImageIcon(getClass().getClassLoader().getResource("Sr.png"));
    properties[38] = new JLabel();
    element[39] = new JRadioButton("39");
    element[39].setBounds(150, 275, 50, 50);
    elementImage[39] = new ImageIcon(getClass().getClassLoader().getResource("Y.png"));
    properties[39] = new JLabel();
    element[40] = new JRadioButton("40");
    element[40].setBounds(225, 275, 50, 50);
    elementImage[40] = new ImageIcon(getClass().getClassLoader().getResource("Zr.png"));
    properties[40] = new JLabel();
    element[41] = new JRadioButton("41");
    element[41].setBounds(275, 275, 50, 50);
    elementImage[41] = new ImageIcon(getClass().getClassLoader().getResource("Nb.png"));
    properties[41] = new JLabel();
    element[42] = new JRadioButton("42");
    element[42].setBounds(325, 275, 50, 50);
    elementImage[42] = new ImageIcon(getClass().getClassLoader().getResource("Mo.png"));
    properties[42] = new JLabel();
    element[43] = new JRadioButton("43");
    element[43].setBounds(375, 275, 50, 50);
    elementImage[43] = new ImageIcon(getClass().getClassLoader().getResource("Tc.png"));
    properties[43] = new JLabel();
    element[44] = new JRadioButton("44");
    element[44].setBounds(425, 275, 50, 50);
    elementImage[44] = new ImageIcon(getClass().getClassLoader().getResource("Ru.png"));
    properties[44] = new JLabel();
    element[45] = new JRadioButton("45");
    element[45].setBounds(475, 275, 50, 50);
    elementImage[45] = new ImageIcon(getClass().getClassLoader().getResource("Rh.png"));
    properties[45] = new JLabel();
    element[46] = new JRadioButton("46");
    element[46].setBounds(525, 275, 50, 50);
    elementImage[46] = new ImageIcon(getClass().getClassLoader().getResource("Pd.png"));
    properties[46] = new JLabel();
    element[47] = new JRadioButton("47");
    element[47].setBounds(575, 275, 50, 50);
    elementImage[47] = new ImageIcon(getClass().getClassLoader().getResource("Ag.png"));
    properties[47] = new JLabel();
    element[48] = new JRadioButton("48");
    element[48].setBounds(625, 275, 50, 50);
    elementImage[48] = new ImageIcon(getClass().getClassLoader().getResource("Cd.png"));
    properties[48] = new JLabel();
    element[49] = new JRadioButton("49");
    element[49].setBounds(675, 275, 50, 50);
    elementImage[49] = new ImageIcon(getClass().getClassLoader().getResource("In.png"));
    properties[49] = new JLabel();
    element[50] = new JRadioButton("50");
    element[50].setBounds(725, 275, 50, 50);
    elementImage[50] = new ImageIcon(getClass().getClassLoader().getResource("Sn.png"));
    properties[50] = new JLabel();
    element[51] = new JRadioButton("51");
    element[51].setBounds(775, 275, 50, 50);
    elementImage[51] = new ImageIcon(getClass().getClassLoader().getResource("Sb.png"));
    properties[51] = new JLabel();
    element[52] = new JRadioButton("52");
    element[52].setBounds(825, 275, 50, 50);
    elementImage[52] = new ImageIcon(getClass().getClassLoader().getResource("Te.png"));
    properties[52] = new JLabel();
    element[53] = new JRadioButton("53");
    element[53].setBounds(875, 275, 50, 50);
    elementImage[53] = new ImageIcon(getClass().getClassLoader().getResource("I.png"));
    properties[53] = new JLabel();
    element[54] = new JRadioButton("54");
    element[54].setBounds(925, 275, 50, 50);
    elementImage[54] = new ImageIcon(getClass().getClassLoader().getResource("Xe.png"));
    properties[54] = new JLabel();
    element[55] = new JRadioButton("55");
    element[55].setBounds(50, 325, 50, 50);
    elementImage[55] = new ImageIcon(getClass().getClassLoader().getResource("Cs.png"));
    properties[55] = new JLabel();
    element[56] = new JRadioButton("56");
    element[56].setBounds(100, 325, 50, 50);
    elementImage[56] = new ImageIcon(getClass().getClassLoader().getResource("Ba.png"));
    properties[56] = new JLabel();
    /*element[57]=new JRadioButton("57);
    elementImage[57]=new ImageIcon(getClass().getClassLoader().getResource("La.png"));
    properties[57]=new JLabel();*/

    periodicTableIcon = new ImageIcon(getClass().getClassLoader().getResource("periodicTableIcon.png"));
    periodicTable = new ButtonGroup();
    panel = new JPanel();
    panel.add(title);
    for (int i = 1; i < 57; i  ) {
      element[i].addActionListener(this);
      element[i].setFocusable(false);
      element[i].setIcon(elementImage[i]);
      element[i].setHorizontalTextPosition(SwingConstants.CENTER);
      element[i].setVerticalTextPosition(SwingConstants.TOP);
      periodicTable.add(element[i]);
      panel.add(element[i]);
      panel.add(properties[i]);
    }
    for (int i = 0; i < 2; i  ) {
      panel.add(button[i]);
    }
    panel.setLayout(null);
    panel.setOpaque(false);
    this.add(panel);
    this.getContentPane().setBackground(Color.WHITE);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setIconImage(periodicTableIcon.getImage());
    this.setSize(1500, 1500);
    this.setTitle("The Periodic Table of the Chemical Elements");
    this.setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
      Properties Properties = new Properties();
      for (int i = 1; i < 119; i  ) {
        if (e.getSource() == button[0] && element[i].isSelected()) {
          periodicTable.clearSelection();
        }
      }
      if (e.getSource() == button[1]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            new AtomicNumber();
            JOptionPane.showMessageDialog(null, "Program has restarted.", "Restart", JOptionPane.INFORMATION_MESSAGE);
          }
        });
      }
      if (element[1].isSelected()) {
        new Properties().setVisible(true);}
********************
//Properties.java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Properties extends JFrame {

  private static final long serialVersionUID = 1L;

  JLabel[] properties;
  JPanel propertiesPanel;
  Properties() {
    properties = new JLabel[119];
    properties[0] = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("H_properties.png")));

    propertiesPanel = new JPanel();
    propertiesPanel.add(properties[0]);
    this.add(propertiesPanel);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setResizable(false);
    this.setSize(1250, 340);

  }
}

CodePudding user response:

You should only really need on Class for all your elements.

Now, this is not an answer but merely a suggestion for perhaps a different approach (concept) on the task at hand and the best way to to describe it is to see it in action:

enter image description here

This is actually from www.britannica.com and you can play with it here. When the page displays, click on the small periodic table image so to Zoom the image. A panel is opened displaying a larger version of the periodic table and at the bottom is an Image Spinner. Scroll the image spinner to the very last image and click on it with your mouse pointer. An interactive periodic table will be displayed in the window. Now just move your mouse pointer over the periodic table elements and select the one you want to see the properties for. This is basically how I would do this but, to each their own. I just thought I would pass it along.

The periodic table itself could be a JPanel consisting of 103 JLabels in a grid type layout with each having it's own element image and other JLabels for things like the title and legend data. Doll it up the way you like. The important part would be to create a global Mouse Listener to handle mouseEntered, mouseExited (for border highlighting), and mouseClicked (to display properties) events for all element JLabels. This could perhaps even be an Inner Class, for example:

private class ElementsMouseHandler extends java.awt.event.MouseAdapter {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
         // Do whatever you like here
    }

    @Override
    public void mouseReleased(java.awt.event.MouseEvent evt) {
         // Example:

         // Ignore the right mouse button.
         if (SwingUtilities.isRightMouseButton(evt)) {
            return;
        }
        
        /* Which JLabel fired the event. You can do this also in 
           the other events below.       */
        javax.swing.JLabel source = (javax.swing.JLabel) evt.getSource();

        // Get the name placed into the element JLabel's Name property
        String elementName = source.getName();

        /* Do whatever you want here like perhaps display another JPanel
           in the window containing the properties information or the 
           image thereof related to the elementName retrieved.    */
    }

    @Override
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        // Do whatever you like here
    }

    @Override
    public void mouseExited(java.awt.event.MouseEvent evt) {
        // Do whatever you like here
    }
}

Then when in the members area of your Main class you might have something like this:

ElementsMouseHandler elementsMouseHandler;

Then just before you initialize all your element JLabels you could have something like this:

elementsMouseHandler = new ElementsMouseHandler();

And when you actually initialize your elements JLabel you could do something like this:

elementJLabel[idx].addMouseListener(elementMouseHandler);

Now the same mouse listener can handle the mouse events for all Elements JLabels. Anyways...food for thought.

  • Related