Results 1 to 17 of 17
  1. #1
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default JTable is not fit to the screen width.

    JTable is not displayed to the full width of the screen.
    I am using AUTO_RESIZE_OFF option in setAutoResizeMode(JTable.AUTO_RESIZE_OFF); API.
    It makes the JTable to shrink fit to the screen and not fit to the entire width of the screen.

    Any thoughts please to make the JTable to fit completely to the width of the screen?


    Thanks in advance,
    Rajakumar

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    What container is the JTable in? What layout are you using? The best way to answer these questions is to post a short piece of runnable code that demonstrates what you're doing.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Last edited by KevinWorkman; 10-12-2010 at 01:38 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    OK Kevin, you got well ahead of me here ;)

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  6. #6
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default JTable is not fit to the screen width.

    Hi KevinWorkman,
    Thanks a lot for spending your time for me.
    I relatively new to this forum practice and hardly I posted 2 or 3 threads so far. I feel sorry for my unfair practice. I continue only on this forum.

    I put the JTable in JScrollPane and I am using BorderLayout.
    Please find below piece of code which demonstrates this problem.

    public class JTableIssue extends JTabbedPane {
    JScrollPane paramTableScrollPane = null;
    DefaultTableModel dataModel = new DefaultTableModel();
    JTable paramTable = new JTable(dataModel);

    JTableIssue() {
    JButton button1 = new JButton("Button One");

    paramTable.getTableHeader().setReorderingAllowed(f alse);
    paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_OF F);

    JPanel tab1 = new JPanel();
    tab1.setLayout(new BorderLayout());

    tab1.add(createTable(), BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.add(button1);
    tab1.add(buttonPanel, BorderLayout.PAGE_END);

    this.addTab("Tab no One", null, tab1, "Displays tab no One");
    }

    public JScrollPane createTable() {
    String rowData2[][] = {
    { "001", "vinod", "Bihar", "India", "Biology", "65", "First" },
    { "002", "Raju", "ABC", "Kanada", "Geography", "58", "second" },
    { "003", "Aman", "Delhi", "India", "computer", "98",
    "Dictontion" },
    { "004", "Ranjan", "Bangloor", "India", "chemestry", "90",
    "Dictontion" } };

    String tableColNames2[] = { "Roll", "Name", "State", "country", "Math",
    "Marks", "Grade" };

    dataModel.setDataVector(rowData2, tableColNames2);
    paramTableScrollPane = new JScrollPane(paramTable);
    return paramTableScrollPane;
    }

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame("My Application");
    JSplitPane splitPane = new JSplitPane(
    JSplitPane.VERTICAL_SPLIT, null, new JTableIssue());
    splitPane.setOneTouchExpandable(true);
    Dimension screenSize = Toolkit.getDefaultToolkit()
    .getScreenSize();
    f.setSize(screenSize);
    f.getContentPane().add(splitPane);
    f
    .setExtendedState(f.getExtendedState()
    | JFrame.MAXIMIZED_BOTH);
    f.setVisible(true);
    }
    });
    }
    }


    The problem is that the table is not spread across the entire screen width and the last column (i.e the Grade column) is positioned in the middle of the screen whenever the table is displayed. I want the last column should align with right edge of the frame or the screen without changing the rest of the behaviour of the application.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    When posting code, make sure you use the code tags. Just highlight all of your code and press the code button (it looks like a # symbol). Your code will come out looking like this:

    Java Code:
    System.out.println("Look how pretty formatted code is!");
    The code you posted already has lost its formatting already, so make sure you paste it in from your code editor. Also, make sure your code is runnable- simple spelling and syntax errors will cause people to not want to help you.

  8. #8
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default JTable is not fit to the screen width.

    Hi KevinWorkman,
    I directly copied the runnable code from Eclipse editor and reposting it with formatting the code using # icon(earlier I am not aware of this # icon code formatting feature).

    Java Code:
    public class JTableIssue extends JTabbedPane {
    	JScrollPane paramTableScrollPane = null;
    	DefaultTableModel dataModel = new DefaultTableModel();
    	JTable paramTable = new JTable(dataModel);
    
    	JTableIssue() {
    		JButton button1 = new JButton("Button One");
    
    		paramTable.getTableHeader().setReorderingAllowed(false);
    		paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
    		JPanel tab1 = new JPanel();
    		tab1.setLayout(new BorderLayout());
    
    		tab1.add(createTable(), BorderLayout.CENTER);
    
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new FlowLayout());
    		buttonPanel.add(button1);
    		tab1.add(buttonPanel, BorderLayout.PAGE_END);
    
    		this.addTab("Tab no One", null, tab1, "Displays tab no One");
    	}
    
    	public JScrollPane createTable() {
    		String rowData2[][] = {
    				{ "001", "vinod", "Bihar", "India", "Biology", "65", "First" },
    				{ "002", "Raju", "ABC", "Kanada", "Geography", "58", "second" },
    				{ "003", "Aman", "Delhi", "India", "computer", "98",
    						"Dictontion" },
    				{ "004", "Ranjan", "Bangloor", "India", "chemestry", "90",
    						"Dictontion" } };
    
    		String tableColNames2[] = { "Roll", "Name", "State", "country", "Math",
    				"Marks", "Grade" };
    
    		dataModel.setDataVector(rowData2, tableColNames2);
    		paramTableScrollPane = new JScrollPane(paramTable);
    		return paramTableScrollPane;
    	}
    
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame f = new JFrame("My Application");
    				JSplitPane splitPane = new JSplitPane(
    						JSplitPane.VERTICAL_SPLIT, null, new JTableIssue());
    				splitPane.setOneTouchExpandable(true);
    				Dimension screenSize = Toolkit.getDefaultToolkit()
    						.getScreenSize();
    				f.setSize(screenSize);
    				f.getContentPane().add(splitPane);
    				f
    						.setExtendedState(f.getExtendedState()
    								| JFrame.MAXIMIZED_BOTH);
    				f.setVisible(true);
    			}
    		});
    	}
    }
    The problem is that the table is not spread across the entire screen width and the last column (i.e the Grade column) is positioned in the middle of the screen whenever the table is displayed. I want the last column should align with right edge of the frame or the screen without changing the rest of the behaviour of the application

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Thanks for reposting your formatted code. So you just want the JTable to be fill the entire width? Commenting out this line seems to work for me:
    Java Code:
    //paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    Does that fix your problem? Or am I misunderstanding your question?

  10. #10
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Commenting "paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_O FF);" line of code causes all other columns width are getting adjusted when I try to change the width of one particular column.

    What I want is whenever I increase the column width say by dragging the columns towards right side, it should not adjust the other column's width and at the same time the other columns can hide in the right side of the screen by moving the columns themselves towards right side of the screen. Now the horizontal scroll bar should be created to view the hidden columns. Hope you got the picture of my problem now.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    It sounds like you should leave that line commented out (or deleted) and then just set the width of each column. Experiment to find a value that works for you.
    Java Code:
    for(int i = 0; i < paramTable.getColumnCount(); i++){
       paramTable.getColumnModel().getColumn(i).setPreferredWidth(200);
    }
    Or you could try telling it to resize only the next column instead:
    Java Code:
    paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

  12. #12
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Smile

    I am doing your first suggestion.
    Here I divide the screen width by no of columns to set the width equally for all columns, so that table can fit to the enitre screen width.

    Thank you so much for spending your precise time for me and suggestions.

  13. #13
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    KevinWorkman,
    Is it possible to solve this problem using JViewPort class or any other alternative solutions?
    Please let me know if you have any alternate ideas.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Quite possibly, but those ways are not immediately obvious to me. Is there something about setting the widths that doesn't satisfy your requirements?

  15. #15
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    KevinWorkman,
    Setting the width is satisfying my requirement.
    I thought it is better to have some API to do this job, that is why I asked you for any alternate methods.

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    As far as I see it, you did use the standard Java API for exactly what it was designed. Somebody with more experience might know another way to accomplish your goal, but that's how I would do it.

  17. #17
    rajakumarjk is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. array.width?
    By noobgrammer in forum New To Java
    Replies: 2
    Last Post: 07-01-2010, 02:35 AM
  2. Width (-1) and height (-1) cannot be <= 0
    By LovJava in forum AWT / Swing
    Replies: 5
    Last Post: 04-24-2010, 12:16 AM
  3. How get width of string?
    By artemff in forum CLDC and MIDP
    Replies: 1
    Last Post: 12-13-2009, 02:52 PM
  4. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM
  5. ComboBox Fix Width
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 05-29-2008, 04:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •