I want to make my JTextArea field as big as it can be in current JPanel. How to do that?
Now it is like this:
JPanel statusBar = new StatusBar(project);
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox);
The default layout manager of JPanel is FlowLayout, which wouldn't let the text area fill the entire available space in the panel.
Using BorderLayout should work well:
statusBar.setLayout( new BorderLayout() );
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox, BorderLayout.CENTER);
You need a layout manager on the JPanel. If its just the JTextArea contained within it and you need to maximise it you can use a simple GridLayout:
statusBar.setLayout(new GridLayout(1,1));
Related
When i increase the window size to full, my components goes back to standard layout(jtable,button1,button2,button3) and so on. so i wonder if my code is right and how i can decrease the window size.
JTabbedPane jtabbed = new JTabbedPane(JTabbedPane.TOP);
JPanel panel=new JPanel();
tabellinnhold = new DefaultTableModel(defaulttabell,kolonnenavn);
posttabell = new JTable(tabellinnhold);
rullefelt = new JScrollPane(posttabell);
koble = new JButton("koble til");
lukke = new JButton("lukke");
hente = new JButton("Hente data");
avslutt = new JButton("Avslutt");
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);
//action listener
koble.addActionListener(this);
lukke.addActionListener(this);
hente.addActionListener(this);
avslutt.addActionListener(this);
jtabbed.add("se post",panel);
add(jtabbed);
//////////////////////////////////////////////////
Grensesnitt p = new Grensesnitt();
p.setDefaultCloseOperation(EXIT_ON_CLOSE);
p.GUIcode();
p.setTitle("title");
p.setSize(500,700);
p.setVisible(true);
JPanel panel=new JPanel();
...
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);
The default layout manager for a JPanel is a FlowLayout which will simply display all the components on a single line.
You can't just use the BorderLayout constraints and expect it to work.
If you want to use a BorderLayout then the code should be:
//JPanel panel=new JPanel();
JPanel panel=new JPanel( new BorderLayout() );
Also, you can't add 4 components to the "SOUTH" of the BorderLayout. You can only add a single component. So you need to create a child panel and add your components to that first:
JPanel south = new JPanel();
south.add(koble);
south.add(lukke);
south.add(hente);
south.add(avslutt);
panel.add(south, Borderlayout.SOUTH);
Read the section from the Swing tutorial on Using Layout Manager for more information and working examples to get you started.
Keep a link to the tutorial handy for examples of all Swing basics.
I need to totally align components to the JTextArea components, I am currently using a BoxLayout and I already used the setAlignmentX and setHorizontalAlignment to LEFT but it's not working. Here I upload an image to make clearer what I mean. For example look at "+ Pla PLAMARC" it's clearly not aligned with the text area component.
By the moment this is the code:
//Declarations
private JLabel nomPla;
private JTextArea infoPla;
private JScrollPane textAreaScroll;
//Inside the constructor
nomPla = new JLabel();
infoPla = new JTextArea(2, 50);
textAreaScroll = new JScrollPane(infoPla);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
nomPla.setAlignmentX(Component.LEFT_ALIGNMENT);
nomPla.setHorizontalAlignment(nomPla.LEFT);
textAreaScroll.setAlignmentX(Component.CENTER_ALIGNMENT);
this.setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));
this.add(nomPla, BorderLayout.NORTH);
this.add(textAreaScroll, BorderLayout.NORTH); //Orientacions
I am clearly telling nomPla to be on the leftside, but this is not the same as the JTextArea.
How is this done then?
I already used the setAlignmentX and
The setAlignmentX(...) needs to be applied to all the components you add to the BoxLayout if you want all to be left aligned with respect to the BoxLayout.
Edit:
I just want the labels to be on the left side, not the JTextArea components..
Then you need to use a wrapper panel for the BoxLayout Panel.
For example:
JPanel wrapper = new JPanel( new FlowLayout(FlowLayout.CENTER) );
wrapper.add(yourBoxLayoutPanel);
frame.add(wrapper);
Now all the components in the BoxLayout will be left aligned and the BoxLayout panel will be center aligned in the wrapper panel.
Layout management is about nesting panels with different layout manager to achieve you desired effect.
the jscrollpane that I am adding doesnt appearin my textarea
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(textArea);
this.add(scroll);
this.setSize(1000, 600);
this.setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
//this.add(textArea); // get rid of this
this.add(scroll);
You create the scrollpane with the text area, but then the next statement removes the text area from the scrollpane because a component can only have a single parent.
Get rid of that statement and just add the scrollpane to the frame.
Then scrollbars will appear automatically as you add data to the text area.
Also you should create the text area using something like:
textArea = new JTextArea(5, 20);
to give a suggestion on how big to make the text area.
I did what you said but still nothing happens
Another problem is that you need to set the layout manager BEFORE you start adding components to the frame (or panel).
Remove this.add(textArea); and add scroll.setSize( 100, 100 ); will also work for you.
I'm developing a Java application for homework. This is my code
JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
final JCheckBox queryHandlerCB = new JCheckBox();
JPanel checkBoxPanel = new JPanel(new FlowLayout());
checkBoxPanel.add(queryHandlerL);
checkBoxPanel.add(queryHandlerCB);
// Query Panel
// set image
picLabelQuery = new JLabel("",JLabel.LEFT);
picLabelQuery.setIcon(currentPicForm);
JPanel queryPanel = new JPanel();
final JButton queryButton = new JButton("Insert a query");
queryPanel.add(queryButton);
queryPanel.add(picLabelQuery);
// Panel create
final JButton createButton = new JButton("Create");
JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
createPanel.add(createButton);
JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2));
finalPanel.add(queryPanel);
finalPanel.add(checkBoxPanel);
finalPanel.add(createPanel);
finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));
setLayout(new GridBagLayout());
add(finalPanel);
I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.
This piece of code works but this is the result
How do I remove the space that is automatically created between the panels?
How do I remove the space that is automatically created between the panels?
Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.
Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.
Read the section from the Swing tutorial on Layout Managers for more information and examples.
You should pack() your surrounding panel or set the height to a desired value.
My code shows a button inside a textbox, but when the input value changes, the size of the text box also changes. That I don't like. Is there any solution such that the textbox size remains fixed? Or any other idea on how to create a button inside textbox?
The following is my code:
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
Try a BorderLayout and add the textfield at BorderLayout.CENTER and the button at BorderLayout.EAST.
I tried your code, and it seemed to work fine for me, with a slight modification. It may be that panel is resizing itself, not textField. Try wrapping panel in another JPanel to force its shape.
JPanel panel = new JPanel();
JPanel outerPanel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
outerPanel.add(panel);
Your example appears to be the second alternative discussed in the article Component Border. Instead, you may want to install() a ComponentBorder, a third alterative described later in the same article.
Is there any solution such that the textbox size remains fixed?
I'm guessing you are creating the JTextField like:
JTextField textField = new JTextField();
In this case the text field size changes as you add text to it.
Instead you should use:
JTextField textField = new JTextField(10);
and the text field will remain a constant size an hold a minimum of 10 characters before scrolling.
Use a different Layout, or try absolute positioning