Results 1 to 3 of 3
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Controlling/limiting width of ProgressBars and TextFields in JToolbar as frame change

    JHello JEveryone!

    I have a JToolbar holding 3 JProgressBars and 4 JTextFields, as shown:



    As I stretch the frame horizontally, I'd like the TextFields to constrain their maximum width, and the progressbars to keep going. However, just the opposite seems to happen. The progressbars do grow a bit from their smallest size, but at a certain point, their width fixes itself while the TextFields keep growing and growing.



    As far as I know I am not doing anything to control their size at all currently.

    Here is minimal (sorry it's not exactly "small") code example.

    Java Code:
    /** SpinCAD Designer - DSP Development Tool for the Spin FV-1
     * SpinCADFrame.java
     * Copyright (C) 2013 - 2014 - Gary Worsham
     * Based on ElmGen by Andrew Kilpatrick.  Modified by Gary Worsham 2013 - 2014.  Look for GSW in code.
     * 
     *   This program is free software: you can redistribute it and/or modify
     *   it under the terms of the GNU General Public License as published by
     *   the Free Software Foundation, either version 3 of the License, or
     *   (at your option) any later version.
     *
     *   This program is distributed in the hope that it will be useful,
     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *   GNU General Public License for more details.
     *
     *   You should have received a copy of the GNU General Public License
     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     * 	
     */
    
    
    
    import java.awt.BorderLayout;
    
    // import javax.sound.sampled.spi.AudioFileReader;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    import javax.swing.WindowConstants;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import java.awt.event.ActionListener;
    //import java.io.IOException;
    import java.awt.Color;
    
    public class SpinCADFrame extends JFrame {
    	/**
    	 * 
    	 */
    	int buildNam = 881;
    
    	private static final long serialVersionUID = -123123512351241L;
    
    	// Swing things
    	private JPanel contentPane;
    	private final ModelResourcesToolBar pb = new ModelResourcesToolBar();
    
    	// ------------------------------------------------------------
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					SpinCADFrame dspFrame = new SpinCADFrame();
    					dspFrame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    
    	public SpinCADFrame() {
    		setTitle("SpinCAD Designer - Untitled");
    		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    		setBounds(100, 100, 800, 600);
    
    		WindowListener exitListener = window();
    		addWindowListener(exitListener);
    
    		// ==========================================================================================
    		// ======================= main panel
    		// =========================================================
    
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.LIGHT_GRAY);
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    
    
    		// =========================================================
    		// ======================= toolbars ========================
    		// =========================================================
    
    		JPanel toolBarPanel = new JPanel();
    		toolBarPanel.setLayout(new BoxLayout(toolBarPanel, BoxLayout.Y_AXIS));
    		contentPane.add(toolBarPanel, BorderLayout.SOUTH);
    
    		pb.setFloatable(false);
    		toolBarPanel.add(pb, BorderLayout.SOUTH);
    	}
    
    	private WindowListener window() {
    		WindowListener exitListener = new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				int confirm = JOptionPane.showOptionDialog(null,
    						"Do you wish to exit SpinCAD?", "Exit Confirmation",
    						JOptionPane.YES_NO_OPTION,
    						JOptionPane.QUESTION_MESSAGE, null, null, null);
    				if (confirm == JOptionPane.YES_OPTION) {
    					System.exit(0);
    				}
    			}
    		};
    		return exitListener;
    	}
    
    
    	// Swing dialog boxes.
    
    	void MessageBox(String title, String message) {
    		JFrame frame = new JFrame();
    		JOptionPane.showMessageDialog(frame,
    				message, title,
    				JOptionPane.DEFAULT_OPTION);
    
    	}
    
    	int yesNoBox(JPanel panel, String title, String question) {
    		int dialogButton = JOptionPane.YES_NO_OPTION;
    		int dialogResult = JOptionPane.showConfirmDialog(panel,
    				question,
    				title, dialogButton);
    		return dialogResult;
    	}
    
    	public class EditResourcesToolBar extends JToolBar implements ActionListener {
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = -8905757462245337214L;
    		final JLabel pinName = new JLabel("");
    
    		class Task extends SwingWorker<Void, Void> {
    			 // Main task. Executed in background thread.
    			@Override
    			public Void doInBackground() {
    				// Sleep for at least one second to simulate "startup".
    				try {
    					Thread.sleep(200);
    				} catch (InterruptedException ignore) {
    				}
    				done();
    				return null;
    			}
    			// Executed in event dispatch thread
    			 
    			public void done() {
    			}
    		}
    
    		public void actionPerformed(ActionEvent evt) {
    			System.out.println("Print: ");
    
    		}
    
    		public void update() {
    			ActionEvent evt = null;
    			actionPerformed(evt);
    		}
    	}
    
    	public class ModelResourcesToolBar extends JToolBar implements
    	ActionListener {
    		private static final long serialVersionUID = -8905757462245337214L;
    		final JProgressBar progressBar_2 = new JProgressBar();
    		final JTextField ramp0Bar = new JTextField("RMP 0", 6);
    
    		class Task extends SwingWorker<Void, Void> {
    			@Override
    			public Void doInBackground() {
    				// Sleep for at least one second to simulate "startup".
    				try {
    					Thread.sleep(200);
    				} catch (InterruptedException ignore) {
    				}
    				done();
    				return null;
    			}
    
    			public void done() {
    			}
    		}
    
    		// ==============================================================
    		// == Resources toolbar
    		public ModelResourcesToolBar() {
    			super();
    
    			progressBar_2.setMaximum(128);
    			progressBar_2.setBackground(Color.CYAN);
    			progressBar_2.setString("Instructions Used");
    			progressBar_2.setStringPainted(true);
    			Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED);
    			progressBar_2.setBorder(border);
    
    			ramp0Bar.setHorizontalAlignment(JTextField.CENTER);
    			ramp0Bar.setBackground(Color.GREEN);
    			ramp0Bar.setForeground(Color.BLUE);
    
    			add(progressBar_2);
    			add(ramp0Bar);
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    	}
    }


    Thanks for any suggestions!

    DL
    Last edited by Digital Larry; 02-02-2015 at 11:07 PM. Reason: made it smaller

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Controlling/limiting width of ProgressBars and TextFields in JToolbar as frame ch

    I've edited your post to change block comments (/* ... */) to doc comments (/** ... */) as the forum syntax highlighter doesn't deal with block comments correctly.

    It doesn't take 482 lines of code to demonstrate the problem you describe. Creating and posting a SSCCE (Short, Self Contained, Correct (Compilable), Example) would go a long way to getting better help sooner.

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

  3. #3
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Re: Controlling/limiting width of ProgressBars and TextFields in JToolbar as frame ch

    Thanks for your help. I apologize for my overly large example.

    The answer is in fact easy peasy. I'm just not sure if it is portable. This appears to keep the JTextFields at the same width as when the window first appears. It's sort of a seat of the pants solution but it seems to work OK on Windows anyway.

    Java Code:
    			Dimension lfoBarDim = sine1Bar.getPreferredSize();
    	
    			ramp0Bar.setMaximumSize(lfoBarDim);
    			ramp1Bar.setMaximumSize(lfoBarDim);
    			sine0Bar.setMaximumSize(lfoBarDim);
    			sine1Bar.setMaximumSize(lfoBarDim);
    Last edited by Digital Larry; 02-03-2015 at 04:52 PM.

Similar Threads

  1. how to change jframe into frame in java
    By nataraj87 in forum AWT / Swing
    Replies: 1
    Last Post: 09-23-2014, 09:05 AM
  2. Replies: 1
    Last Post: 06-25-2012, 11:39 PM
  3. Replies: 1
    Last Post: 02-11-2012, 03:07 AM
  4. change icon of frame?
    By Manish87 in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2011, 04:11 AM
  5. Replies: 7
    Last Post: 01-17-2011, 11:47 AM

Posting Permissions

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