Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: After removing some components, frame's layout doesn't correct itself until resized.

  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default After removing some components, frame's layout doesn't correct itself until resized.

    I don't believe the full source code is necessary for something like this.

    Since it's a rather visual thing, I'll provide screenshots.

    Before connection:

    After connection:

    After being resized a little(how I want it to look after connection):




    I believe I could be removing the components wrong. I wanted to google search this, but I don't know how to describe it.


    Here's the code for when setUpNetworking() is called.
    Java Code:
    	private void setUpNetworking() {
    		connectionIP = usr_ip.getText();
    		connectionPort = Integer.parseInt(usr_port.getText());
    		try {
    			incoming.append("Successfully connected to server." + "\n");
    			sock = new Socket(connectionIP, connectionPort);
    			InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
    			reader = new BufferedReader(streamReader);
    			writer = new PrintWriter(sock.getOutputStream());
    			System.out.println("Networking Established");
    		} catch(IOException ex) {System.out.println("Failed to connect to server. Error below."); ex.printStackTrace(); incoming.append("Something went wrong between the connection of you and the server. Due to how Sockets function, you will need to restart the application. A more detailed explanation of the error is visible in your console."); outgoing.setEnabled(false); outgoing.setText("Sending messages has been disabled due to an error.");}
    	
    	//mainPanel.remove(usr_ip);
    	//mainPanel.remove(usr_port);
    		usr_ip.setVisible(false);
    		usr_port.setVisible(false);
    		mainPanel.revalidate();
    		mainPanel.repaint();
    	} // close setup networking

    I'm new to swing, incase if anyone is wondering.

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

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Quote Originally Posted by Vinx View Post
    I don't believe the full source code is necessary for something like this....
    Full source isn't necessary, but an SSCCE sure would help (please check out the link)!

    Do you call pack() on the JFrame after adding all components and prior to setVisible(true)? Are you calling revalidate and repaint on the correct JPanel?

    Also as an aside, I fear you may be doing things on the Swing event thread that should be done on a background thread. This is unrelated to your current problem.

  3. #3
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Quote Originally Posted by Fubarable View Post
    Full source isn't necessary, but an SSCCE sure would help (please check out the link)!

    Do you call pack() on the JFrame after adding all components and prior to setVisible(true)? Are you calling revalidate and repaint on the correct JPanel?

    Also as an aside, I fear you may be doing things on the Swing event thread that should be done on a background thread. This is unrelated to your current problem.
    I just tried calling pack(), everything was centered and horizontal. No items stacked vertically. Let's just say it was rather unpleasant looking.
    I don't believe calling pack() would be the right way to go.

    I only have one JPanel, I don't know how I could be calling it incorrectly.

    By the way I'm nearly clueless when it comes to multithreading, I still need to read up on it. Very inexperienced.


    I read the page on SSCCE as you requested, the issue is with how the GUI is working. I've removed everything but the GUI code in this, I hope I did the SSCCE correctly.

    [Java] package main; import java.io.*; import java.net.*; //import java.util.*; imp - Pastebin.com

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Most of us don't click links. If it's a SSCCE, it's short enough to post here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Quote Originally Posted by DarrylBurke View Post
    Most of us don't click links. If it's a SSCCE, it's short enough to post here.

    db
    It's just pastebin, I don't see what's so dangerous about it but alright.
    Java Code:
    package main;
    import java.io.*;
    import java.net.*;
    //import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Main {
    //	String rpModeSelection = "IC";
    	JTextArea incoming;
    	JTextField outgoing;
    	JTextField charName;
    //	BufferedReader reader;
    //	PrintWriter writer;
    //	Socket sock;
    	JTextField usr_ip;
    	JTextField usr_port;
    	JLabel usr_ip_lbl;
    	JPanel mainPanel;
    	JFrame frame;
    //	JComboBox rpMode;
    	
    	//String connectionIP = "173.254.223.98";
    	//int connectionPort = 5000;
    	
    	public static void main(String[] args) {
    		Main client = new Main();
    		client.go();
    	} // end main
    	
    	public void go() {
    		
    		JFrame frame = new JFrame("Simplex RP Client V1.1 Beta");
    		JPanel mainPanel = new JPanel();
    		incoming = new JTextArea(15,50);
    		incoming.setLineWrap(true);
    		incoming.setWrapStyleWord(true);
    		incoming.setEditable(false);
    		JScrollPane qScroller = new JScrollPane(incoming);
    		qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		outgoing = new JTextField(50);
    		
    		
    
    		usr_ip = new JTextField("IP", 10);
    		usr_port = new JTextField("Port", 4);
    		
    		
    		mainPanel.add(BorderLayout.NORTH, usr_ip);
    		mainPanel.add(BorderLayout.NORTH, usr_port);
    		
    		
    		
    		charName = new JTextField(30);
    		charName.setText("Character Name");
    		JButton sendButton = new JButton("Send");
    		mainPanel.add(qScroller);
    		mainPanel.add(outgoing);
    		mainPanel.add(sendButton);
    		mainPanel.add(BorderLayout.SOUTH, charName);
    //////////// MENU BAR ///////////////////////////////////////////////////////////////////////////////
    		JMenuBar menubar = new JMenuBar();
    		frame.setJMenuBar(menubar);
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    		
    		JMenuItem connect = new JMenuItem("Connect");
    		file.add(connect);
    		connect.addActionListener(new SetUpNetworkingListener());
    		
    		JMenuItem exit = new JMenuItem("Exit");
    		file.add(exit);
    		exit.addActionListener(new exitListener());
    		
    /////////////////////////////////////////////////////////////////////////////////////////////////////		
    		frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    		frame.setSize(577,375);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	//	frame.pack();
    	} // close go
    	
    	
    	private void setUpNetworking() {
    	//mainPanel.remove(usr_ip);
    	//mainPanel.remove(usr_port);
    		usr_ip.setVisible(false);
    		usr_port.setVisible(false);
    		mainPanel.revalidate();
    		mainPanel.repaint();
    	//	frame.pack();
    	} // close setup networking
    	
    	public class SetUpNetworkingListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    			
    			setUpNetworking();
    		}
    	}
    	
    	public class exitListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    		System.exit(0);	
    		}
    	}
    	
    	
    	public class SendButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    			outgoing.setText("");
    			outgoing.requestFocus();
    		} }
    	} // close inner class

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

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Quote Originally Posted by Vinx View Post
    I just tried calling pack(), everything was centered and horizontal. No items stacked vertically. Let's just say it was rather unpleasant looking.
    I don't believe calling pack() would be the right way to go.
    It is if you used layouts well, and right now your GUI is relying solely on FlowLayout and setting the size of the JFrame, so it comes as no surprise that calling pack() won't work, and that any other solution without layout changes will be nothing but kludges.

    The key is to read up on and use the layout managers to your advantage. Consider using BorderLayout for the overall GUI, using FlowLayout for the north JPanel that holds your IP and Port text fields, using BoxLayout for the south JPanel that holds the two JTextFields and the JButton...

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

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Since you've posted an SSCCE, I'll show you mine:

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class Main2 {
       private JPanel mainPanel = new JPanel();
       private JMenuBar jMenuBar = new JMenuBar();
    
       public Main2() {
          initComponents();
       }
    
       public void initComponents() {
          JMenu menu = new JMenu("File");
          menu.add(new JMenuItem("Connect"));
          menu.add(new JMenuItem("Exit"));
          jMenuBar.add(menu);
    
          JPanel northPanel = new JPanel();
          northPanel.add(new JTextField("IP", 15));
          northPanel.add(new JTextField("Port", 5));
    
          JScrollPane centralScrollPane = new JScrollPane();
          centralScrollPane
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          centralScrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          centralScrollPane.setViewportView(new JTextArea(15, 55));
          
          JPanel sendPanel = new JPanel();
          sendPanel.setLayout(new BoxLayout(sendPanel, BoxLayout.LINE_AXIS));
          sendPanel.add(new JButton("Send"));
          sendPanel.add(Box.createHorizontalStrut(10));
          sendPanel.add(new JTextField(40));
          JPanel southPanel = new JPanel();
          southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.PAGE_AXIS));
          southPanel.add(new JTextField(55));
          southPanel.add(Box.createVerticalStrut(5));
          southPanel.add(sendPanel);
          
          mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          mainPanel.setLayout(new BorderLayout(5, 5));
          mainPanel.add(northPanel, BorderLayout.NORTH);
          mainPanel.add(centralScrollPane, BorderLayout.CENTER);
          mainPanel.add(southPanel, BorderLayout.SOUTH);
       }
    
       public JComponent getMainComponent() {
          return mainPanel;
       }
    
       public JMenuBar getJMenuBar() {
          return jMenuBar;
       }
    
       private static void createAndShowGui() {
          Main2 main2 = new Main2();
    
          JFrame frame = new JFrame("My SSCCE 2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(main2.getMainComponent());
          frame.setJMenuBar(main2.getJMenuBar());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    Feel free to modify, but first study the layout tutorials.

    Notice that no where do I set the size of *anything*, note that I do use pack, and note the difference in behavior between your GUI and mine if you change the width of the GUI.
    Vinx likes this.

  8. #8
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    Quote Originally Posted by Fubarable View Post
    Since you've posted an SSCCE, I'll show you mine:
    Feel free to modify, but first study the layout tutorials.

    Notice that no where do I set the size of *anything*, note that I do use pack, and note the difference in behavior between your GUI and mine if you change the width of the GUI.
    Thank you for taking that large amount of time to assist me, it's really helping out. I originally didn't want to try Flow Layout because I was nervous so I did the compass style like in my book. I like Flow Layout because it's flexible and smooth. Thank you, I'll make sure to read up on it.

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

    Default Re: After removing some components, frame's layout doesn't correct itself until resiz

    You're welcome. The key to learning and using the layouts is to play with them, trying them out in different situations and seeing what happens. The other key is to nest JPanels and their layouts as this will allow you to achieve complex layouts using simple layout managers.
    DarrylBurke likes this.

Similar Threads

  1. frame not displaying correct contents
    By yemista in forum AWT / Swing
    Replies: 5
    Last Post: 10-20-2011, 05:30 PM
  2. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:13 PM
  3. How to set layout to have correct output
    By Prajin in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2010, 03:40 PM
  4. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 06:41 AM
  5. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 04:05 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
  •