1

I have created the UI as follows in the below code.I have a serial tab and relay tab and also add buttons in them.I also have 6 buttons numbered 1-6. I want to add values to the jtable.When serial port tab add is clicked it should added when any one of the button in selected.how when relay tab add is clicked same thing should be continued.

My Doubt is:

If button 1 is pressed and values are added and again When button 2 is pressed table should be made empty and new values should be allowed to add to the table and again if i click back button 1 is clicked it should display values which were added to button 1 previously and also allowed to add new values and this should happen the same with all the buttons.

I tried using hash map, list but I am unable to get the best way for this approach.

My code is:

  buttonone=new JButton("1");
     buttonone.setBounds(80, 40, 50, 35);
     buttonone.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);

        }
    });
     buttonpanel.add(buttonone);

     buttontwo=new JButton("2");
     buttontwo.setBounds(180, 40, 50, 35);
     buttontwo.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);
        }
    });
     buttonpanel.add(buttontwo);

     buttonthree=new JButton("3");
     buttonthree.setBounds(80, 100, 50, 35);
     buttonthree.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);
        }
    });
     buttonpanel.add(buttonthree);

     buttonfour=new JButton("4");
     buttonfour.setBounds(180, 100, 50, 35);
     buttonfour.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);
        }
    });
     buttonpanel.add(buttonfour);

     buttonfive=new JButton("5");
     buttonfive.setBounds(80, 160, 50, 35);
     buttonfive.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);
        }
    });
     buttonpanel.add(buttonfive);

     buttonsix=new JButton("6");
     buttonsix.setBounds(180, 160, 50, 35);
     buttonsix.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            buttonid=Integer.parseInt(s);
        }
    });
     buttonpanel.add(buttonsix);

     digitalpanel=new JPanel();
     digitalpanel.setLayout(null);
     digitalpanel.setBounds(430, 30, 380, 70);
     digitalpanel.setBorder(BorderFactory.createTitledBorder("Digital Input"));
     contentPane.add(digitalpanel);

     digitalbtn1=new JButton("Input 1");
     digitalbtn1.setBounds(80, 30, 80, 25);
     digitalbtn1.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            if(s.equals("Input 1"))
            {
            buttonid=7;
            }
        }
    });
     digitalpanel.add(digitalbtn1);

     digitalbtn2=new JButton("Input 2");
     digitalbtn2.setBounds(220, 30, 80, 25);
     digitalbtn2.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=e.getActionCommand();
            if(s.equals("Input 2"))
            {
            buttonid=8;
            }
        }
    });
     digitalpanel.add(digitalbtn2);

     relaytab = new JPanel();
     relaytab.setLayout(null);
     String onoff[]={"ON","OFF"};
     String time[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16",
                    "17","18","19","20","21","22","23","24","25","26","27","28","29","30","31",
                    "32","33","34","35","36","37","38","39","40","41","42","43","44","45","46",
                    "47","48","49","50","51","52","53","54","55","56","58","59","60","61","62",
                    "63","64","65","66","67","68","69","70","71","72","73","74","75","76","77",
                    "78","79","80","81","82","83","84","85","86","87","88","89","90","91","92",
                    "93","94","95","96","97","98","99","100","101","102","103","104","105","106",
                    "107","108","109","110","111","112","113","114","115","116","117","118","119",
                    "120","121","122","123","124","125","126","127","128","129","130","131","132",
                    "133","134","135","136","137","138","139","140","141","142","143","144","145",
                    "146","147","148","149","150","151","152","153","154","155","156","157","158",
                    "159","160","161","162","163","164","165","166","167","168","169","170","171",
                    "172","173","174","175","176","177","178","179","180","181","182","183","184",
                    "185","186","187","188","189","190","191","192","193","194","195","196","197",
                    "198","199","200","201","202","203","204","205","206","207","208","209","210",
                    "211","212","213","214","215","216","217","218","219","220","221","222","223",
                    "224","225","226","227","228","229","230","231","232","233","234","235","236",
                    "237","238","239","240","241","242","243","244","245","246","247","248","249",
                    "250","251","252","253","254","255",};

     relaylabel1=new JLabel("1");
     relaylabel1.setBounds(35, 47, 30, 30);
     relaylabel1.setFont(new Font("serif",Font.BOLD,20));
     relaytab.add(relaylabel1);

     relay1combo=new JComboBox<String>(onoff);
     relay1combo.setBounds(60,50,100,25);
     relay1combo.setSelectedIndex(-1);
     relaytab.add(relay1combo);

     relay1field=new JComboBox<String>(time);
     relay1field.setBounds(170,50,80,26);
     relay1field.setSelectedIndex(0);
     relaytab.add(relay1field);

     relayadd1=new JButton("Add");
     relayadd1.setBounds(260, 50, 70, 25);
     relayadd1.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // TODO Auto-generated method stub
             if(buttonid == 1)
               {

                   model.addRow(new Object[]{relay1combo.getSelectedItem().toString(),relay1field.getSelectedItem().toString()});                     

               }
        }
    });
     relaytab.add(relayadd1);

     relaylabel2=new JLabel("2");
     relaylabel2.setBounds(35, 96, 30, 30);
     relaylabel2.setFont(new Font("serif",Font.BOLD,20));
     relaytab.add(relaylabel2);

     relay2combo=new JComboBox<String>(onoff);
     relay2combo.setBounds(60,100,100,25);
     relay2combo.setSelectedIndex(-1);
     relaytab.add(relay2combo);

     relay2field=new JComboBox<String>(time);
     relay2field.setBounds(170,100,80,26);
     relay2field.setSelectedIndex(0);;
     relaytab.add(relay2field);


     relayadd2=new JButton("Add");
     relayadd2.setBounds(260, 100, 70, 25);
     relaytab.add(relayadd2);

     relaylabel3=new JLabel("3");
     relaylabel3.setBounds(35, 146, 30, 30);
     relaylabel3.setFont(new Font("serif",Font.BOLD,20));
     relaytab.add(relaylabel3);

     relay3combo=new JComboBox<String>(onoff);
     relay3combo.setBounds(60,150,100,25);
     relay3combo.setSelectedIndex(-1);
     relaytab.add(relay3combo);

     relay3field=new JComboBox<String>(time);
     relay3field.setBounds(170,150,80,26);
     relay3field.setSelectedIndex(0);
     relaytab.add(relay3field);


     relayadd3=new JButton("Add");
     relayadd3.setBounds(260, 150, 70, 25);
     relaytab.add(relayadd3);

     relaylabel4=new JLabel("4");
     relaylabel4.setBounds(35, 196, 30, 30);
     relaylabel4.setFont(new Font("serif",Font.BOLD,20));
     relaytab.add(relaylabel4);

     relay4combo=new JComboBox<String>(onoff);
     relay4combo.setBounds(60,200,100,25);
     relay4combo.setSelectedIndex(-1);
     relaytab.add(relay4combo);         

     relay4field=new JComboBox<String>(time);
     relay4field.setBounds(170,200,80,26);
     relay4field.setSelectedIndex(0);
     relaytab.add(relay4field);        

     relayadd4=new JButton("Add");
     relayadd4.setBounds(260, 200, 70, 25);
     relaytab.add(relayadd4);

     serialporttab = new JPanel();
     serialporttab.setLayout(null);

     serialportlabel=new JLabel("Select Serial Port");
     serialportlabel.setBounds(18, 77, 100, 30);
     serialporttab.add(serialportlabel);

     String ports[]={"Port 1","Port 2","Port 3","Port 4"};
     serialportcombobox=new JComboBox<String>(ports);
     serialportcombobox.setBounds(125, 80, 220, 25);
     serialportcombobox.addActionListener(new ActionListener() 
     {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            String s=serialportcombobox.getSelectedItem().toString();
            HashMap<String, String> mainhash=seriallist.get(s);             
            if(serialportcommandcombo.getItemCount() == 0)
            {
                List<String> list=new ArrayList<String>();
                for(String s1:mainhash.values())
                {
                    list.add(s1);
                }
                for(int i=0;i<list.size();i++)
                {
                serialportcommandcombo.addItem(list.get(i));
                }
            }
            else
            {
                serialportcommandcombo.removeAllItems();
                List<String> list=new ArrayList<String>();
                 for(String s1:mainhash.values())
                    {
                        list.add(s1);
                    }
                 for(int i=0;i<list.size();i++)
                    {
                    serialportcommandcombo.addItem(list.get(i));
                    }
            }
        }
    });                 
     serialporttab.add(serialportcombobox);

     commandselection=new JLabel("Select command");
     commandselection.setBounds(18, 137, 100, 30);
     serialporttab.add(commandselection);

     serialportcommandcombo=new JComboBox<String>();
     serialportcommandcombo.setBounds(125, 140, 220, 25);
     serialportcommandcombo.setSelectedIndex(-1);
     serialporttab.add(serialportcommandcombo);

     serialadd=new JButton("Add");
     serialadd.setBounds(230, 260, 100, 25);  
     serialadd.addActionListener(new ActionListener()
     {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // TODO Auto-generated method stub
           if(serialportcommandcombo.getSelectedItem()==null)
           {
             JOptionPane.showMessageDialog(contentPane,"Select command is empty,Please select the command", "Error", JOptionPane.ERROR_MESSAGE);
           }
           else if(serialportcommandcombo.getSelectedItem()!=null)
           {
               if(buttonid == 1)
               {                       
                   model.addRow(new Object[]{serialportcombobox.getSelectedItem().toString(),serialportcommandcombo.getSelectedItem().toString()});                   

                   for(int i=0;i<model.getRowCount();i++)
                   {
                       list1.add((String) model.getValueAt(i, 0));
                   }
                   System.out.println(list1);
               }

           }
        }
    });
     serialporttab.add(serialadd);

     tabbedpane=new JTabbedPane(JTabbedPane.TOP);
     tabbedpane.setBounds(30, 300, 370, 340);
     tabbedpane.addTab("Serial Port",serialporttab);
     tabbedpane.addTab("Relay",relaytab);
     tabbedpane.setBorder(BorderFactory.createTitledBorder(""));
     contentPane.add(tabbedpane);       

     eventlist=new JPanel();
     eventlist.setBounds(430, 110, 380, 490);
     eventlist.setLayout(null);
     eventlist.setBorder(BorderFactory.createTitledBorder("Event List"));
     contentPane.add(eventlist);

     String[] columnnames={"Type","Data"};
     model = new DefaultTableModel(columnnames, 0);
     eventtabel=new JTable(model);
     eventtabel.setRowHeight(20);
     JScrollPane scroll=new JScrollPane(eventtabel);
     scroll.setBounds(10, 20, 360, 460);
     eventlist.add(scroll);

