0

I am having an issue with JTables I know my code is a little hard to follow, it's also a little jumbled around because it's coming from a fairly big program. And yes I just learned about the java naming convention in which you don't start a variable with an uppercase letter.

final JFrame Menu = new JFrame("Crime Database 2013");
    Dimension screenSize0 = Menu.getToolkit().getScreenSize();
    Menu.setBounds(screenSize0.width / 4, screenSize0.height / 4,
            screenSize0.width / 2, screenSize0.height / 2);
    Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Menu.setVisible(true);
    JPanel options = new JPanel(new GridBagLayout());
    GridBagConstraints a = new GridBagConstraints();
    Menu.add(options);
    JButton show = new JButton("Show all records");
    a.gridx = 0;
    a.gridy = 1;
    options.add(show, a);

    final JFrame Show = new JFrame("Crime Database 2013 - Show Records");
        Dimension screenSize3 = Show.getToolkit().getScreenSize();
        Show.setBounds(screenSize3.width/3 - 250, screenSize3.height/7,
                screenSize3.width - 150, screenSize3.height-200);
        Show.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        final JPanel data = new JPanel(new FlowLayout());
        JPanel sortselect = new JPanel(new GridBagLayout());
        GridBagConstraints h = new GridBagConstraints();
        g.gridx = 0;
        g.gridy = 2;
        Show.add(sortselect, g);
        g.gridx = 0;
        g.gridy = 0;
        g.gridheight = 2;
        g.gridwidth = 5;
        Show.add(data, g);
        JLabel label = new JLabel("Sort options");
        JRadioButton none = new JRadioButton("No Sort",true);
        JLabel frname = new JLabel("By First Name");
        JRadioButton frnameup = new JRadioButton("First name - Alphabetical");
        JRadioButton frnamedn = new JRadioButton("First name - Reverse-Alphabetical");
        JLabel lsname = new JLabel("By Last Name");
        JRadioButton lsnameup = new JRadioButton("Last name - Alphabetical");
        JRadioButton lsnamedn = new JRadioButton("Last name - Reverse-Alphabetical");
        JLabel byage = new JLabel("By Age");
        JRadioButton ageup = new JRadioButton("Age - Increasing");
        JRadioButton agedn = new JRadioButton("Age - Decreasing");
        JLabel bycrime = new JLabel("By Crime");
        JRadioButton crimeup = new JRadioButton("Crime - Alhabetically");
        JRadioButton crimedn = new JRadioButton("Crime - Reverse-Alphabetical");
        JLabel year = new JLabel("By Year of release");
        JRadioButton yearup = new JRadioButton("Expected Year of Release - First");
        JRadioButton yeardn = new JRadioButton("Expected Year of Release - Last");
        ButtonGroup sortgroup = new ButtonGroup();
        sortgroup.add(none);
        sortgroup.add(frnameup);
        sortgroup.add(frnamedn);
        sortgroup.add(lsnameup);
        sortgroup.add(lsnamedn);
        sortgroup.add(ageup);
        sortgroup.add(agedn);
        sortgroup.add(crimeup);
        sortgroup.add(crimedn);
        sortgroup.add(yearup);
        sortgroup.add(yeardn);
        h.insets = new Insets(10,10,10,10);
        h.gridx = 0;
        h.gridy = 2;
        sortselect.add(frname, h);
        h.gridx = 0;
        h.gridy = 3;
        sortselect.add(frnameup, h);
        h.gridx = 0;
        h.gridy = 4;
        sortselect.add(frnamedn, h);
        h.gridx = 1;
        h.gridy = 2;
        sortselect.add(lsname, h);
        h.gridx = 1;
        h.gridy = 3;
        sortselect.add(lsnameup, h);
        h.gridx = 1;
        h.gridy = 4;
        sortselect.add(lsnamedn, h);
        h.gridx = 2;
        h.gridy = 2;
        sortselect.add(byage, h);
        h.gridx = 2;
        h.gridy = 3;
        sortselect.add(ageup, h);
        h.gridx = 2;
        h.gridy = 4;
        sortselect.add(agedn, h);
        h.gridx = 3;
        h.gridy = 2;
        sortselect.add(bycrime, h);
        h.gridx = 3;
        h.gridy = 3;
        sortselect.add(crimeup, h);
        h.gridx = 3;
        h.gridy = 4;
        sortselect.add(crimedn, h);
        h.gridx = 4;
        h.gridy = 2;
        sortselect.add(year, h);
        h.gridx = 4;
        h.gridy = 3;
        sortselect.add(yearup, h);
        h.gridx = 4;
        h.gridy = 4;
        sortselect.add(yeardn, h);
        h.gridwidth = 5;
        h.gridheight = 1;
        h.gridx = 0;
        h.gridy =0;
        sortselect.add(label, h);
        h.gridx = 0;
        h.gridy = 1;
        sortselect.add(none, h);
            show.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e1) {
                            Menu.setVisible(false);
                            int entries = 0;
                            BufferedReader lines = null;
                            try {
                                lines = new BufferedReader(new FileReader("L:\\workspace\\new java\\sources\\c-database.txt"));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            try {
                                while (lines.readLine() != null) {
                                    entries++;
                                }
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                            BufferedReader crimeinfo = null;
                            try {
                                crimeinfo = new BufferedReader(new FileReader("L:\\workspace\\new java\\sources\\c-database.txt"));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            String namelist[] = new String[entries];
                            String agelist[] = new String[entries] ;
                            String crimelist[] = new String[entries];
                            String release[] = new String[entries];
                            for (int i = 0; i < entries; i++) {
                                String full = null;
                                try {
                                    full = crimeinfo.readLine();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                String split[] = full.split(",");
                                namelist[i] = split[0];
                                agelist[i] = split[1];
                                crimelist[i] = split[2];
                                release[i] = split[3];
                            }
                            String firstnamelist[] = new String[entries];
                            String lastnamelist[] = new String[entries];
                            for (int i = 0; i < entries; i++) {
                                String namesplit[] = namelist[i].split(" ");
                                firstnamelist[i] = namesplit[0];
                                lastnamelist[i] = namesplit[1];
                            }
                            final String[] headers = {"First Name",
                                                "Last Name",
                                                "Age",
                                                "Crime committed",
                                                "Expected Year of Release"
                            };
                            final String[][] crimedata = new String[entries][5];
                            for (int i = 0; i < entries; i++) {
                                crimedata[i][0] = firstnamelist[i];
                                crimedata[i][1] = lastnamelist[i];
                                crimedata[i][2] = agelist[i];
                                crimedata[i][3] = crimelist[i];
                                crimedata[i][4] = release[i];
                                };
                            for (int i = 0; i < entries; i++) {
                                for (int j = 0; j < 5; j++) {
                                    System.out.println(headers[j]);
                                    System.out.println(crimedata[i][j]);
                                }
                            }
                            final JTable crimetable = new JTable(crimedata, headers);
                            JScrollPane scrollpane = new JScrollPane(crimetable);
                            crimetable.setAutoCreateRowSorter(true);
                            data.add(scrollpane);
                            Show.setVisible(true);
                        }
                    }
                );

I did just put this code here into eclipse and then took out all the radio buttons, and it sorta worked. Although I am not sure why

  • 1
    `And yes I just learned about the java naming convention in which you don't start a variable with an uppercase letter` - and yet your code still does not follow conventions. Why should we look at it when you can't even take 1 minute to do a global change all on the code? Not only that you still haven't posted a SSCCE as you were asked to do in your last posting on this topic. Since you can't be bothered to make our live easier, I will continue to ignore your questions. I'm amazed others continue to help. – camickr Apr 17 '13 at 22:07

2 Answers2

3
  1. JTable can't returns proper Dimension or PreferredSize, there are three ways

  2. table.setPreferredScrollableViewportSize(table.getPreferredSize()); but notice for small JTables with a few Rows and Columns too

  3. to calculate desired size for (part) of Columns and (part) Rows too, then pass this Dimension in form table.setPreferredScrollableViewportSize(new Dimension(x, y));

  4. override getPreferredSize for JScrollPane

  5. then JFrame.pack(before JFrame.setVisible(true)) to calculate desired Size on the screen

  6. JPanel has FlowLayout implemented in API, I'd to suggest to change to BorderLayout, then JScrollPane in CENTER area can fill whole (available) area and will be resizable with JFrame, not possible to resize JComponent (together with its container) layed by FlowLayout

  7. have to call data.revalidate(), data.repaint() and Show.pack() as last code lines instead of (remove this code line) Show.setVisible(true);

  8. rename Show to myFrame and show to myButton

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Override `getPreferredScrollableViewportSize`, for [example](http://stackoverflow.com/a/14429388/230513). – trashgod Apr 17 '13 at 19:09
  • @trashgod what exactly does the setpreferredScrollableViewpotSize do? Does it just force the table to always be that size? what is the whole pack thing? I've never seen that before. – Kieran Ramnarine Apr 17 '13 at 20:38
  • And how do I go about Overriding things? – Kieran Ramnarine Apr 17 '13 at 20:39
  • @trashgod So I threw in crimetable.setPreferredScrollableViewportSize(new Dimension(x,y)); but when I change the values of that then the whole JFrame changes depending on my x and y dimensions. Also, I tried taking out the Show.setVisible(true); but nothing showed up, so I put it back in. I have yet to add the JScrollPane's PrefferedSize and change the layout, are those the factors I am missing – Kieran Ramnarine Apr 19 '13 at 01:03
  • @KieranRamnarine: I'm not sure. Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Apr 19 '13 at 01:14
  • @trashgod Thanks for the help but I just decided to add a new JFrame with all the radio buttons, that seems to slove my problem – Kieran Ramnarine Apr 19 '13 at 17:46
2

What exactly does the setPreferredScrollableViewportSize do? Does it just force the table to always be that size? What is the whole pack thing?

The getPreferredScrollableViewportSize() method is defined in the Scrollable interface, discussed in Implementing a Scrolling-Savvy Client. Rather than setting the preferred size, you can override getPreferredScrollableViewportSize() to change the default. Making the height a multiple of getRowHeight() is illustrated here. More on preferred size may be found here. Conveniently, the pack() method "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045