Two JInternalFrames inside a JDesktopPane

I had developed 2 menu items under a menu

MENU is DISPLAY

MENUITEM-1 is DISPLAY_TABLE

MENUITEM-2 is DISPLAY_SUMMARY

When I click DISPLAY_TABLE; table is read from a file

and parsed and displayed in JTable perfectly.

But when I click DISPLAY_SUMMARY, I am not getting anything.

I feel that I had added everything correctly.

I dont know what is the mistake here in this code.

Anyone please help me. My code is here.

package javapackage;

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

import java.awt.event.*;

import java.text.NumberFormat;

import java.util.*;

import java.io.*;

publicclass DisplayTableSummaryextends JFrame

{

DefaultTableModel dtm;

JTable tbl;

JScrollPane s, scp;

int k=0;

String a,b,c,d,e;

StringTokenizer st;

//File filename1, filename2;

JTextArea txa;

JDesktopPane dsk1 =new JDesktopPane();;

Container c1=getContentPane();

File filenam;

public DisplayTableSummary()

{

JFileChooser filechooser =new JFileChooser ();

filechooser.setFileSelectionMode ( JFileChooser.FILES_ONLY);

int result = filechooser.showOpenDialog (null);

if (result== JFileChooser.CANCEL_OPTION)

{

return;

}

filenam = filechooser.getSelectedFile();

}

publicvoid displayTable()

{

if (filenam==null||filenam.getName().equals (" "))

{

JOptionPane.showMessageDialog( null,"Invalid File Name","Invalid FileName", JOptionPane.ERROR_MESSAGE );

}

else

{

tableGUI();

dynamicTable();

//repaint();

}

}

publicvoid displaySummary()

{

if (filenam==null||filenam.getName().equals (" "))

{

JOptionPane.showMessageDialog( null,"Invalid File Name","Invalid FileName", JOptionPane.ERROR_MESSAGE );

}

else

{

textAreaGUI();

textfmFile();

}

}

publicvoid tableGUI()

{

// Container c=getContentPane();

dtm =new DefaultTableModel();

//dtm.setRowCount(m);

dtm.addColumn("PAYMENT NUMBER");

dtm.addColumn ("PAYMENT AMOUNT");

dtm.addColumn ("PRINCIPLE");

dtm.addColumn ("INTEREST");

dtm.addColumn("LOAN BALANCE");

tbl =new JTable(dtm);

s =new JScrollPane(tbl);

//dsk1 = new JDesktopPane();

c1.add(dsk1);

JInternalFrame inf1 =new JInternalFrame("Internal Frame",true,true,true,true);

inf1.setSize(350,350);

Container con1=inf1.getContentPane();

con1.add(s);

inf1.setOpaque(false);

dsk1.add(inf1);

inf1.setVisible(true);

setSize(400,400);

setVisible(true);

}

publicvoid dynamicTable()

{

System.out.println("I am ready to display your table");

try

{

FileInputStream file =new FileInputStream(filenam);

InputStreamReader i =new InputStreamReader(file);

BufferedReader in =new BufferedReader(i);

String s="PAYMENT NUMBERPAYMENTPRINCIPLEINTERESTBALANCE";

String line =" ",noline=" ";

do

{

line=in.readLine();

System.out.println(line);

}while(!line.equals(s));

noline=in.readLine();

while((line=in.readLine()) !=null)

{

k++;

noline=in.readLine();

st=new StringTokenizer(line);

a=st.nextToken();

b=st.nextToken();

c=st.nextToken();

d=st.nextToken();

e=st.nextToken();

Vector rows=new Vector();

rows.add(a);

rows.add(b);

rows.add(c);

rows.add(d);

rows.add(e);

dtm.addRow(rows);

}

}

catch ( IOException e )

{

e.printStackTrace();

JOptionPane.showMessageDialog (null,"File Does not exist","Invalid File Name", JOptionPane.ERROR_MESSAGE);

}

}

publicvoid textAreaGUI()

{

c1.add(dsk1);

txa =new JTextArea();

scp.add(txa);

JInternalFrame inf2 =new JInternalFrame("Internal Frame",true,true,true,true);

inf2.setSize(350,350);

Container con2=inf2.getContentPane();

con2.add(scp);

inf2.setOpaque(false);

dsk1.add(inf2);

inf2.setVisible(true);

setSize(400,400);

setVisible(true);

}

publicvoid textfmFile()

{

System.out.println("I am ready to display your summary");

try

{

FileInputStream file =new FileInputStream(filenam);

InputStreamReader i =new InputStreamReader(file);

BufferedReader in =new BufferedReader(i);

String line=" ";

String s="PAYMENT NUMBERPAYMENTPRINCIPLEINTERESTBALANCE";

line=in.readLine();

do

{

line += in.readLine()+"/n";

System.out.println(line);

}while(!line.equals(s));

txa.setText(line);

}

catch ( IOException e )

{

e.printStackTrace();

JOptionPane.showMessageDialog (null,"File Does not exist","Invalid File Name", JOptionPane.ERROR_MESSAGE);

}

}

}

