Prev: Imitating a JFrame extended program with JPanel; help needed...
Next: Making JTable column widths obey me
From: Fencer on 16 Feb 2010 17:12 On 2010-02-16 23:00, Knute Johnson wrote: > GBL can make them the same width really easily if they are vertically > aligned with the fill element. Horizontally like that is a little more > difficult. You have to resort to tricks. Once you know the size of the > bigger one which you only will after the container is sized then you can > set the smaller one to the same size. Ok, I think I will wait a while doing this then, it was only a detail anyway. > > I have recreated your code to give you a simpler way of presenting your > application. One note, only one GridBagConstraints object is usually > necessary. Just adjust the fields and add your component with a > reference to the GBC and change the fields as necessary. You will see in > the example below I use only one but for two different components. Yes, I know I was being overly verbose in my code. :-) > > Also, you don't need to get at the ContentPane any more before adding > components to a JFrame. The add method has been overridden to add the > components to the ContentPane of the JFrame. Ah, thanks for the tip! > > Back to the trick. Use a ComponentListener to check for a component > resized event. A resize event is triggered when the component is first > sized for display. Then set the preferred size of the other component > and call revalidate() which causes the component to be redrawn and > triggers another resized event. > > The only other thing I might mention is that you have too many fixed > sizes for panels and frames. Usually one creates the content and then > calls pack() on the frame to size your application. That's not always > possible however but given wide range of display resolutions these days, > it is sometimes better to have fixed sizes. My example below uses your > sizes however. I don't really know how to deal with the problem. I had, for example, to adjust the width of the "group panel" before the entire titles border was visible. > > Oh, and one other thing. Swing (JFrame, JWindow etc) GUI creation must > occur on the Event Dispatch Thread (search Google for EDT). That is > accomplished many ways but the simplest is to use the > EventQueue.invokeLater() method to wrap all GUI creation code. Thank you, I've changed that, but I don't want to inherit from JFrame. [snip code] - F
From: Lew on 16 Feb 2010 18:00 Knute Johnson wrote: >> Oh, and one other thing. Swing (JFrame, JWindow etc) GUI creation must >> occur on the Event Dispatch Thread (search Google for EDT). That is >> accomplished many ways but the simplest is to use the >> EventQueue.invokeLater() method to wrap all GUI creation code. Fencer wrote: > Thank you, I've changed that, but I don't want to inherit from JFrame. > [snip code] Sorry? You used 'JFrame' in your original post. The question of whether to extend 'JFrame' (I'm with you in preferring to compose it) is orthogonal to the need to put all GUI work on the EDT. Did I misconstrue something? Perhaps your sentence means that you put the GUI creation on the EDT, but you don't want to inherit from 'JFrame'. I just don't get how the inheritance comment connects to what Knute wrote about the EDT. -- Lew
From: Fencer on 16 Feb 2010 18:05 On 2010-02-17 00:00, Lew wrote: > Knute Johnson wrote: >>> Oh, and one other thing. Swing (JFrame, JWindow etc) GUI creation must >>> occur on the Event Dispatch Thread (search Google for EDT). That is >>> accomplished many ways but the simplest is to use the >>> EventQueue.invokeLater() method to wrap all GUI creation code. > > Fencer wrote: >> Thank you, I've changed that, but I don't want to inherit from JFrame. >> [snip code] > > Sorry? > > You used 'JFrame' in your original post. The question of whether to > extend 'JFrame' (I'm with you in preferring to compose it) is orthogonal > to the need to put all GUI work on the EDT. > > Did I misconstrue something? Perhaps your sentence means that you put > the GUI creation on the EDT, but you don't want to inherit from > 'JFrame'. I just don't get how the inheritance comment connects to what > Knute wrote about the EDT. > Ok, extend, not inherit, I prefer composition, yes. That comment is not related to the EDT and reading what I wrote again I can see that I should have separated those two statements more clearly. But I must say there is something in your tone I don't like but perhaps (hopefully) that too is a misunderstanding. - F
From: Lew on 16 Feb 2010 18:58 Knute Johnson wrote: >>>> Oh, and one other thing. Swing (JFrame, JWindow etc) GUI creation must >>>> occur on the Event Dispatch Thread (search Google for EDT). That is >>>> accomplished many ways but the simplest is to use the >>>> EventQueue.invokeLater() method to wrap all GUI creation code. >> >> Fencer wrote: >>> Thank you, I've changed that, but I don't want to inherit from JFrame. >>> [snip code] Lew wrote: >> Sorry? >> >> You used 'JFrame' in your original post. The question of whether to >> extend 'JFrame' (I'm with you in preferring to compose it) is orthogonal >> to the need to put all GUI work on the EDT. >> >> Did I misconstrue something? Perhaps your sentence means that you put >> the GUI creation on the EDT, but you don't want to inherit from >> 'JFrame'. I just don't get how the inheritance comment connects to what >> Knute wrote about the EDT. Fencer wrote: > Ok, extend, not inherit, I prefer composition, yes. That comment is not > related to the EDT and reading what I wrote again I can see that I > should have separated those two statements more clearly. But I must say > there is something in your tone I don't like but perhaps (hopefully) > that too is a misunderstanding. That "tone" to which you refer was confusion. Frankly, I don't see what in what I said could have offended you. I was simply asking for clarification of a sentence that I didn't understand. Have a nice day. -- Lew
From: Knute Johnson on 16 Feb 2010 19:22 On 2/16/2010 2:12 PM, Fencer wrote: >> The only other thing I might mention is that you have too many fixed >> sizes for panels and frames. Usually one creates the content and then >> calls pack() on the frame to size your application. That's not always >> possible however but given wide range of display resolutions these days, >> it is sometimes better to have fixed sizes. My example below uses your >> sizes however. > > I don't really know how to deal with the problem. I had, for example, to > adjust the width of the "group panel" before the entire titles border > was visible. TitledBorder has a method to provide the minimum size necessary to display the complete text, TitledBorder.getMinimumSize(). It appears to have a bug though and not calculate the height correctly. But even that can be gotten around by using the original preferred height with the width returned from the getMinimumSize() method. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CG extends JFrame { public CG() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JPanel p = new JPanel(new GridBagLayout()); TitledBorder tb = new TitledBorder( "Start an new session by opening a BioModel or laod a" + " previously saved session"); p.setBorder(tb); c.gridx = 0; c.weightx = 1.0; final JButton b1 = new JButton("Press Me"); p.add(b1,c); c.gridx = 1; final JButton b2 = new JButton("Please Don't Press Me"); p.add(b2,c); b1.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent ce) { b1.setPreferredSize(b2.getPreferredSize()); b1.revalidate(); System.out.println(b1.getSize()); System.out.println(b2.getSize()); } }); c.gridx = c.gridy = 0; c.weighty = 1.0; // c.weightx is still 1.0 c.anchor = GridBagConstraints.CENTER; add(p,c); // this needs to be after all the components are added to JPanel p p.setPreferredSize(new Dimension( tb.getMinimumSize(p).width,p.getPreferredSize().height)); c.gridy = 1; c.weighty = 0.1; // the panel above gets most of the weighty c.fill = GridBagConstraints.HORIZONTAL; // c.weightx is still 1.0 c.anchor = GridBagConstraints.SOUTH; JLabel l = new JLabel("Blah Blah Blah Blah Blah Blah"); l.setBorder(BorderFactory.createLineBorder(Color.BLUE,10)); add(l,c); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { CG cg = new CG(); cg.setSize(640,480); cg.setLocationRelativeTo(null); cg.setVisible(true); } }); } } -- Knute Johnson email s/nospam/knute2010/
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Imitating a JFrame extended program with JPanel; help needed... Next: Making JTable column widths obey me |