0

I have a mainPanel ,there is a button in mainPanel, on the click of that button, textPanel becomes active ,inside textpanel ,only a comobox appears at first,when you select any item from combobox a table displayed with the data corresponding to that

mainPanel->textPanel->Panelc2->F1Table

mainPanel->textPanel->Panelc1->box2

When I do this first time it works properly ,but when I again select some item then it does not refresh the table and show the same data.The problem is not related to data,I think there is something I am missing in java swings.I am new to Swings.Please give some suggestion,If anybody can look into it

Code:

public void box2actionPerformed(ActionEvent event)
{

    try
    {
        String str=(String)box2.getSelectedItem();
        Pattern pat=Pattern.compile("//[(.)*//]");
        Matcher patMatcher=pat.matcher(str);
        Configuration conf = new Configuration();
        conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        String location="hdfs://localhost:54310/user/a.txt";
        modelF1 = new DefaultTableModel();
        F1Table=new JTable(modelF1);
        modelF1.addColumn("Data");
        System.out.println(modelF1.getRowCount());
        if (modelF1.getRowCount() > 0) {
            for (int i = modelF1.getRowCount() - 1; i > -1; i--) {
                modelF1.removeRow(i);
                System.out.println(i);
            }
        }
        //modelF1.fireTableDataChanged();

        if (modelF1.getRowCount() ==-1) {
            System.out.println("no data");
            F1Table.removeAll();
        }

        rowCount=0;
        panelC2=new JPanel();
        panelC2.repaint();
        panelC2.revalidate();
        textPanel3.revalidate();
        textPanel3.repaint();
        mainPanel.revalidate();
        mainPanel.repaint();
        Path perr=new Path(location);
        BufferedReader breader1=new BufferedReader(new InputStreamReader(fs.open(perr)));
        String line="";
        modelF1 = new DefaultTableModel();
        modelF1.fireTableDataChanged();
        F1Table=new JTable(modelF1);
        modelF1.addColumn("Data");

        if(patMatcher.find())
        {

            String patVal=str.substring(patMatcher.start(), patMatcher.end());
            System.out.println(patVal);
            while((line=breader1.readLine()) != null)
            {
                Matcher patMatcherl=pat.matcher(line);
                if(patMatcherl.find())
                {
                    String patVall=line.substring(patMatcherl.start(), patMatcherl.end());
                    System.out.println(patVall);
                    if(patVal.equals(patVall))
                    {

                         modelF1.addRow(new Object[]{line});
                    }
                }
            }
        }
        breader1.close();
        modelF1.fireTableDataChanged();
        F1scrollPanel = new JScrollPane(F1Table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        F1Table.setPreferredSize(new Dimension(1000, 450));
        F1Table.setSize(1000,450);
        F1scrollPanel.setPreferredSize(new Dimension(1000, 450));
        F1scrollPanel.setSize(1000,450);
        panelC2.setPreferredSize(new Dimension(1000, 520));
        panelC2.setSize(1000,520);
        progressBar.setValue(100);
        F1Table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        panelC2.add(F1,BorderLayout.NORTH);
        panelC2.add(F1scrollPanel,BorderLayout.SOUTH);
        textPanel3.add(panelC2,BorderLayout.CENTER);
        textPanel3.revalidate();
        textPanel3.repaint();
        mainPanel.revalidate();
        mainPanel.repaint();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public void buttonclickactionPerformed(ActionEvent event)
{

    try
    {

        String filepath=pathonconsole.getText();

        filepath=filepath.trim();
        String filename=filepath.substring(filepath.lastIndexOf('/') + 1);
        int i=0;
        textPanel3=new JPanel();
        textPanel3.setPreferredSize(new Dimension(1100,600));
        textPanel3.setSize(1100,600);
        box2=new JComboBox();
        Configuration conf = new Configuration();
        conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        String location="hdfs://localhost:54310/user/b.txt";
        Path pcor=new Path(location);
        BufferedReader breader1=new BufferedReader(new InputStreamReader(fs.open(pcor)));
        String line="";
        while((line=breader1.readLine())!=null)
        {
            box2.addItem(line);
        }
        breader1.close();
        box2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                box2actionPerformed(evt);
            }
        });
        panelC1=new JPanel();
        box2.setPreferredSize(new Dimension(150, 30));
        box2.setSize(150,30);
        panelC1.setPreferredSize(new Dimension(1100, 40));
        panelC1.setSize(1100, 40);
        panelC1.add(box2,BorderLayout.NORTH);
        FlowLayout obj=new FlowLayout(FlowLayout.CENTER);
        textPanel3.setLayout(obj);
        textPanel3.add(panelC1);
        mainPanel.add(textPanel3,BorderLayout.CENTER);
        System.out.println("added in main panel");
        textPanel3.setVisible(true);
        System.out.println("done");


    }
    catch (Exception e) {

        e.printStackTrace();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ek1234
  • 407
  • 3
  • 7
  • 17
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). 2) Rather than adding/removing components on user action, use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html), as shown [here](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005). – Andrew Thompson Aug 24 '14 at 16:41
  • If I use the card layout,then I have to keep all model name already created.Number of items in combo box is huge and also dynamically added at button click. – Ek1234 Aug 25 '14 at 04:28
  • *"If I use the card layout,then I have to keep all model name already created"* If all the components for each 'model' are the same, don't create new components at all, but simply refresh the state of the existing components. – Andrew Thompson Aug 25 '14 at 04:45
  • ..but as I mentioned, for better help, post an MCVE (as opposed to an uncompilabe code snippet). – Andrew Thompson Aug 25 '14 at 04:46
  • See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Aug 25 '14 at 04:47
  • 1
    All the component for the model are same,I was refreshing it with modelF1.fireTableDataChanged();,but it was not working,Is there any other way too?I will post the MCVE form of the code.This is part of main compiled code...but it was too big.I will make it compilable. – Ek1234 Aug 25 '14 at 05:01
  • *"I will make it compilable."* Let me know when it is, as I'm unable to offer best advice until I see it. – Andrew Thompson Aug 25 '14 at 05:04

