0

I am trying to load file name along with few details in the JTable, first time when I press the button, it loads fine, but for the second time when I press the same button it goes blank.

Short example:

package exampleDemo;

public class JTableExample {

private JFrame frame;
private JTable table_1;

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

public JTableExample() {
    initialize();
}

    public void showEmptyTable() {
        final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
        final String[][] emptyData = {
            {"", "", "", "", "", "", "", "", "",},
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },};
    table_1.setModel(new DefaultTableModel(emptyData, colNames));
    frame.getContentPane().add(table_1);
    JScrollPane scrollPane = new JScrollPane(table_1);
    scrollPane.setBounds(81, 162, 830, 180);
    frame.getContentPane().add(scrollPane);
};

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1002, 576);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    table_1 = new JTable();
    table_1.setBounds(81, 162, 830, 180);
    frame.getContentPane().add(table_1);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            table_1.repaint(); 
            showEmptyTable();
        }
    });
    btnNewButton.setBounds(578, 70, 89, 23);
    frame.getContentPane().add(btnNewButton);
    }
}
Java.beginner
  • 871
  • 2
  • 19
  • 37
  • 2
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) `btnSearchContent.setBounds(913, 175, 124, 23);` Java GUIs have to work on different OS', screen size, screen resolution etc. 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 Dec 01 '14 at 15:36
  • And for that ..thing beneath `Document Details` use a [`JSeparator`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JSeparator.html). – Andrew Thompson Dec 01 '14 at 15:38
  • Thanks @AndrewThompson, I was trying to post the complete code but it was giving formatted error, that's why have posted only the function, now have posted the [complete code](http://pastebin.com/B4ii3yVQ) in pastebin, thanks – Java.beginner Dec 01 '14 at 15:42
  • I didn't suggest you should post close to 500 lines of code. I suggested an ***MCVE***. Hard code some data for the table and it should be less than 150 LOC in total for this type of problem. Voting to close for lack of an MCVE. – Andrew Thompson Dec 01 '14 at 15:52
  • Thanks @AndrewThompson, I made a short example and [posted](http://pastebin.com/KJQvVQrq) in the pastebin, thanks – Java.beginner Dec 01 '14 at 16:23
  • @Java.beginner you are added JComponent to the already visible AWT/Swing GUI, you have to notify LayoutManager programatically (there isn't automatical notifier in APIs), but better should be to play with TableModel, there you ca to reset JTables view, thoeretically whatever what do you want to do – mKorbel Dec 02 '14 at 10:48
  • Thanks @mKorbel, I want fetch the file name with path from [JTree](http://i.imgur.com/iihYbeJ.png) and using 'Add Files' button and put it in JTable and keep adding the file names to the existing JTable content as and when user select from JTree, my problem when the app loads fine but when I press JButton again the table goes blank and also I want to get checkbox in last five columns in second table for I have been working with your examples, have pasted the [complete code](http://pastebin.com/ZevTA3Ee), please give me your directions on this, thank you so much for your time and help. – Java.beginner Dec 02 '14 at 11:10

1 Answers1

1

A quick glance at your code would suggest to me that you're going about this the wrong way: you're literally reconstructing a new table model each time you click the button and I suspect (although it's just a hunch at the moment) is because each time you click that button you're adding the same table via a new scrollpanel to the main JFrame - each and every time.

What you want to do is initialise the table and its model during the loading of the frame. And then if you want to clear the table, then simply get the reference to that table's model and manipulate the existing model. That's it - no need to create an entire new model; no need to add a new table via a scrollpanel.

So what I'd do in your case is add a line to your constructor:

public YourClassName() {
  initialize();
  initializeYourTable();
}

Then initializeYourTable() would look like this:

private void initSearchTable() {
    final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
    //PO: Part Order
    //LN: Line Number
    //SI: Serial Number
    //MS: Material Supplier
    //PT: Part Item 
    final String[][] emptyData = {
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},};
    table_3.setModel(new DefaultTableModel(emptyData, colNames));
    //frmViperManufacturingRecord.getContentPane().add(table_3); <-- NB this should be commented out!!
    table_3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table_3.getColumnModel().getColumn(0).setPreferredWidth(40);
    table_3.getColumnModel().getColumn(1).setPreferredWidth(290);
    table_3.getColumnModel().getColumn(2).setPreferredWidth(80);
    table_3.getColumnModel().getColumn(3).setPreferredWidth(178);
    table_3.getColumnModel().getColumn(4).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(5).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(6).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(7).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(8).setPreferredWidth(25);
    JScrollPane scrollPane = new JScrollPane(table_3);
    scrollPane.setBounds(324, 209, 713, 160);
    frmViperManufacturingRecord.getContentPane().add(scrollPane);
}

To clear the table you can use this shortcut:

private void resetTable() {
    DefaultTableModel model = (DefaultTableModel)table_3.getModel();
    model.setRowCount(0);
}

And therefore you need to change your actionPerformed:

public void actionPerformed(ActionEvent arg0) {
  table_3.repaint();
  //showEmptyTable();
  resetTable();
}
Community
  • 1
  • 1
andyroberts
  • 3,458
  • 2
  • 37
  • 40
  • JTable dosn't required call for repaint();, wrong suggestion based on poor question – mKorbel Dec 01 '14 at 18:02
  • Thanks @mKorbel, I took **arooaroo** suggestions about not to _reconstruct a new table model each time_. As you mentioned, should change the question? I cldn't find any similar answer from your previous post, thanks – Java.beginner Dec 02 '14 at 09:18
  • Thanks @mKorbel, if not repaint();, what would you suggest? thanks – Java.beginner Dec 02 '14 at 10:44
  • I agree about the repaint, but I addressed the fundamental issues rather than getting into a separate discussion about other unnecessary coding points. (The repaints weren't the cause of the original problem so I let it be.) – andyroberts Dec 02 '14 at 11:20