File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes can this button be align at the top left coner of a container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "can this button be align at the top left coner of a container" Watch "can this button be align at the top left coner of a container" New topic
Author

can this button be align at the top left coner of a container

joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;

public class HomePage extends JFrame {
JFrame frame;

public HomePage() {
frame = new JFrame("Home");
frame.setBounds(100, 100,600,500);
setGUIPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void setGUIPanel() {
Container content = frame.getContentPane();
content.setLayout(new BorderLayout(10,10));

//create a Base panel to hold navigation and display panels
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(0,2,5,5));
content.add("Center",basePanel);

//create left panel and it's components and
//add to the Base panel
JPanel navPanel = new JPanel();
//Dimension size = new Dimension(80,20);
Box controlBox = Box.createVerticalBox();
JButton main = new JButton("Main");
//main.setPreferredSize(size);
JButton employ = new JButton("Employee");
//employ.setPreferredSize(size);
JButton admin = new JButton("Admin");
//admin.setPreferredSize(size);

//add button to the BoxLayout
controlBox.add(main);
controlBox.add(employ);
controlBox.add(admin);
//add controlBox to navPanel
navPanel.add(controlBox);


//add navPanel to BasePanel
basePanel.add(navPanel);
frame.setVisible(true);

}
public static void main(String[] args) {
HomePage hp = new HomePage();
}

}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Which button is this button? You have three of them.


GenRocket - Experts at Building Test Data
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by joseph okon:

can this button be align at the top left coner of a container


I'm not exactly sure what you are asking, but try changing
JPanel navPanel = new JPanel();
to
JPanel navPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));


bitguru blog
joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
sorry guys,
i want the all the three(3)buttons to be align horizontally,but at the top left
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by joseph okon:
i want the all the three(3)buttons to be align horizontally,but at the top left


The navPanel thing above will put them at the top left (as opposed to the top middle of the left side).

If you also want the buttons to all be the same size (i.e. to align on the right as well as on the left) then change
Box controlBox = Box.createVerticalBox();
to
JPanel controlBox = new JPanel(new GridLayout(0, 1, 0, x));

Use 0 for x if you want the buttons to run together. Use something like 5 to give them a little space.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can this button be align at the top left coner of a container