Results 1 to 6 of 6
  1. #1
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default JFrame-buttons missing

    Hi helpers,
    I have two small JFrame-problems (second is worst). First wat I want. I want to serialize a drawing in an applet. Because of disk-access being blocked I decided to use XMLSerializer and let the user copy the XML by hand. It works.
    Therefore I use a custom JDialog (called TextFrame) with a JTextArea in scrollbars. The constructor needs a JFrame, I can't use a local private JFrame in the constructor for super(parent) so it comes in with the TextFramecontructor (a loose JFrame instance).
    The first problem has a workaround: I want TextFrame being modal, but then I can't set the text with a setter. (In the save-version. setModal() only works before the call to setText(), and then the textarea stays blanco (workaround: saveversion gets text in the constructor). Question1: is modal always blocking setters?
    The second: my JFrames (open and save instance) have no minimize and iconify buttons, only the close-button.
    Question2: Howcome?
    (I.m.h.o. my class has everything the FrameDemo from the SwingTutorial has, but that has buttons.)


    Java Code:
    	package fimme.graph.ioframe;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import fimme.graph.lib.Lib;
    /**
     * Nu voor IO bij Applet (handmatig kopieren)
     * */
    public class TextFrame extends JDialog implements WindowListener 
    {
    	private static final long serialVersionUID = -201376880383204672L;
    	private JFrame parent;
    	private JTextArea textArea = new JTextArea();
    	private JButton OKButton;
    	private boolean withOKButton=true;
    
    	// constructor (open)
    	public TextFrame( JFrame theParent ){
    		super( theParent, "Venster voor handmatige opslag bij Applet" );
    		parent = theParent;
    		//setModal(true);
    		setText( "\n\nKopiŽer een Xml-Graphgegevens bestand in deze ruimte en sluit het venster." +
    				"\n\nCopy XML Graph-data in this area and close the window." );
    		makeFrameLayout( );
    		parent.pack();
    		parent.setVisible( true );
    	}
    	// constructor (save)
    	public TextFrame( JFrame theParent, String text ){
    		super( theParent, "Venster voor handmatige opslag bij Applet" );
    		parent = theParent;
    		makeFrameLayout( );
    		parent.pack();
    		parent.setVisible( true );
    		setText ( text );
    //		try{
    			//setModal(true);//Werkt niet na setText()
    //		}catch(Exception e){
    //			Lib.melding( "Exception: " + e, "TextFrame.TextFrame()");
    //		}
    	}
    	public void setText(String text){//Doet het niet indien modal
    	    textArea.setText( text );
    	}
    	public String getText(){
    	    return textArea.getText();
    	}
    	private void makeFrameLayout(){
    		getContentPane().setLayout( new BorderLayout());
    		setSize(new Dimension(1000,500));
    		if( withOKButton ){
    			getContentPane().add( makeKnoppenPanel() ,"North");
    		}
    		getContentPane().add( makeTextArea() ,"Center");
    		setVisible(true);
    		addWindowListener(this);
    	}
    	private JScrollPane makeTextArea(){
    		JScrollPane scrollPane = new JScrollPane( textArea );
    		textArea.setFont( new Font( "Courier", Font.BOLD,12 ));
    		textArea.setLineWrap( true );
    		textArea.setWrapStyleWord( true );
    		return scrollPane;
    		
    	}
    	private JPanel makeKnoppenPanel()
    	{
    	   JPanel thePanel = new JPanel();
    	   thePanel.setLayout( new FlowLayout());
    		OKButton = makeOKButton();
    		thePanel.add( OKButton );
    	   return thePanel;
    	}
    	private JButton makeOKButton(){
    	    JButton button = new JButton();
    	    button.setText("OK");
    	    button.addActionListener(new java.awt.event.ActionListener() {
    	      public void actionPerformed(ActionEvent e) {
    	        OKButton_actionPerformed(e);
    	      }
    	    });
    	    return button;
     	}
    
    	//_______________________________________________________
    	// WindowListeners and WindowEvents
    		public void windowClosing(WindowEvent e){
    			stoppen();
    		}
    		public void windowOpened(WindowEvent e)       {}
    		public void windowClosed(WindowEvent e)       {}
    		public void windowIconified(WindowEvent e)    {}
    		public void windowDeiconified(WindowEvent e)  {}
    		public void windowActivated(WindowEvent e)    {}
    		public void windowDeactivated(WindowEvent e)  {}
    		
    		private void stoppen()
    		{
    			parent.dispose();         // close the window
    		}
    		private void OKButton_actionPerformed(ActionEvent e) {
    		      stoppen();  // close the dialog
    		}
    
    	//_______________________________________________________
    
    }

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Sorry potential helpers.
    I was too hasty. I said it worked, but I just made a teststart-class, and the tekst you type in doesn't come out in the open-mode at all. So it seems I have a bigger problem.
    (The two listings together compile, but you can also let me try for a while. )

    Java Code:
    package fimme.graph.ioframe;
    
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    
    public class StartTestTekstFrame extends JApplet {
    	public void init(){
    		TestGui gui = new TestGui();
    	}
    	
    class TestGui {
    	private JFrame frame = new JFrame();
    	TestGui(){
    	    TextFrame textFrame = new TextFrame( frame );
    	    String xml = textFrame.getText();
    	    System.out.println( "Tekst: " + xml );
    	}
    	
    }	
    }

  3. #3
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    The second: my JFrames (open and save instance) have no minimize and iconify buttons, only the close-button.
    Question2: Howcome?
    Your TextFrames are not JFrames. They are extensions of JDialog.
    Make them extentions of JFrame if you want the window dressings.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Embarassing, but is sounds reasonable. I think it was a kind of magic thinking that the constructor woud use the JFrame for embedding, but then why call it "parent"? Also DialogDemo in my book had the buttons, but that was the startingframe.
    I'll try it, tomorrow I guess (demanding life). Thanks. If I get it all working (getting the text out before it closes) I'll let you know.

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default Solved, with trailing question

    I'm marking this thread as solved now.
    I'll give the code I came up with (see below). It works (and runs as given here), you can use it.
    My questions are not completely solved: I wanted a modal JFrame that can be resized, it became a JOptionPane that cannot.

    A last question: I 'm using an interface to get something back from the called
    class. It works fine but is difficult to read. I use this pattern more often. Has the pattern a name? Is there a more elegant solution? Thanks.

    Java Code:
    package fimme.graph.iooptionpane;
    public interface IFileOpener {
    	public void openFile( String xml );
    }
    Java Code:
    package fimme.graph.iooptionpane;
    
    import javax.swing.JApplet;
    
    public class StartTestOptionPane extends JApplet {
    	private static final long serialVersionUID = -7361432657916706950L;
    
    	public void init(){
    		new TestGui();
    	}
    //===============================================================================	
    class TestGui implements IFileOpener {
    	private String xmlToSave = "This is a stand-in text for XML to be \"saved\".\n" +
    								"Dit staat voor een xml-tekst die moet worden \"gesaved\".\n\n";
    
    	TestGui(){
    		/** Save a drawing by copying to file */
    		new TextOptionPane( xmlToSave );
    		System.out.println( "I guess you got the chance to save the text.\n" +
    		"Je hebt de tekst nu wel kunnen kopiŽren naar een bestand denk ik.\n\n" );
    		
    		/** Open a drawing copying an XML in the frame */
    	    new TextOptionPane( (IFileOpener) this );
    	}
    	/** Implementation of IFileOpener */
    	public void openFile( String xml ){
    		System.out.println( "The next xml will be opened:\nDe volgende xml wordt geopend:\n" + xml );
    	}
    	
    }	
    }
    Java Code:
    package fimme.graph.iooptionpane;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class TextOptionPane {
    	private JFrame frame = new JFrame();
    	private String textToSave = "";
    	private IFileOpener opener = null;
    	
    	public TextOptionPane( IFileOpener theOpener){
    		opener = theOpener;
    		makeOpenPane();
    	}
    	public TextOptionPane( String theTextToSave){
    		textToSave = theTextToSave;
    		makeSavePane();
    	}
    
    	private void makeSavePane(){
    		String title = "Save an XML-graphfile";
    	    String choices[] = { "Close window" };
    	    int returnValue = 999;
    	    String paneLabel = "XML:";
    	    JTextArea area = new JTextArea ( textToSave, 40, 100);
    	    JScrollPane sp = new JScrollPane ( area );
    	    Object msgs[] = new Object[]{ paneLabel, sp };
    	    while ( returnValue != 0 && returnValue != JOptionPane.CLOSED_OPTION ) {
    	      returnValue = JOptionPane.showOptionDialog( frame, msgs, title, JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, choices[0]);
    	    }
    	}
    	private void makeOpenPane(){
    		String title = "Open an XML-graphfile";
    	    String choices[] = {"Open file"};
    	    int returnValue = 999;
    	    String paneLabel = "XML:";
    	    String input = "";
    	    JTextArea area = new JTextArea ( "", 40, 100);
    	    JScrollPane sp = new JScrollPane ( area );
    	    Object msgs[] = new Object[]{ paneLabel, sp };
    	    while (returnValue != 0 && returnValue != JOptionPane.CLOSED_OPTION ) {
    	      returnValue = JOptionPane.showOptionDialog( frame, msgs, title, JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, choices[0]);
    	    }
    	    if( returnValue == 0 ){
    		    input = (String)area.getText();
    		    opener.openFile( input );
    	    }
    	}
    	
    }

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Really Closed!
    I was warned that it's a bad habit to pose questions at the end of a closed thread. It also wasn't clear which Interface I ment (IFileOpener). The question just popped up when I made the SSCCE ("I 've seen this pattern in my code before, and often wondered..."). Maybe I'll make a new thread.

Similar Threads

  1. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 07:29 PM
  2. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  3. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  4. [SOLVED] Buttons in JFrame
    By Nemo1959 in forum New To Java
    Replies: 8
    Last Post: 09-15-2008, 06:57 PM
  5. is it bug? or am i missing a point?
    By blowguy in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:19 PM

Posting Permissions

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