Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple JFrame problem  RSS feed

 
Daniel Roach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application that uses a JFrame. It's constructor creates a menubar. From the actionperformed method, a filechooser is used to select a file, which is passed to the constructor of a Tablemodel class, which then
creates a table in a scrollpane. How do I get the table to be visible.
getContentPane().add(pane); does not work. Is there a better approach
Thanks in advance
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i think u need to set the size of the table first.
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
i am not sure,may be iam wrong also...
Raj
 
Daniel Roach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice.
Here is the code I have been using. The problem still exists though, any other suggestions.
Thanks in advance
Dan
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
/**
build a table from a table model.
*/
public class DanSpreadII
{
public static void main(String[] args)
{
JFrame frame = new DanSpreadFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
/**
This frame contains the DanSpread table.
*/
class DanSpreadFrame extends JFrame
{
public DanSpreadFrame()
{
super("SpreadSheet");
setSize(600,600);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);

}
protected JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File");
JMenuItem open = new JMenuItem("Open file");
open.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
int r = chooser.showOpenDialog(DanSpreadFrame.this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();

TableModel model = new DanSpreadData(f);
JTable table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
table.setPreferredScrollableViewportSize(new Dimension(150,150));
frame.getContentPane().add(pane);
}
});
mFile.add(open);
mFile.addSeparator();
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
mFile.add(exit);
menuBar.add(mFile);
return menuBar;
}
private static final int WIDTH = 1800;
private static final int HEIGHT = 300;
}
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
i just tried to compile ur code and it was not compiling as there was no 'DanSpreadData' class.
ie the TableModel u r using.Any way,i got it compiled using the default TableModel.
can u send me the code of the table model also,so that i can test it fully..
Raj
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!