This is a piece of code generated from netbeans Swing gui designer.
I'm trying to do the following: jLabel1 and jLabel2 will contain only image icon with dimension 52x46 px without text, they should be fixed in the left and right side of the line, jTextField2 is expected to fill the gap between jlabels and autoresize to full screen/view width.
Problem is, that jTextField2 remains with same width no matter what size of window/view is... The initial width is dependent on the length of hard-coded text inside the field...
Do you have any idea, how to do this?
private void initComponents() {
javax.swing.JLabel jLabel1;
javax.swing.JLabel jLabel2;
javax.swing.JTextField jTextField2;
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
jLabel1.setText("ABC");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
getContentPane().add(jLabel1, gridBagConstraints);
jTextField2.setText("some text field content");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane().add(jTextField2, gridBagConstraints);
jLabel2.setText("ABC");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
getContentPane().add(jLabel2, gridBagConstraints);
pack();
}
See documentation of GridBagLayout
GridBagConstraints.weightx, GridBagConstraints.weighty
Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least
one component in a row (weightx) and column (weighty), all the
components clump together in the center of their container. This is
because when the weight is zero (the default), the GridBagLayout
object puts any extra space between its grid of cells and the edges of
the container.
jTextField2.setText("some text field content");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
getContentPane().add(jTextField2, gridBagConstraints);
Related
Currently my JTextField component is sizing to what appears to be 0. I have attempted to setPreferredSize, setSize(new Dimension), added columns & removed the anchor with GridBagConstraints. Nothing has worked thus far. Here is my code:
private void createPayFrame() {
JFrame payFrame = new JFrame();
payFrame.setSize(new Dimension(450, 300));
payFrame.setLocationRelativeTo(null);
payFrame.setLayout(new GridBagLayout());
JLabel payment = new JLabel("<html>Your bill has been generated. It is located in your "
+ System.getProperty("user.home") + "\\eclipse-workspace\\Turbo Team directory</html>");
GridBagConstraints gbc = new GridBagConstraints();
JTextField payField = new JTextField(10);
payField.setToolTipText("Enter in the amount due here");
gbc.anchor = GridBagConstraints.ABOVE_BASELINE;
gbc.insets = new Insets(0,0,20,0);
payFrame.add(payment, gbc);
gbc.gridy = 1;
gbc.insets = new Insets(10,0,0,0);
payFrame.add(payField, gbc);
payFrame.setVisible(true);
}
This is what it looks like when I run the code:
Any idea what is going on here?
Set the fill for the textfield
gbc.fill = GridBagConstraints.HORIZONTAL;
I've had this problem with GridBagLayout when the JTextField was too large to display. Have you tried with less columns? Maybe try to start at new JTextField(1) and work your way up to see if it works at smaller lengths.
I have a simple GUI with multiple tabs like that:
GUI
The problem is that, after filling the text area up to the bottom (filled console) and switching the tab - it completely breaks the markup (bottom-broken)
Here is the code example of the textArea and Constraints
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
tabbedPanelOne.add(textArea);
CustomAppender.setTextArea(textArea);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 5;
gbc.weightx = 1.0;
gbc.weighty = 10.0;
Font font = textArea.getFont();
float size = font.getSize() - 4.0f;
textArea.setFont(font.deriveFont(size));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//textArea.setPreferredSize(new Dimension(500, 300));
layout.setConstraints(textArea, gbc);
/*
* Scrolls for TextArea
*/
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPanelOne.add(scroll);
layout.setConstraints(scroll, gbc);
//textArea.setPreferredSize(new Dimension(500, 300)); works fine, but it disables vertical scroller.
layout.setConstraints(textArea, gbc);
First of all when add a component to a scrollpane you would never attempt to set constraints on the component. A JScrollPane uses its own custom layout manager so you don't do anything special when you add the component to the viewport of the scroll pane.
However, the simplest solution would be to use a BorderLayout on the panel you add to the tab. Something like:
JPanel top = new JPanel();
top.add(...);
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel main = new JPanel( new BorderLayout() );
main.add(top, BorderLayout.PAGE_START);
main.add(scrollPane, BorderLayout.CENTER);
Now the scroll pane will take up all the available space in the tab and scrollbars will appear when needed.
I'm trying to create a simple UI for a program that can read a file, write to a file and search for text within a file. I have most of my Components created, the problem is that they're all being "drawn" in the same (center) cell. I've tried applying weights, widths, etc. all to no avail.
Here's my base code for the UI:
public void GUI(){
//Create main window for Program
JFrame mainWindow = new JFrame("Simple Data Base"); //Init frame
mainWindow.setSize(500, 400); //Set frame size
mainWindow.setVisible(true); //Make frame visible
//Create panel for the main window of the GUI
JPanel simpleGUI = new JPanel( new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
simpleGUI.setBackground(Color.cyan);
//Create button linking to read function
JButton readButton = new JButton("Read"); //Init button, and give text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
//Create button linking to the search function
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
//Create label prompting user to specify desired function
JLabel promptText = new JLabel("Click 'Read' to read a file, 'Search' to search within a file, 'Write' to write to a file:");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 0;
//Add components to Main window
mainWindow.getContentPane().add(simpleGUI);
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
}
the problem is that they're all being "drawn" in the same (center) cell.
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
You are using the same GridBagConstraints for each component so the contraints are identical for each component.
You neeed to:
set the constraints
add the component to the panel using the constraints
repeat steps 1 and 2.
For example:
JButton readButton = new JButton("Read");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
simpleGUI.add(readButton, gbCons);
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
simpleGUI.add(searchButton, gbCons);
I suggest you read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.
Download the demo code and use that example as your starting code. The demo code will show you how to better structure your class by:
NOT extending JFrame
Creating the GUI on the Event Dispatch Thread
Use the pack() method, NOT the setSize(...) method
Make the frame visible AFTER all components have been added to the frame
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.
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);