How to make JTextField as wide as the window - java

I want to make the text automatically as wide as the window. I tried using text.setSize(window.getWidth(),20) and text.setBounds(window.getWidth(),20), (where text is JTextfield), but the only way that seems to work is: static JTextField text = new JTextField(int numberOfColumns); I'm using GridBag layout.

EDIT: I have edited example according to GridBagLayout.
Use layout manager. It will automatically expands component according to window.
For example;
Jpanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = GridBagConstraints.REMAINDER;
panel.add(textfield,c);

Related

JEditorPane is blocking the divider of a JSplitPane

In my application I'm using a JSplitPane to devide the GUI into two sides. Every side will contain some components, one of them being a JEditorPane. The components are placed horizontally next to each other. All components should have a fixed size exept for the JEditorPanel. This should take all the available horizontal space. So, if the divider of the splitPane is moved the JEditorPane should change its width. My current implementation works fine if the divider is moved in a direction where the with of the JSplitPane increases. However, it is not possible to move the divider in the other direction. Below is a minimalistic example of my problem. In the example I'm using a GridbagLayout. However, this problem remains also for other layout manager. Also interesting is that this problem seems to be unique for the JEditorPane. The implementation works fine when the JEditorPane is replaced, for example by a JButton.
Here is my code:
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelRight = new JPanel();
JPanel panelLeft = new JPanel();
panelRight.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton b = new JButton("button");
c.gridx = 1;
c.gridy = 0;
panelRight.add(b, c);
JEditorPane ep = new JEditorPane();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
panelRight.add(ep, c);
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
sp.setResizeWeight(0.5);
f.add(sp);
f.setVisible(true);
}
}
Does anyone have an idea why this is happending and/or how to fix it?

JScrollPane and GridBagLayout

