I have this code written to make a database connection and add a client:
//adding the left panel
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(250, 500));
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
add(left);
//adding the right panel
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(250, 500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
add(right);
//adding the jlabel title to the left panel
JLabel leftTitle = new JLabel("Add a client");
leftTitle.setAlignmentX(CENTER_ALIGNMENT);
left.add(leftTitle);
//adding the jlabel title to the right panel
JLabel rightTitle = new JLabel("Make a reservation");
rightTitle.setAlignmentX(CENTER_ALIGNMENT);
right.add(rightTitle);
//adding the jlabel "name"
JLabel nameL = new JLabel("Name:");
left.add(nameL);
and I want to move this JLabel here:
I've tried doing nameL.setAlignmentX(LEFT_ALIGNMENT); but it's still not working
Your problem is that you've used a BoxLayout.
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
^^^^^^^^^
Your BoxLayout is set to align things centered along the y-axis, so no amount of setting alignment is going to change that. In order to fix your problem, you need a different layout manager like GroupLayout or CardLayout.
Related
//Attributes
//Stats GUI components
JLabel hp = new JLabel();
JLabel hpPoints = new JLabel("TEST");
JLabel chakra = new JLabel();
JLabel chakraPoints = new JLabel("TEST");
JLabel ryo = new JLabel();
JLabel ryoPoints = new JLabel("TEST");
//Output & Input GUI components
JTextField input = new JTextField();
JTextArea output = new JTextArea(1000, 300);
JPanel statsPanel = new JPanel();
JPanel outputPanel = new JPanel();
JPanel inputPanel = new JPanel();
//Constructor
public Terminal() {
setTitle("Shinobi Shinso");
setSize(1000, 600);
//setResizable(false);
setLocation(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panneau = getContentPane();
panneau.setLayout(new GridLayout(0, 1));
statsPanel.setLayout(new GridLayout(1, 3));
//Output & input
//Add outputPanel to the panneau
panneau.add(outputPanel);
//Add output to outputPanel
outputPanel.add(output);
//Add input to outputPanel
outputPanel.add(input);
input.setColumns(98);
output.setRows(15);
output.setEditable(false);
output.setBackground(Color.BLACK);
output.setForeground(Color.WHITE);
//Add stats panel
panneau.add(statsPanel);
//Statistics
//Health
hp.setIcon(new ImageIcon(new ImageIcon("D:\\eclipse-workspace\\Shinobi Shinso\\images\\scroll-hp.png").getImage().
getScaledInstance(300, 150, Image.SCALE_DEFAULT)));
hp.setHorizontalAlignment(JLabel.CENTER);
statsPanel.add(hp);
hpPoints.setBounds(100, 25, 100, 100);
hp.add(hpPoints);
setVisible(true);
}
Here's how it appears :
I tried to use a JScrollPanel and a lot of obscure coding witchcraft to no avail. I can't seem to find a way to reduce the height of the JPanel containing the pictures.
I deleted 2 of the scrolls in the picture, but I don't think that it will change anything.
I can't seem to find a way to reduce the height of the JPanel containing the pictures.
Don't use a GridLayout as the parent layout manager. The GridLayout makes all components the same size.
I would suggest you don't change the layout manager of the content pane. Leave it as the default BorderLayout.
Then use:
panneau.add(outputPanel, BorderLayout.CENTER);
panneau.add(statsPanel, BorderLayout.PAGE_END);
Also, your creation of the JTextArea is incorrect:
JTextArea output = new JTextArea(1000, 300);
The parameters are for rows/columns, not width/height.
So you should use something like:
JTextArea output = new JTextArea(15, 40);
and a text area is usually added to a JScrollPane so scrollbars can appear when needed.
Read the Swing tutorial for Swing basics. There are section on:
Layout managers
How to Use Text Areas
that should help.
I am trying to make a basic login menu following this mock up :
I decided to put this whole menu into a JPanel so I can switch to another panel once the connexion is successful.
So I decided to use a Borderlayout to have the title in north area and the connect button in the south area .
I made the center of the borderlayout a panel itself . I decided to make it a gridlayout to both have the labels(login,password) but also the textfield in which the user will put his id.
The result is very ugly and very far from what I expected :
Here is the code of the menu :
public class EcranAccueil extends JPanel {
private JLabel labelTitre;
private JPanel PanelConnexion;
private JButton boutonConnexion;
private JLabel labelLogin;
private JLabel labelMotDepasse;
private JTextField loginUser;
private JTextField MotDepasseUser;
EcranAccueil(EcranGestion EcranPrincipale){
PanelConnexion = new JPanel();
this.setLayout(new BorderLayout());
PanelConnexion.setLayout(new GridLayout(2,2));
loginUser = new JTextField("User");
loginUser.setMinimumSize(new Dimension(20,20));
loginUser.setMaximumSize(new Dimension(20,20));
MotDepasseUser = new JTextField("Password");
boutonConnexion = new JButton("Connect");
boutonConnexion.setMinimumSize(new Dimension(200,200));
boutonConnexion.setMaximumSize(new Dimension(200,200));
labelTitre= new JLabel("ApplicationName");
labelLogin= new JLabel("Login");
labelMotDepasse = new JLabel("Password");
PanelConnexion.add(labelLogin);
PanelConnexion.add(loginUser);
PanelConnexion.add(labelMotDepasse);
PanelConnexion.add(MotDepasseUser);
this.add(labelTitre, BorderLayout.NORTH);
this.add(PanelConnexion, BorderLayout.CENTER);
this.add(boutonConnexion, BorderLayout.SOUTH);
} }
I tried to use a gridboxlayout but I completely failed at using it and it did not compile. Does anyone have advices or suggestion?
A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
This also applies to gui: break the design into small, easy to layout containers.
In this case, for example start by dividing the design into 3 areas:
Each such area is implemented by a nested panel.
As you can see in the code, mainPanel is further divided into two nested panels, to ease and improve layout:
class EcranAccueil extends JPanel {
EcranAccueil(){
//Set layout (JPanel uses Flowlayout by default)
setLayout(new BorderLayout(5,5));
// a nested panel for application label
JPanel topPanel = new JPanel();
add(topPanel, BorderLayout.NORTH);
topPanel.setLayout(new FlowLayout(FlowLayout.LEADING));//set
JLabel labelTitre= new JLabel("ApplicationName");
topPanel.add(labelTitre);
// a nested panel for login and password, having two rows
JPanel mainPanel = new JPanel(new GridLayout(2, 1));
add(mainPanel, BorderLayout.CENTER);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
mainPanel.add(loginPanel);
JLabel labelLogin = new JLabel("Login");
loginPanel.add(labelLogin);
JTextField loginUser = new JTextField("User");
loginUser.setColumns(10);
loginPanel.add(loginUser);
JPanel passwordPanel = new JPanel();
passwordPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
mainPanel.add(passwordPanel);
JLabel labelMotDepasse = new JLabel("Password");
passwordPanel.add(labelMotDepasse);
JTextField motDepasseUser = new JTextField("Password");
motDepasseUser.setColumns(10);
passwordPanel.add(motDepasseUser);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
add(buttonPanel,BorderLayout.SOUTH);
JButton boutonConnexion = new JButton("Connect");
buttonPanel.add(boutonConnexion);
}
}
Once you get the basic idea, the layout and its responsiveness can be further improved.
More examples of applying this strategy: 1 2 and 3
I recently started working with Java and I am not too sure how to put my BoxedLayout Panel in the middle of my `JFrame. At the moment, I have the following:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel quizLabel = new JLabel("Java Quiz",SwingConstants.CENTER);
quizLabel.setForeground(Color.BLUE);
quizLabel.setFont(new Font("Arial", Font.BOLD, 20));
quizLabel.setOpaque(true);
panel.add(quizLabel);
JLabel newLineLabel = new JLabel(" ",SwingConstants.CENTER);
newLineLabel.setOpaque(true);
panel.add(newLineLabel);
JLabel createdByLabel = new JLabel("Created By",SwingConstants.CENTER);
createdByLabel.setOpaque(true);
panel.add(createdByLabel);
JLabel nameLabel = new JLabel("XXX",SwingConstants.CENTER);
nameLabel.setOpaque(true);
panel.add(nameLabel);
contentPane.add(panel, BorderLayout.CENTER);
contentPane is taken from my frame. This gives me the following output:
I want the three labels inside the panel to appear in the middle of the Frame.
Because it is the only panel on the screen, the BoxLayout will fill the entire frame and thus depending on how your JComponents are created in the panel, it will show it like that on the frame too.
What I would do if I were you, is created a BorderLayout as a container for your BoxLayout.
This way, you can set your BoxLayout as the center of the Borderlayout.
See if this code works:
//This will fill your frame
JPanel containerPanel = new JPanel(new BorderLayout());
contentPane.add(containerPanel);
//this is the BoxPanel you wnat your components to be organized in
JPanel boxPanel = new JPanel(new BoxLayout());
//Add all your components to the boxPanel
//add your panel with all the components to the container panel
containerPanel.add(boxPanel, BorderLayout.CENTER);
The easiest way is to use a GridBagLayout. Using the default constraints a single component will be centered in the panel:
//contentPane.add(panel, BorderLayout.CENTER);
contentPane.setLayout( new GridBagLayout() );
contentPane.add(panel, new GridBagConstraints());
I created two panels and a main panel. Each panel contains a very large image, and I wanted both of them to be scroll-able to see the rest of the image. But when I add the two panels in the main panel and run it, the first panel is soo big that it covers the second panel. How would I implement ScrollPane for both panels?
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(jPanelSouth, BorderLayout.SOUTH);
add(mainPanel);
//where would I use this?
//scrollPane.setViewportView();
}
}
Each panel contains a very large image>
//JPanel mainPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
You may want to use a GridLayout so that each scroll pane takes up half the frame so as much of each image as possible is displayed.
//JScrollPane scrollPane = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane(jPanelNorth);
The easiest way to use the scroll pane is to create the scrollpane with the component you want displayed and the scrollpane will add the component to the viewport for you.
//mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(scrollPane); // don't need the constraint when using GridLayout.
Then you add the scrollPane to the main panel, since the scrollpane contains the panel with the image.
it seems to use grid layout is much better than using border layout , in this case :
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
//1. use GridLayout with 2 rows and 1 column .
JPanel mainPanel = new JPanel(new GridLayout(2,1));
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
//2.you should place .setViewportView() here :
scrollPane.setViewportView(jPanelNorth);
scrollPane2.setViewportView(jPanelSouth);
mainPanel.add(scrollPane);//is in the top ("North")
mainPanel.add(scrollPane2);//next ("South")
//3.use setContentPane instead of add()
setContentPane(mainPanel);
}
}
I have a JTextArea in a JPanel. How can I have the JTextArea fill the whole JPanel and resize when the JPanel resizes and scroll when too much text is typed in?
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); //give your JPanel a BorderLayout
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space
See http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html or http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html for more details on JScrollPane
Place the JTextArea inside of a JScrollPane, and place that into the JPanel with with a layout that fixes the size. An example with a GridBagLayout, for instance could look like this:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane();
GridBagConstraints cons = new GridBagContraints();
cons.weightx = 1.0;
cons.weighty = 1.0;
panel.add(scrollPane, cons);
JTextArea textArea = new JTextArea();
scrollPane.add(textArea);
This is only a rough sketch, but it should illustrate how to do it.