Positioning buttons in a specific way in a JPanel - java

I'm trying to position six buttons in a specific way but currently the output is almost right but the buttons are not in the exact positions that I want them to be in (with all buttons touching adjacent buttons with no gaps). This is show below:
Could someone have a look at my code to see why the positioning isn't exact?
public void createDirectionButtonPanel() {
JButton northButton = new JButton("north");
JButton southButton = new JButton("south");
JButton westButton = new JButton("west");
JButton eastButton = new JButton("east");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");
JPanel directionButtonPanel = new JPanel();
// directionButtonPanel.setOpaque(false);
directionButtonPanel.setLayout(new BoxLayout(directionButtonPanel, BoxLayout.Y_AXIS));
JPanel directionRow_1 = new JPanel();
// directionRow_1.setOpaque(false);
directionRow_1.setLayout(new BoxLayout(directionRow_1, BoxLayout.X_AXIS));
directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_1.add(northButton);
directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_1.add(upButton);
JPanel directionRow_2 = new JPanel();
// directionRow_2.setOpaque(false);
directionRow_2.setLayout(new BoxLayout(directionRow_2, BoxLayout.X_AXIS));
directionRow_2.add(westButton);
directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_2.add(eastButton);
directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));
JPanel directionRow_3 = new JPanel();
// directionRow_3.setOpaque(false);
directionRow_3.setLayout(new BoxLayout(directionRow_3, BoxLayout.X_AXIS));
directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_3.add(southButton);
directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_3.add(downButton);
upButton.setMaximumSize(southButton.getPreferredSize());
northButton.setMaximumSize(southButton.getPreferredSize());
westButton.setMaximumSize(southButton.getPreferredSize());
southButton.setMaximumSize(southButton.getPreferredSize());
downButton.setMaximumSize(southButton.getPreferredSize());
eastButton.setMaximumSize(southButton.getPreferredSize());
directionButtonPanel.add(directionRow_1);
directionButtonPanel.add(directionRow_2);
directionButtonPanel.add(directionRow_3);
}