Which method is the good way to follow?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wint
  • 13
  • 4
  • Sounds like you'll want a `TableModel` per button – MadProgrammer Aug 18 '15 at 05:53
  • can i use tablebutton per button.If yes how can i do it – wint Aug 18 '15 at 05:58
  • Yes, you can use a `TableModel` per button, switching them out when you press them, although I might consider using a `JTabbedPane` for the same effect but with a richer user experience. Start by looking at [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). You would need to create a `TableModel` for each button you have on the screen, maybe keeping them in some kind of `Map`, so when a button is clicked, you'd just need the instance of the button and you'd be able to look up the associated `TableModel` – MadProgrammer Aug 18 '15 at 06:29
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). E.G. as far as I understand, an MCVE would require 2 buttons, rather than 6. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 18 '15 at 06:30
  • @MadProgrammer if i use map, and if i want enter same values in the hashmap the value is getting overritted and old value is getting vanished – wint Aug 18 '15 at 06:35
  • No, you would use the `Map` to create a link between the `JButton` and the `TableModel`, the `TableModel` would still hold the data – MadProgrammer Aug 18 '15 at 06:36
  • can u please provide me a example so that i can understand better – wint Aug 18 '15 at 06:42
  • See [`SwapTableModel`](http://stackoverflow.com/a/8260663/230513). – trashgod Aug 18 '15 at 08:33

0 Answers0