=======================================================

JMenu service =new JMenu("Services");

JMenuItem s1=new JMenuItem ("Display Amortization");

JMenuItem s2=new JMenuItem ("Display Summary");

service.add(s1);

service.add(s2);

s1.addActionListener(handler);

s2.addActionListener(handler);

class TextFieldHandlerimplements ActionListener

{

publicvoid actionPerformed (ActionEvent event)

{

if (event.getSource()== s1)

{

DisplayTableSummary a1=new DisplayTableSummary();

a1.displayTable();

}

elseif (event.getSource()== s2)

{

DisplayTableSummary a2=new DisplayTableSummary();

a2.displaySummary();

}

}

}

[10584 byte] By [qazwsx_qaza] at [2007-10-3 5:21:28]
# 1

here's a stripped debugging-version of your code, showing both methods are called

perhaps your problem is you are working with different instances of the

DisplayTableSummary() class.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DisplayTableSummary

{

public void displayTable()

{

System.out.println("displayTable()");

}

public void displaySummary()

{

System.out.println("displaySummary()");

}

}

class Testing

{

JMenuItem s1= new JMenuItem ("Display Amortization");

JMenuItem s2= new JMenuItem ("Display Summary");

public void buildGUI()

{

TextFieldHandler handler = new TextFieldHandler();

JMenuBar menuBar = new JMenuBar();

JMenu service = new JMenu("Services");

menuBar.add(service);

service.add(s1);

service.add(s2);

s1.addActionListener(handler);

s2.addActionListener(handler);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(100,100);

f.setLocationRelativeTo(null);

f.setJMenuBar(menuBar);

f.setVisible(true);

}

class TextFieldHandler implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

if (event.getSource()== s1)

{

DisplayTableSummary a1=new DisplayTableSummary();

a1.displayTable();

}

else if (event.getSource()== s2)

{

DisplayTableSummary a2=new DisplayTableSummary();

a2.displaySummary();

}

}

}

public static void main(String[] args){new Testing().buildGUI();}

}

Michael_Dunna at 2007-7-14 23:28:25 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thankyou very much.

I tried this but it did not work.

My main goal is to get both

the JInternalFrames (table-display and summary-display)

inside the same JDesktopPane.

Table display works well.

but the problem comes with the click event for summary display.

qazwsx_qaza at 2007-7-14 23:28:25 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I tried this but it did not work.

the different messages didn't print? - works OK for me.

> My main goal is to get both

> the JInternalFrames (table-display and summary-display)

> inside the same JDesktopPane.

you are using different instances of DisplayTableSummary()

try these changes

1.

add this as a field to your class that has the menu items

DisplayTableSummary a;

2.

try this for your TextFieldHandler class

class TextFieldHandler implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

if (event.getSource()== s1)

{

//DisplayTableSummary a1=new DisplayTableSummary();

//a1.displayTable();

if(a == null) a = new DisplayTableSummary();

a.displayTable();

}

else if (event.getSource()== s2)

{

//DisplayTableSummary a2=new DisplayTableSummary();

//a2.displaySummary();

if(a == null) a = new DisplayTableSummary();

a.displaySummary();

}

}

}

Michael_Dunna at 2007-7-14 23:28:25 > top of Java-index,Desktop,Core GUI APIs...
# 4
I tried this but not working. Its ok sir.I created a separated dsktoppane and displaing the summary frame. I am very greatful to you. You helped me a lot by sparing time inspite of ur bussy schedule.Thankyou very much. Very kind of you.
qazwsx_qaza at 2007-7-14 23:28:25 > top of Java-index,Desktop,Core GUI APIs...
# 5

The code you posted isn't compileable or executable so we can't see exactly what is going on.

You should use the constructor of your class for "building the GUI". For example create the desktop pane and add it to the content pane. Currently you are adding the desktop pane to the content pane in each of the two methods. It shouldn't cause a problem but its ugly. All your methods need to to is create and add the internal frame to the desktop pane.

Maybe the problem is that the frame is hidden below the other frame since you assign the same frame size.

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html]How to Use Internal Frames[/url] for an example of creating and showing new internal frames.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 23:28:25 > top of Java-index,Desktop,Core GUI APIs...