Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hs teacher needs help - display swing components with coordinates  RSS feed

 
Jeff Borland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hs teacher needs help -
hi folks,
im a high school teacher teaching a java class for the first time. The students are doing their final projects and I'm coming across some problems.
One student wants to use swing components to display numerous buttons and menus. He wants to know the best way to do this. I decided i need to use jbuttons (right?), and i thought to setlayout to null. As below:
Container c = getContentPane();
c.setLayout(null);
But i cant get menus or scrollpane (scrolling textarea) to work, any simple code to do that...
i and my students appreciate any help you can give
jeff
 
Matthew Middleton
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you set the content pane to null then you have to use the reshape(xCoor, yCoor, ht, width) class. if you have a JButton next, and a Container box, the code would look something like:
box.add(next);
next.reshape(5,5,100, 25);
this should be what you need, tell me if this helped
GOD Bless
 
Jeff Borland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i could get the button to work using .setBounds,
but is that the best way to specify coordinates...
and if so, do you have any code for menus that works with coordinates?
thanks,
jeff
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the layout to null causes a lot of problems like this... what was the reason that led you to need to lay out components with specific coordinates? This usually leads to more problems than it solves. There's usually a way to nest layouts, use GridBagLayout, or even create your own layout that will let you achieve what you are trying to do without resorting to null layout.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple code for working with null layout? No, not really. Using layout managers makes for simple code.

In the beginning a null layout appears to be an easy choice but as the example below shows it can be quite a chore to get any useful results with it. The one place it seems to work okay is when using mouse code to drag/move components around with.

Once you make friends with and learn to use a few layout managers, say, BorderLayout, FlowLayout, GridLayout and CardLayout, nesting panels opens a whole world of possibilities in putting things together. Later you can get to know more specialized layout managers like BoxLayout and GridBagLayout.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!