pronane
pronane

Reputation: 248

Java Swing GUI layout

I am building a simple Swing app that I want to have text fields/ combo boxes / dropdowns / etc. going down the page from left to right. I also want to be able size the textfields and combo boxes to the size of the text and no bigger, like on a web page.

I have create a JFrame, which contains a list of panes. Currently the Search pane has a lable on the right, then the textfield, and then a search button which all appear vertically the full length of the screen.

Here is the code:

public class SearchEvent {

public JPanel createSearchPane() {
    JTextField searchEvent = new JTextField();
    //searchEvent.setSize(10, 20);
    //searchEvent.setSize(new Dimension(10, 10));
    JPanel panel = new JPanel();
    GridLayout topLayout = new GridLayout(1,1);
    panel.setLayout(topLayout); 
    panel.add(new JLabel("Event  to search"));
    panel.add(searchEvent);
}
}

MainClass{

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Search Event ");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    tabbedPane.addTab("Search", null, new SearchEvent().createSearchPane(),"Search an event");
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

            //Display the window.
    frame.pack();
    frame.setVisible(true);
}

How can I make the text field appear on the right of the screen, the size of the text in the text field and the label to appear on the left beside the text field?

Upvotes: 0

Views: 56

Answers (1)

pronane
pronane

Reputation: 248

Using springLayout I achieved what I wanted to do by the following 2 lines:

textField.setMinimumsize(tf.getPreferredsize()) textField.setMaximumSize(tf.getPreferredSize())

For what i want to do im not sure its worth investing in the time to learn gridbaglayout

Upvotes: 0

Related Questions