• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable size

 
Charley Zuo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My Client class extends JFram and I set dimension as:
setSize(new Dimension(1000,600));
And for JTable I set dimension as:
table.setPreferredScrollableViewportSize(new Dimension(1000, 400));
It works fine on my machine. But today I tried it on another machine and the JTable only occupied about 70% of the width of the frame. Also I noticed that some field with big width can not be displayed completely. But once I ever resize the window the JTable expands to the same size as the JFrame.
Does anybody have any experience on this?
Many thanks.
Charley
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charley,
What kind of layout manager are you using? Are you wrapping your JTable in a JScrollPane? There are a lot of factors that determine why a JTable reacts on a resize. Also you might consider using pack() on your JFrame instead of setting a fixed size. You could also get some control by listening to WindowEvents and controling behaviour there although it's generally easier to do that by the proper choice of layout managers.
Hope this helps
Michael Morris
SCJP
 
amit ahuja
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me too in JTable. you look like a savior to me..
I have used default table model and JTable and i found that ENTER key is for moving the selection down in the Jtable By default. I wanted to use enter as click to default button. But it seems i'm not able to override (or disable)the default function of enter.
I tried objectMap & inputMap and couple of such things... but nothing helped.. please help me..
thanks
amit
 
Rajesh Matti
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit- I think the enter key behaviour is perfectly all right as it is the default behaviour Sun has provided. If you still want to change it, I think , table.registerKeyboardAction() is the way to go. (I am not sure though).
ActionListener enterAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//whatever you wanna do
};
};

table.registerKeyboardAction(downAction,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),
JComponent.WHEN_FOCUSED);
Let me know.
Thanks
-Rajesh
 
Charley Zuo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Machael,
Many thanks for your reply and the pack() method solved my problem.
Just for your information that I use BoarderLayout for JFrame and I didn't wrap JTable in a JScrollPane.
Thanks again.
Charley
 
amit ahuja
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh sorry for delay,
Actually i tried that method before and that didn't help me.
Any more ideas.. it is LAST hoodUp in my assignement.. waiting on it.
Thanks
amit
 
Rajesh Matti
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amith- The following code works for me, I do not know what is happening in your code, so this is all I can do.

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
public class TestJTable {
public TestJTable() {
super();
}
public static void main(java.lang.String[] args) {
String[] columnNames = { "COL1", "COL2" };
String[][] data = { { "Data_1", "Data_2" }, {
"Data_3", "Data_4" }, {
"Data_5", "Data_6" }
};
final JFrame frame = new JFrame("JTABLE TEST");
JTable table = new JTable(data, columnNames);
ActionListener enterAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "ENTER KEY PRESSED");
};
};
table.registerKeyboardAction(
enterAction,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_FOCUSED);
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic