Results 1 to 9 of 9
- 06-28-2012, 07:08 PM #1
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
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.
-
Re: After removing some components, frame's layout doesn't correct itself until resiz
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.
- 06-28-2012, 07:35 PM #3
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
Re: After removing some components, frame's layout doesn't correct itself until resiz
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
- 06-28-2012, 08:31 PM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-28-2012, 08:36 PM #5
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
Re: After removing some components, frame's layout doesn't correct itself until resiz
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
-
Re: After removing some components, frame's layout doesn't correct itself until resiz
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...
-
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:
Feel free to modify, but first study the layout tutorials.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(); } }); } }
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.
- 06-28-2012, 10:18 PM #8
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
Re: After removing some components, frame's layout doesn't correct itself until resiz
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.
-
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.
Similar Threads
-
frame not displaying correct contents
By yemista in forum AWT / SwingReplies: 5Last Post: 10-20-2011, 05:30 PM -
Adding and removing components from a GridBagLayout
By peterhabe in forum New To JavaReplies: 4Last Post: 09-19-2010, 10:13 PM -
How to set layout to have correct output
By Prajin in forum AWT / SwingReplies: 6Last Post: 07-07-2010, 03:40 PM -
How to place the GUI components in correct order
By impact in forum New To JavaReplies: 2Last Post: 05-04-2008, 06:41 AM -
Removing components from JPanel
By Echilon in forum New To JavaReplies: 0Last Post: 12-30-2007, 04:05 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks