Results 1 to 17 of 17
- 10-12-2010, 12:34 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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
- 10-12-2010, 01:17 PM #2
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.
- 10-12-2010, 01:36 PM #3
Crossposted here: http://forums.oracle.com/forums/thre...56851&tstart=0
And here: http://www.coderanch.com/t/513508/GU...t-screen-widthLast edited by KevinWorkman; 10-12-2010 at 01:38 PM.
- 10-12-2010, 01:42 PM #4
OK Kevin, you got well ahead of me here ;)
db
- 10-12-2010, 01:48 PM #5
Hey Kevin --
JTable is not fit to the screen width. - Java Programming Forums
db
- 10-12-2010, 04:12 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-12-2010, 04:24 PM #7
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:
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.Java Code:System.out.println("Look how pretty formatted code is!");
- 10-12-2010, 04:47 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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).
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 applicationJava 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); } }); } }
- 10-12-2010, 06:13 PM #9
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:
Does that fix your problem? Or am I misunderstanding your question?Java Code://paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
- 10-13-2010, 08:56 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-13-2010, 01:20 PM #11
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.
Or you could try telling it to resize only the next column instead:Java Code:for(int i = 0; i < paramTable.getColumnCount(); i++){ paramTable.getColumnModel().getColumn(i).setPreferredWidth(200); }
Java Code:paramTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
- 10-13-2010, 01:42 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-13-2010, 01:46 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-13-2010, 01:55 PM #14
Quite possibly, but those ways are not immediately obvious to me. Is there something about setting the widths that doesn't satisfy your requirements?
- 10-13-2010, 02:27 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-13-2010, 03:03 PM #16
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.
- 10-14-2010, 09:08 AM #17
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
array.width?
By noobgrammer in forum New To JavaReplies: 2Last Post: 07-01-2010, 02:35 AM -
Width (-1) and height (-1) cannot be <= 0
By LovJava in forum AWT / SwingReplies: 5Last Post: 04-24-2010, 12:16 AM -
How get width of string?
By artemff in forum CLDC and MIDPReplies: 1Last Post: 12-13-2009, 02:52 PM -
Blank Screen while navigating from one screen to another
By mohana.krishna in forum Java ServletReplies: 0Last Post: 03-03-2009, 05:03 PM -
ComboBox Fix Width
By Azndaddy in forum New To JavaReplies: 2Last Post: 05-29-2008, 04:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks