0

I'm trying to obtain square ( cells with the same width and height ) However I can only resize the width with the method setPreferredWidth like this:

TableColumnModel tcm = table1.getColumnModel();
tcm.getColumn(0).setPreferredWidth(100);

I tried to use the method setPreferredSize(100,100); but it doesn't work and I tried the method setRowHeight(100); but it still does not work... I'm using an internal frame, I don't know if it is the reason setRowHeight doesn't work. How can I do?

luz
  • 25
  • 7
  • setRowHeight should do the trick... have you tried calling it after loading the table model? – Erich Kitzmueller May 05 '15 at 11:03
  • I didn't use a table model, what I did was: I created a class that inherits JInternalFrame. Then I created a table like this String[] data = { {"1","2","3","4"}...... } String[] cols = {"Col 1", "Col 2","Col 3", "Col 4"}; public JTable table1 = new JTable(data, cols); and within the constructor I set SetRowHeight(100); but it didn't work – luz May 05 '15 at 11:11
  • 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). – Andrew Thompson May 05 '15 at 11:31
  • here is my code: class ShowEmployee extends JInternalFrame { String[][] data = { {"1", "2", "3","4"}, {"5", "6", "7","8"}, {"9","10","11","12"}, {"13","14","15","16"} }; String[] cols = {"Col 1", "Col 2", "Col 3","Col 4"}; public JTable table = new JTable(data, cols); public ShowEmployee() { JScrollPane scroll = new JScrollPane(table); getContentPane().add(scroll); pack(); setVisible(true); setRowHeight(100);}} – luz May 05 '15 at 11:41
  • 1) Tip: Add @ammoQ (or whoever, the `@` is important) to *notify* the person of a new comment. 2) Don't post code in comments, instead [edit the question](http://stackoverflow.com/posts/30051047/edit). 3) An uncompilable code snippet is not an MCVE. Post an *MCVE* as an edit. – Andrew Thompson May 06 '15 at 02:52

2 Answers2

1

You can override getPreferredScrollableViewportSize() like they show here and here.

private static int N = …;
…
JTable table = new JTable() {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        Dimension dim = new Dimension(
            table.getRowHeight() * N,
            table.getRowHeight() * N);
        return dim;
    }
};
Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
1

Don't extend JInternalFrame. You have NOT added any new functionality to that class. Instead you create a JInternalFrame and you add the scroll pane to the internal frame.

setRowHeight(100);

The setRowHeight(...) is a method of JTable so you need to use table.setRowHeight(100). Also, this method should be invoked when you create the table, not after the internal frame is visible.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for your answer but I have to use iJIternalFrame to make 2 JTables in the same JFrame. – luz May 05 '15 at 15:54
  • I didn't say you couldn't use a JInternalFrame. I said you should NOT `extend` JInternalFrame. – camickr May 05 '15 at 16:42