Try this. GridLayout seems appropriate, as JavaDoc says: Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. Also saves you the trouble of having to arrange the rows yourself. Just add everything to your directionButtonPanel.
JButton northButton = new JButton("north");
JButton southButton = new JButton("south");
JButton westButton = new JButton("west");
JButton eastButton = new JButton("east");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");
JPanel directionButtonPanel = new JPanel();
directionButtonPanel.setLayout(new GridLayout(3,4));
// row 1
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(northButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(upButton);
// row 2
directionButtonPanel.add(westButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(eastButton);
directionButtonPanel.add(new JPanel());
// row 3
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(southButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(downButton);

Related

how to add a "console" like window to a GUI?

I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField

Swing Formatting with JTextField & BoxLayout

JTextField area2 = new JTextField();
JTextField searchtext=new JTextField();
JPanel mainframe = new JPanel();
JButton searchbutton=new JButton("Submit");
JButton registerbutton=new JButton("Login/Register");
mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
p4.setLayout(new BoxLayout(p4, BoxLayout.X_AXIS));
p1.setBorder(BorderFactory.createTitledBorder("FLIGHT LISTINGS"));
p2.setBorder(BorderFactory.createTitledBorder("SEARCH"));
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
p3.setBorder(BorderFactory.createTitledBorder("User Account"));
p3.add(registerbutton);
p2.add(searchtext);
p2.add(searchbutton);
ImageIcon icon = new ImageIcon("map.jpg");
JLabel thumb = new JLabel();
thumb.setIcon(icon);
p4.add(thumb);
mainframe.add(p3);
mainframe.add(p4);
mainframe.add(p2);
mainframe.add(p1);
this.add(mainframe);
this is my code, and the result seems to always give me a gross-looking output. i would much rather have my JTextField be only 1 line in height instead of so fat. would anyone please tell me what I am doing wrong?
this is the devastating result: http://imgur.com/XCuDefM
mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS));
A BoxLayout will grow components based on the available space in the frame. So if your text field is growing to an unreasonable size that means you are using:
frame.setSize(...);
and are giving your frame some random size that is not appropriate for the components you added to the frame.
Instead you should be using:
frame.pack();
and all the components will be displayed at their preferred sizes.
Let the layout managers to their jobs and don't assign sizes to components.

BorderLayout() doesn't seem to be working. JButtons won't go to SOUTH

As the title says, a panel with JButtons in it stays north for some reason. Here's the code which I think is relevant.
f = new JFrame();
f.setTitle("Book Quiz");
f.setSize(800, 400);
f.setLocation(400, 250);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
card = new JPanel();
cardLayout = new CardLayout();
card.setLayout(cardLayout);
takeQuizCard = new JPanel();
takeQuizCard.setLayout(new BorderLayout());
quizButtons = new JPanel();
submit = new JButton("Submit Answer");
next = new JButton("Next");
quizDone = new JButton("Done");
quizDone.addActionListener(this);
quizQuit = new JButton("Quit");
quizQuit.addActionListener(this);
quizButtons.setLayout(new FlowLayout());
quizButtons.add(submit);
quizButtons.add(next);
quizButtons.add(quizDone);
quizButtons.add(quizQuit);
takeQuizCard.add(quizButtons, BorderLayout.SOUTH);
quizInfo = new JPanel(new GridLayout(0, 1));
card.add(takeQuizCard, TAKE_QUIZ_CARD);
takeQuizCard.add(quizButtons);
f.add(card);
There are also 4 radio buttons and two labels on the WEST part. I left it out so it doesn't distract anyone, but if it is relevant, I'll add it. Anyone have any ideas? I have another 'card' in my program that works properly, and everything seems to be the same code-wise.
You are re-adding quizButtons on the second last line. It is overriding your previous placement of SOUTH.
Remove:
takeQuizCard.add(quizButtons);
And keep:
takeQuizCard.add(quizButtons, BorderLayout.SOUTH);

How to make JTexArea fit the window

I have created JTextArea inserted in JScrollPane inside JPanel which is inside the JFrame.
I had to put exact number of colls and rows in the JTextArea's constructor, if didn't the the JTextArea was not visible, but when I resize the window the JTextArea has fixed size and I don't want it. I would like it to fix the Window. How to do it? Thank you.
jpMiddle = new JPanel();
JTextArea ta = new JTextArea(20,42);
JScrollPane sp = new JScrollPane(
ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jpMiddle.add(sp);
EDIT : Figured it out myself:
jpMiddle = new JPanel(new GridLayout(1,1));
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(
ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jpMiddle.add(sp);

JScrollPane Scrolling Horizontally when it Should be Scrolling Vertically

I have a panel with FlowLayout specified that is full of labels, text fields, and text areas. The frame isn't that wide, but the panel becomes tall because of all the components inside. I want to add the panel to a JScrollPane, so I can scroll vertically through the panel. However, when I add the panel to the scroll pane and add the scroll pane to the frame, all the components are right next to each other and it scrolls horizontally through them. Here's the code:
public class Form {
JTextField jtfName = new JTextField(15);
JTextField jtfTitle = new JTextField(15);
JTextField jtfAuthor = new JTextField(15);
JTextArea jtaSetting = new JTextArea(5, 15);
JTextArea jtaMainChars = new JTextArea(5, 15);
JTextArea jtaConflict = new JTextArea(5, 15);
JTextArea jtaQuote = new JTextArea(5, 15);
JTextArea jtaMainCharShows = new JTextArea(5, 15);
JPanel jpnlName = new JPanel();
JPanel jpnlTitle = new JPanel();
JPanel jpnlAuthor = new JPanel();
Form() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Organizer");
jfrm.setLayout(new GridLayout(0,1));
// Give the frame an initial size.
jfrm.setSize(300, 300);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel.
JPanel jpnl = new JPanel();
// Create labels.
JLabel jlabName = new JLabel("Student Name:");
JLabel jlabTitle = new JLabel("Title:");
JLabel jlabAuthor = new JLabel("Author:");
JLabel jlabSetting = new JLabel("Setting (Time and Place):");
jlabSetting.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainChars = new JLabel("Main Characters:");
jlabMainChars.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabConflict = new JLabel("<html>Describe the major conflict of the<br>story in one well-written sentence:");
jlabConflict.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabQuote = new JLabel("<html>Find and write down a passage (quote<br>from the book that reveals a significant<br>personality trait of one of the main characters<br>and GIVE THE PAGE #:");
jlabQuote.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainCharShows = new JLabel("<html>Explain in your own words what the<br>passage (quote) shows about the main character.");
jlabMainCharShows.setHorizontalAlignment(SwingUtilities.CENTER);
// Add text fields to panel.
jpnlName.add(jlabName);
jpnlName.add(jtfName);
jpnlTitle.add(jlabTitle);
jpnlTitle.add(jtfTitle);
jpnlAuthor.add(jlabAuthor);
jpnlAuthor.add(jtfAuthor);
// Add components to main panel.
jpnl.add(jpnlName);
jpnl.add(jpnlTitle);
jpnl.add(jpnlAuthor);
jpnl.add(jlabSetting);
jpnl.add(jtaSetting);
jpnl.add(jlabMainChars);
jpnl.add(jtaMainChars);
jpnl.add(jlabConflict);
jpnl.add(jtaConflict);
jpnl.add(jlabQuote);
jpnl.add(jtaQuote);
jpnl.add(jlabMainCharShows);
jpnl.add(jtaMainCharShows);
// Add the panel to a scroll pane.
JScrollPane jspPanel = new JScrollPane(jpnl);
// Add the scroll pane to the frame.
jfrm.getContentPane().add(jspPanel);
// Display the frame.
jfrm.setVisible(true);
}
}
Without know exactly the layout you want, the best solution I can suggest is to try the WrapLayout
It addresses the major problem with the FlowLayout, it doesn't wrap.
Why don't you try BoxLayout:
jpnl.setLayout(new BoxLayout(jpnl, BoxLayout.PAGE_AXIS));

Categories

Resources