Results 1 to 5 of 5
  1. #1
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default [BUG]: jframe objects messed up

    Hello,

    This my first thread, so be patient:)

    Ok, i have some problems with JFrame.

    When i launch it, sometimes everything goes ok, bu sometimes swing objects just apear wherethere they "want"...

    Ok, i attach some pictures.
    normalLook.png is where how it has to look like.
    weirdLook.png is when something weird happens :(

    And JFrame code bellow.

    Any help would be appreciated.

    Java Code:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    import java.util.Vector;
    import java.util.zip.Deflater;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.filechooser.FileFilter;
    
    
    class MainWindow extends JFrame implements ActionListener {
    //	JFrame window;
    	JPanel panel;
    	JTextField field;
    	JScrollPane pane;
    	JButton 
    		addButton, importFileButton, 
    		removeSelectedButton, removeAll, editCellButton,
    		countStatsButton, showStats, showHistogram, exportToHtml,
    		exitButton;
    	JCheckBox 
    		meanCheckBox, medianCheckBox, modeCheckBox,
    		deviationCheckBox, varianceCheckBox,
    		quartileCheckBox;
    	JLabel
    		messageLabel;
    	
    	JTextArea Notes;
    	
    	Vector <String> obs;
    	TableModel1 model;
    	StatConfig config;
    	JTable table;
    	
    	Statistics stat;
    		
    	public MainWindow() {
    		super("Statistics manager");
    	
    		obs = new Vector <String>();
    		config = new StatConfig();
    		model = new TableModel1(obs,config.TableColumns);
    		
    		messageLabel= new JLabel("");
    		setResizable(false);
    		
    		panel = new JPanel();
    		field = new JTextField("");
    		
    		addButton = new JButton("add number");
    		importFileButton = new JButton("import data");
    		removeSelectedButton = new JButton("remove");
    		removeAll = new JButton("remove all");
    		editCellButton = new JButton("edit data");
    		
    		countStatsButton = new JButton("calculate stats");
    		showStats = new JButton("show stats");
    		showHistogram = new JButton("histogram");
    		exportToHtml = new JButton("export to HTML");
    		
    		exitButton = new JButton("exit");
    		
    		meanCheckBox = new JCheckBox("Mean",true);
    		modeCheckBox = new JCheckBox("Mode",true); 
    		varianceCheckBox = new JCheckBox("Variance",true);
    		medianCheckBox = new JCheckBox("Median",true);
    		deviationCheckBox = new JCheckBox("Standard deviation",true);
    		quartileCheckBox = new JCheckBox("Quartile",true);
    
    		table = new JTable(model);
    		
    		pane = new JScrollPane(table);
    		
    		add(panel);
    		panel.add(field);
    		panel.add(pane);
    		panel.add(messageLabel);
    		
    		panel.add(addButton);
    		panel.add(importFileButton);
    		panel.add(removeSelectedButton);
    		panel.add(removeAll);
    		panel.add(editCellButton);
    		panel.add(countStatsButton);
    		panel.add(exportToHtml);
    		panel.add(exitButton);
    		panel.add(meanCheckBox);
    		panel.add(modeCheckBox);
    		panel.add(varianceCheckBox);
    		panel.add(medianCheckBox);
    		panel.add(deviationCheckBox);
    		panel.add(quartileCheckBox);
    		panel.add(showStats);
    		panel.add(showHistogram);
    		panel.add(exportToHtml);
    		
    		setContentPane(panel);
    		pack();
    		
    		field.setBounds(10,10,140, 20);
    		addButton.setBounds(10,40,120,25);
    		importFileButton.setBounds(10,70,120,25);
    		removeSelectedButton.setBounds(10,100,120,25);
    		removeAll.setBounds(10,130,120,25);		
    		editCellButton.setBounds(10,160,120,25);		
    		
    		countStatsButton.setBounds(150,40,120,25);
    		showStats.setBounds(150,70,120,25);
    		
    		meanCheckBox.setBounds(150, 100, 60, 15);
    		modeCheckBox.setBounds(150, 120, 60, 15);
    		varianceCheckBox.setBounds(150, 140, 80, 15);
    		
    		medianCheckBox.setBounds(230, 100, 80, 15);
    		deviationCheckBox.setBounds(230, 120, 140, 15);
    		quartileCheckBox.setBounds(230, 140, 140, 15);
    
    		showHistogram.setBounds(150,160,120,25);
    		exportToHtml.setBounds(150,190,120,25);
    		
    		exitButton.setBounds(10,420,60,25);
    		
    		pane.setBounds(390,60,200,360);
    		
    		messageLabel.setBounds(10, 380 , 300, 20);
    		
    		
    		showStats.setEnabled(false);
    		showHistogram.setEnabled(false);
    		exportToHtml.setEnabled(false);
    		
    		meanCheckBox.setEnabled(false);
    		modeCheckBox.setEnabled(false);
    		varianceCheckBox.setEnabled(false);
    		
    		medianCheckBox.setEnabled(false);
    		deviationCheckBox.setEnabled(false);
    		quartileCheckBox.setEnabled(false);		
    		
    		
    		messageLabel.setBackground( new Color(107,156,100) );
    		messageLabel.setOpaque(true); 
    
    		setLayout(null);
    		setSize(650,600);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		
    		addButton.addActionListener(this);
    		exitButton.addActionListener(this);
    		importFileButton.addActionListener(this);
    		removeSelectedButton.addActionListener(this);
    		editCellButton.addActionListener(this);
    		removeAll.addActionListener(this);
    		countStatsButton.addActionListener(this);
    		showStats.addActionListener(this);
    		showHistogram.addActionListener(this);
    		exportToHtml.addActionListener(this);
    		
    	}
    
    	
    
    
    
    	@Override
    	public void actionPerformed(final ActionEvent event) {}}
    Attached Thumbnails Attached Thumbnails [BUG]: jframe objects messed up-normallook.jpg   [BUG]: jframe objects messed up-weirdlook.jpg  

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's all about use or misuse of layout managers. Your panel JPanel uses a default FlowLayout which sometimes manifests itself and sometimes not. For now, just set the layout for this component to null,

    Java Code:
          //!! panel = new JPanel(); //!! 
          panel = new JPanel(null); //!!
          field = new JTextField("");

    but in the long run, you're much better off avoiding null layouts and absolute positioning and instead using nested JPanels as containers, each using an appropriate layout manager.

  3. #3
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default

    Thanks, Fubarable,

    That was helpful.
    Two more questions:
    1.
    manifests itself and sometimes not?
    How to understand that? What means layoutManager manifest itself? object invocation?

    2. Does other layouts support setBounds positioning?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by batia View Post
    1. How to understand that? What means layoutManager manifest itself? object invocation?
    Sometimes the FlowLayout forces its layout on the components you've added causing your GUI to look funny, and sometimes the absolute positioning wins out and your GUI looks better. Why this is intermittent, I don't know, but I do know that you don't want to mix the two (FlowLayout and setBounds) as you'll get unexpected behavior.


    2. Does other layouts support setBounds positioning?
    Yes, but you don't want to use setBounds. Rather you want to set the position of components relative to other components, and let the layout managers take care of the locations of the components.

  5. #5
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Non functional radio buttons and messed up array
    By flameofSuzaku in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 05:38 PM
  2. Is object of my class in a thread or I messed up again?
    By atch in forum Threads and Synchronization
    Replies: 5
    Last Post: 02-09-2010, 08:51 AM
  3. [newbie] messed up code
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-31-2009, 04:29 PM
  4. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  5. I must be seriously messed up...
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-16-2008, 07:20 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
  •