2 Answers2

1
panelC2=new JPanel();
panelC2.repaint();
panelC2.revalidate();
textPanel3.revalidate();
textPanel3.repaint();
mainPanel.revalidate();
mainPanel.repaint();

The above code is unnecessary. First you create a new panel, but you don't add anything to the panel and you don't add the panel to any other component so there is no need to revalidate()/repaint() the panel. Same for the textPanel3 and mainPanel. You haven't made any changes so you don't need the code.

Whenever you do dynamically add/remove components then you only need to revalidate()/repaint() the top level container. In this case that would be the mainPanel, because it will then revalidate() the textPanel3 and panelC2.

F1scrollPanel = new JScrollPane(F1Table, ...)

Another general comment is don't keep adding/removing components for a panel. The better approach is to create the scrollpane with an empty JTable. Then when you want to change the data in the table you just create a new TableModel and update the existing table by using the table.setModel(...) method. This way the table will automatically repaint itself and you don't even have to worry about revalidate() and repaint().

camickr
  • 321,443
  • 19
  • 166
  • 288
  • If I create a model everytime on comboBox selection ,I need to create model name dynamically.list of items in combo box are too many. – Ek1234 Aug 25 '14 at 04:18
  • I tried creating model name dynamically.but it did not work.When we use "new",Example, modelF1 = new DefaultTableModel();...then it should refresh the value of variable and allocate new memory to it?but it is also not working if i leave the code like that. – Ek1234 Aug 25 '14 at 04:25
0

Thanks Andrew and Camickr,I tried making the compilable solution, In the process of making it I got the solution. As Camickr said,I only need to update model values.Earlier I was defining all panelc2,table and scrollpane in box2actionperform method.So every time the select index change event fires it need to recreate everything or some problem arise in that part.I have changed the definition location only,I defined the panelc2=new panelc2() and all other component in buttonclickactionPerformed function.I am only updating the values in box2actionperform method.That solved the purpose.Thanks a lot to both of you.:)

Ek1234
  • 407
  • 3
  • 7
  • 17