set JButton size in gridLayout - java

How to set the size of jbutton in gridlayout? otherwise the button size have lot of width and its not listening setSize or setBounds or setPreferedSize.
addBut = new JButton("Add");
addBut.addActionListener(this);
deleteBut = new JButton("Delete");
deleteBut.addActionListener(this);
selectionPanel = new JPanel();
selectionPanel.setLayout(new GridLayout(2,2));
TitledBorder selectionBorder = new TitledBorder("Options");
selectionBorder.setTitleColor(Color.BLUE);
selectionPanel.setBorder(selectionBorder);
selectionPanel.add(new JLabel("Department Name"));
selectionPanel.add(new JTextField(deptName));
selectionPanel.add(addBut);
selectionPanel.add(deleteBut);
selectionPanel.setPreferredSize(new Dimension(900,100));

I believe setting GridLayout(2,2) will override size changes made to the panels. To be more precise, use GridBagConStraints;
private JTextField field1 = new JTextField();
private JButton addBtn = new JButton("Save: ");
public void addComponents(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Components
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(field1, c);
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(addBtn, c);
}
public MainView() {
//Create and set up the window.
JFrame frame = new JFrame("NAME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponents(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(400, 125);
frame.setLocation(400, 300);
}

Related

The component doesn't display correctly with GridBagLayout

I'm learning java swing and this is very confused to me. The quit button doesn't display. However, if I move the code part of textArea after the two parts of buttons, it will display correctly. So why?
package exercise1;
import javax.swing.*;
import java.awt.*;
public class ChatClient {
private JTextArea textArea;
private JTextField textField;
private JButton btnSend;
private JButton btnQuit;
private JFrame frame;
private JPanel panel;
private JScrollPane scrollPane;
private void launchFrame() {
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
textArea = new JTextArea(10, 50);
scrollPane = new JScrollPane(textArea);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
panel.add(scrollPane, c);
btnSend = new JButton("Send");
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnSend, c);
btnQuit = new JButton("Quit");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnQuit, c);
}
protected ChatClient() {
frame = new JFrame("Chat Room");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
launchFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ChatClient client = new ChatClient();
}
}
Simple: You forgot to reset c.gridheight = 1; after adding the JScrollPane. Without doing this, the send button will overlie the quit button.
private void launchFrame() {
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL; // ** This is also worthwhile **
textArea = new JTextArea(10, 50);
scrollPane = new JScrollPane(textArea);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
panel.add(scrollPane, c);
btnSend = new JButton("Send");
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1; // ********* ADD THIS *********
c.anchor = GridBagConstraints.NORTH;
panel.add(btnSend, c);
btnQuit = new JButton("Quit");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnQuit, c);
}

Java add jscrollpane not work