I want to create two non-editable textboxes (each will contain only one line) with a fixed size, but I want them to be scrollable (horizontally only) because I know the text they will contain will be very long. I want them to be below the two buttons I define below, and I want each textbox on their own row.
Problem is, everything shows up and buttons work as expected, but the textbox won't scroll, although I can somehow drag and select the rest of the text in the box that isn't visible. I don't know if labels are scrollable, would they be a better option?
Code:
public static void main(String[] args)
{
JFrame win = new JFrame("Window");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(400, 300);
GridBagConstraints c = new GridBagConstraints();
win.setLayout( new GridBagLayout() );
JTextArea master = new JTextArea(1,1);
JTextArea vendor = new JTextArea(1,1);
master.setEditable(false);
vendor.setEditable(false);
master.setPreferredSize( new Dimension(100,20) );
vendor.setPreferredSize( new Dimension(100,20) );
master.setText(/*some really long string*/);
vendor.setText(/*some really long string*/);
JScrollPane mPane = new JScrollPane(master, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane vPane = new JScrollPane(vendor, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mPane.getHorizontalScrollBar().isVisible();
vPane.getHorizontalScrollBar().isVisible();
JButton one = new JButton("Select");
ActionListener select = new SelectButton(master, vendor);
one.addActionListener(select);
JButton two = new JButton("Run");
c.gridx = 0;
c.gridy = 0;
win.add(one, c);
c.gridx = 1;
c.gridy = 0;
win.add(two, c);
c.gridx = 0;
c.gridy = 1;
win.add(master, c);
win.add(mPane, c);
c.gridx = 0;
c.gridy = 2;
win.add(vendor, c);
win.add(vPane, c);
win.setLocationRelativeTo(null);
win.setVisible(true);
return;
}
Don't, ever, use setPreferredSize! This is overriding the information that the JScrollPane needs in order to make decisions about how the components should be scrolled. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details. Instead use the JTextArea(int, int) constructor to provide hints to the JScrollPane, for example JTextArea master = new JTextArea(1, 20);. Any text longer then 20 characters will cause the JScrollPane to display the horizontal scroll bar...
Don't add both the JTextArea AND the JScrollPane to the container. Adding the JTextArea automatically removes it from the JScrollPane, which isn't what you want.
Use GridBagConstaints#gridwidth to control the number of columns a component might expand across to help fix your layout...
For example...
c.gridx = 0;
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
win.add(mPane, c);
c.gridx = 0;
c.gridy = 2;
win.add(vPane, c);
I'm hoping this is a really simple example, if not, you should always make sure that your UI is created and modified from within the context of the EDT. See Initial Threads for more details

Please suggest me a basic layout to use, other than the GridBag layout

OK I have a panel, and I want to place two panels on to it, left and right, such that the right panel should be double the width of the left side panel.
I want to add a menu to the left side panel, and the details of the selected item will appear in the right side panel.
Secondly, the size of the panels and their components should increase proportionately when the window is expanded (If any methods can be used for the purpose, please suggest!)
I did try to use the GridBagLayout but I think I have not yet been able to grasp it well. So please suggest the simplest layout manager which can serve my purpose.
EDIT:- PROBLEMS WITH WHAT I HAD TRIED WITH THE GRIDBAG
//Set Layout
setLayout(new GridBagLayout());
GridBagConstraints c= new GridBagConstraints();
//Set Layout constraints of components and add them to the MainPanel
c.gridx=0;
c.gridy=0;
c.weightx=0.5;
c.insets= new Insets(5,5,5,5);
c.fill=GridBagConstraints.BOTH;
add(iListPanel);
iListPanel.setBorder(BorderFactory.createTitledBorder("Check") );
GridBagConstraints c1= new GridBagConstraints();
c.gridx=1;
c.gridy=0;
c.weightx=1;
c.insets= new Insets(5,5,5,5);
c.fill=GridBagConstraints.BOTH;
add(iDetailsPanel);
iDetailsPanel.setBorder(BorderFactory.createTitledBorder("Check"));
Of the layout managers that come with Java, I think GridBagLayout is the simplest one that will do that. It's worth the time to learn it, because it's about the only layout manager that's halfway-competent. This should do it:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c;
JPanel left = new JPanel();
left.setBorder(BorderFactory.createTitledBorder("Left"));
c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.fill = c.BOTH;
panel.add(left, c);
JPanel right = new JPanel();
right.setBorder(BorderFactory.createTitledBorder("Right"));
c = new GridBagConstraints();
c.weightx = 2;
c.weighty = 1;
c.fill = c.BOTH;
panel.add(right, c);
However, from the description of your problem, it sounds like JSplitPane will serve you better for this. It's a ready-made component to do more-or-less what you're asking, and has a user-resizeable separator as well. An example:
JSplitPane pane = new JSplitPane();
pane.setResizeWeight(1/3f); // right will be twice size of left
JPanel left = new JPanel();
left.setBorder(BorderFactory.createTitledBorder("Left"));
pane.setLeftComponent(left);
JPanel right = new JPanel();
right.setBorder(BorderFactory.createTitledBorder("Right"));
pane.setRightComponent(right);
Edit: The problem with your original code is that it does not use the constraints.
add(iListPanel);
should be:
add(iListPanel, c);
and likewise for iDetailsPanel.
You want to use a BorderLayout.
panel.add(panelLeft,"West");
panel.add(panelRight,"East");
North, South and Center is also available

GridBagLayout: how to fill all empty spaces

I've have a JFrame contains some JPanels using a gridBagLayout (3 rows, one column). That's my code:
Container main_container = getContentPane();
GridBagLayout layout = new GridBagLayout();
main_container.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
StatoMagazzini jpanel_stato_magazzini = new StatoMagazzini();
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
layout.setConstraints(jpanel_stato_magazzini, c);
AcquistoLotto jpanel_acquisto = new AcquistoLotto(i, jpanel_stato_magazzini);
c.gridx = 1;
c.gridy=1;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
layout.setConstraints(jpanel_acquisto, c);
ButtonPanel jpanel_button_panel = new ButtonPanel(i);
c.gridx=1;
c.gridy=3;
c.anchor = GridBagConstraints.CENTER;
layout.setConstraints(jpanel_button_panel, c);
main_container.add(jpanel_acquisto);
main_container.add(jpanel_stato_magazzini);
main_container.add(jpanel_button_panel);
pack();
and that's the result (a ugly result): https://docs.google.com/file/d/0Bxi2arJ2Dv9xbEo0Smd5QUN4UGc/edit?usp=sharing
i would eliminate that empty spaces on top and extend the second component (that is a scrollable JTable). How i should modify code?
When a GridBagLayout has more space than it needs, it distributes that extra space using the weightx and weighty properties of each cell. If no cells have a non-zero weight property, the extra space is not allocated to any cell, and instead all cells are centered, and sized to their preferred width/height.
If you do c.weighty = 1 for the constraints used by the component containing the JTable, that component will be allocated all extra vertical space. You may also want to do c.weightx = 1 so the table will fill all horizontal space.
you could set the layout of the container to BorderLayout (or something equivalent) and create an extra JPanel with the GridBagLayout and add it to the container
because of Borderlayout, the JPanel will take as much space as possible

Starting GridBagLayout from top left corner in Java Swing

I'm new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.
I'd appreciate if you could help me by telling what I need to do after this point:
JPanel panel = new JPanel(new GridBagLayout());
frame.add(panel);
GridBagConstraints c = new GridBagConstraints();
I know that I have to use NORTHWEST or FIRST_LINE_START constants, but I don't know how. I tried to do it this way' but it did not realize the constants.
frame.getContentPane().add(panel, BorderLayout.NORTHWEST);
Thanks for your help.
Read the section from the Swing tutorial on How to Use GridBagLayout. The secton on "weightx,weighty" states:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.
You need to use your GridBagConstraints' anchor property. This should do it for you:
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
frame.add(panel, gbc);
I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set weightx and weighty to be 1 so that the panel takes up all of the available space given to it.
For those, who use IDE (e.g. NetBeans), I finally found nice trick: if you want to add components to top and use their preferred sizes: add another empty panel with weighty = 1.0. Copied from auto-generated code (NetBeans):
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.weighty = 1.0;
jPanelOptions.add(jPanelFiller, gridBagConstraints);
a quick and simple way:
add a blank JLabel to end of page:
// your code goes here
gbc.weightx = 1;
gbc.weighty = 1;
bg.add(new JLabel(" "), gbc); // blank JLabel
There's a workaround. You can put the GridBagLayout panel in a BorderLayout panel with BorderLayout.NORTH. Then Component in GridBagLayout panel will start from top position.
static void test4(){
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 360);
JPanel borderLayoutPanel=new JPanel(new BorderLayout());
JPanel gridBagLayoutPanel = new JPanel(new GridBagLayout());
borderLayoutPanel.add(gridBagLayoutPanel, BorderLayout.NORTH);
frame.add(borderLayoutPanel);
JButton testButton=new JButton("test button");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
gridBagLayoutPanel.add(testButton, c);
frame.setVisible(true);
}
if you want the grid to go all the way to the top, simply set all your weighty = 0 until the last item, set it to any number greater than 0, it will effectively push the rest of the buttons to the top.
Don't forget to also increment your button's gridy value.
Otherwise it will be centered.
(the same can be done using gridx and weightx value if you arent using the c.fill = GridBagConstraints.HORIZONTAL property.)

Categories