I would like to add a JScrollpane into an existing Jpanel, I created a for loop for adding some buttons, but are not displayed. This is the code, but nothing is displayed inside the Jpanel
public class Main extends javax.swing.JFrame {
public Main() {
initComponents();
//EXISTING JPANEL
gridPanel = new JPanel(new GridLayout(0, 1));
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(gridPanel, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
Avvio();
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
for (int i = 0; i < 10; i++) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
}
}

How do I align components in a JPanel to the left?

I have created a layout for a basic text editor, I would like the top bar to have the buttons alligned to the left, the top bar is a JPanel and it is using the FlowLayout manager. It is inside of a grid using the GridBag layout manager. Any suggestions?
import java.awt.*;
import javax.swing.*;
public class GridTest {
private static JButton firstButton, secondButton, thirdButton;
private static JPanel panel, sidebar, infoPanel;
private static JTextArea textArea;
public static void addComponentsToPane(Container container) {
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
firstButton = new JButton("Button 1");
secondButton = new JButton("Button 2");
thirdButton = new JButton("Button 3");
panel = new JPanel(new FlowLayout());
sidebar = new JPanel(new FlowLayout());
infoPanel = new JPanel(new FlowLayout());
textArea = new JTextArea("This is some generic text!");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.weighty = 0;
c.weightx = 1;
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START;
container.add(panel, c);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(firstButton);
panel.add(secondButton);
panel.add(thirdButton);
c.fill = GridBagConstraints.BOTH;
c.ipady = 300;
c.ipadx = 100;
c.weighty = 1;
c.weightx = 0;
c.gridx = 0;
c.gridheight = 2;
c.gridwidth = 1;
c.gridy = 1;
container.add(sidebar, c);
sidebar.setBorder(BorderFactory.createLineBorder(Color.black));
c.fill = GridBagConstraints.BOTH;
c.ipady = 40;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 2;
c.gridheight = 1;
c.gridx = 1;
c.gridy = 1;
container.add(textArea, c);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 0;
c.weightx = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 2;
container.add(infoPanel, c);
infoPanel.setBorder(BorderFactory.createLineBorder(Color.black));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Grid test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(800,600);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Consider using a JToolBar instead of a JPanel, as it has it's own layout manager, which, basically, does this any way.
Images from following tutorial...
Check out How to use Tool Bars for more details
One way: Change your FlowLayout to one that prefers to place its components on the left:
panel = new JPanel(new FlowLayout(FlowLayout.LEADING));

JScrollPane not properly stretching horizontal distance in GridBagLayout

I've seen other posts on this subject, but the solutions they found do not apply to me. I am setting a weighted value and using the c.fill = GridBagConstraints.BOTH constraints as well.
I'm including the whole GUI code I have, just in case my mistake is coming form something other than the GridBagLayout.
I want the scrollable text block on the right to expand the remaining space within the GUI and I have set all the variables that should be attributed to that and yet it still isn't working. What am I doing wrong?
My result:
import java.awt.*;
import javax.swing.*;
public class TestCode extends JFrame {
JTextArea textArea = new JTextArea ();
JComboBox <String> typeComboBox;
JTextField searchField;
JTextField fileField;
public TestCode(){
setTitle ("GUI Test");
setSize (600, 300);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
JScrollPane scrollPane = new JScrollPane(textArea);
JButton readButton = new JButton("Read File");
JButton displayButton = new JButton("Display");
JButton searchButton = new JButton("Search");
searchField = new JTextField(10);
fileField = new JTextField(15);
typeComboBox = new JComboBox <String> ();
typeComboBox.addItem("Index");
typeComboBox.addItem("Type");
typeComboBox.addItem("Name");
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
container.setPreferredSize(new Dimension(250, 100));
JPanel filePanel = new JPanel();
filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
filePanel.add(new JLabel("Source file", SwingConstants.LEFT));
JPanel filePanelTop = new JPanel();
filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
filePanelTop.add(fileField);
JPanel filePanelBottom = new JPanel();
filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
filePanelBottom.add(readButton);
filePanelBottom.add(displayButton);
filePanel.add(filePanelTop);
filePanel.add(filePanelBottom);
filePanel.setMaximumSize(filePanel.getPreferredSize());
filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
searchPanel.add(new JLabel("Search target", SwingConstants.LEFT));
JPanel searchPanelTop = new JPanel();
searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
searchPanelTop.add(searchField);
searchPanelTop.add(typeComboBox);
searchPanel.add(searchPanelTop);
searchPanel.add(searchButton);
searchPanel.setMaximumSize(searchPanel.getPreferredSize());
searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
container.add(filePanel, c);
c.gridx = 0;
c.gridy = 1;
container.add(searchPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTHWEST;
container.add(scrollPane, c);
add(container, BorderLayout.WEST);
validate();
} // end method toString
public static void main(String[] args){
TestCode run = new TestCode();
}
} // end class Treasure
//add(container, BorderLayout.WEST);
add(container);
The West contrains the components to their preferred width. The default is the CENTER which allows components to expand to fill the space available.
Also, the main structure of you code is wrong. You should be adding all the component to the frame first and then invoke:
frame.pack();
frame.setVisible(true);
Then there is no need for the validate().

(Java Newbie - Panel Transitions) How do I switch between panels in a frame

I have the following simple code and I don't know how to modify it so as to have 3 separate panels to switch to, one for each button:
package TouristLocations;
import javax.swing.*;
import java.awt.*;
public class buildApp extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel title = new JLabel("Locations");
title.setFont(new Font("Serif", Font.BOLD, 40));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
frame.add(title, c);
JButton b1 = new JButton("View Locations");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
frame.add(b1, c);
JButton b2 = new JButton("Insert Locations");
c.gridx = 1;
c.gridy = 1;
frame.add(b2, c);
JButton b3 = new JButton("Help");
c.gridx = 2;
c.gridy = 1;
frame.add(b3, c);
TextArea text1 = new TextArea(15,40);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
frame.add(text1, c);
frame.pack();
}
}
thank you
Sounds like you should consider using JTabbedPane.
In addition to How to Use Tabbed Panes, you may want to look at CardLayout, mentioned here and here.
You should create a main container:
JPanel mainContainer = new JPanel();
//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
... so each button must add those panels into the main container and resize new panel size, something like this:
//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());
... for button2 action, you must follow the same way above.

Categories

Resources