I am trying to implement a JAVA GUI app, but I am facing a problem that I could not find the solution anywhere about how to move the JTextArea to a certain place on the panel. I took a picture of the software.
I have tried many ways to make the white JTextArea object placed below the big JTextArea object on the right panel. Does anyone have any idea about this? Like how to move the JTextArea object around the others, and how to make the JButton/JTextArea objects stay in the fixed place?
public class menu extends JFrame {
private JButton generate, sort;
private final JTextArea TEXT_AREA_EAST = new JTextArea(30,40);
private final JTextArea TEXT_AREA_WEST = new JTextArea(30,40);
private final JTextArea TEXT_AREA_EAST_BELOW = new JTextArea(5,30);
private void display() {
setTitle ("Sorting Algorithm Runtime Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,300);
setLocationRelativeTo(null);
setLayout(new GridLayout(5,2,5,5));
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.white);
contentPane.setBorder(
BorderFactory.createEmptyBorder(horizontal, horizontal, horizontal, horizontal));
contentPane.setLayout(new BorderLayout(horizontal, vertical));
ButtonGroup bg = new ButtonGroup();
rightPanel = new JPanel();
rightPanel.setOpaque(true);
rightPanel.setBackground(Color.yellow);
border = BorderFactory.createTitledBorder("Sorted List");
border.setTitleJustification(TitledBorder.CENTER);
rightPanel.setBorder(border);
sort = new JButton("Sort");
rightPanel.add(sort, JPanel.TOP_ALIGNMENT);
rightPanel.add(TEXT_AREA_EAST, BorderLayout.EAST);
rightPanel.add(TEXT_AREA_EAST_BELOW, BorderLayout.SOUTH);
TEXT_AREA_EAST.setBackground(Color.CYAN);
TEXT_AREA_EAST.setEditable(false);
This is how the app look like
Related
I have a class that extends JFrame and works by adding in 2 panels with BoxLayout buttons, and one JTabbedPane in the center which displays graphs.
I want one of the buttons to remove all current components in the frame and add new ones.
Here are the methods used.
private void createAndShowGraphs() {
ImageIcon createImageIcon(lsuLettersPath); //simple png file to fill one tab
final JTabbedPane jtp = new JTabbedPane();
JLabel iconLabel = new JLabel();
iconLabel.setOpaque(true);
jtp.addTab(null, icon, iconLabel);
//Here is where the errors begin
JPanel menu = new JPanel();
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
//I want this button to remove all components currently in the JFrame and replace them with new components specified in the createAndShowIntro() method
menu.add(new JButton(new AbstractAction("Intro Pane") {
public void actionPerformed(ActionEvent e) {
//I've also tried putting removeAll in the Intro method
removeAll();
createAndShowIntro();
}
}));
add(jtp, BorderLayout.CENTER);
add(menu, BorderLayout.WEST);
pack();
setVisible(true);
}
private void createAndShowIntro() {
System.out.println("Made it to Intro");
//all I want is a blank JLabel with the String "test" to show up
JPanel test = new JPanel();
test.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setText("test");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
test.add(label);
add(test, BorderLayout.CENTER);
test.revalidate();
label.revalidate();
validate();
test.repaint();
label.repaint();
repaint();
pack();
setVisible(true);
}
When I call createAndShowGraphs() in main() and then hit the 'Intro' button, everything freezes and nothing is actually removed. I know it makes it the Intro method because of the "Made it to Intro" string output to the terminal.
I've tried all kinds of combinations of invalidate(), validate(), revalidate(), repaint() on the labels and on the frame itself. Really frustrated because I don't know how else I'm going to be able to display 3 different screens to switch back and forth between while only actually displaying one at a time.
Thanks for your time.
First time posting here, and let me first say that I'm a bit of a Java noob, I've only started learning it in uni this year.
So anyway, I have to create a UI for a Tamagotchi project, and I'm trying to create it using JFrame etc.
This is what I'm trying to create:
This is my code so far:
import javax.swing.*;
import java.awt.*;
public class DogUI {
private JFrame DogUI;
private JPanel leftPanel, topPanel, bottomPanel, rightPanels;
private JButton jb;
private JLabel lb, lb1, lb2;
public DogUI() {
GUI();
}
public void GUI() {
DogUI = new JFrame("Dog UI");
DogUI.setSize(800, 600);
DogUI.setResizable(false);
leftPanel = new JPanel();
leftPanel.setBackground(Color.green);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
topPanel = new JPanel();
topPanel.setBackground(Color.white);
bottomPanel = new JPanel();
bottomPanel.setBackground(Color.red);
rightPanels = new JPanel();
rightPanels.setLayout(new BoxLayout(rightPanels, BoxLayout.X_AXIS));
DogUI.setVisible(true);
lb = new JLabel("Name: ");
leftPanel.add(lb);
lb1 = new JLabel("Image");
topPanel.add(lb1);
lb2 = new JLabel("Buttons");
bottomPanel.add(lb2);
rightPanels.add(topPanel);
rightPanels.add(bottomPanel);
DogUI.add(rightPanels);
}
public static void main(String [] args) {
new DogUI();
}
}
This is what I end up with:
I'm sure it's something simple, or perhaps I am going the complete wrong way about doing it, but please try explain it in layman's terms if possible.
Thanks.
Chris.
You are only adding the rightPanels to the frame, you never add leftPanel
The rightPanel is using X_AXIS which is horizontal. You want Y_AXIS
When you do add the leftPanel, you're going to want to set your frame's layout to GridLayout(1, 2). See GridLayout
setVisible after adding all your components.
Follow Java namingConvention. variables begin with lower case, using camel casing. DogUI → dogUI
Swing apps should be run from the Event Dispatch Thread (EDT). You can do this by wrapping the code in the main in a SwingUtilities.invokeLater(...). See more at Initial Threads
Try using a GridLayout instead.
public void GUI() {
DogUI = new JFrame("Dog UI");
DogUI.setSize(800, 600);
DogUI.setResizable(false);
DogUI.setLayout(new GridLayout(1,2));//1row 2col
leftPanel = new JPanel();
leftPanel.setBackground(Color.green);
topPanel = new JPanel();
topPanel.setBackground(Color.white);
bottomPanel = new JPanel();
bottomPanel.setBackground(Color.red);
rightPanels = new JPanel();
rightPanels.setLayout(new GridLayout(2,1));//2row 1col
lb = new JLabel("Name: ");
leftPanel.add(lb);
lb1 = new JLabel("Image");
topPanel.add(lb1);
lb2 = new JLabel("Buttons");
bottomPanel.add(lb2);
rightPanels.add(topPanel);
rightPanels.add(bottomPanel);
DogUI.add(leftPanel);
DogUI.add(rightPanels);
DogUI.setVisible(true);
}
It sounds like you want a parent JPanel with a BorderLayout. That parent JPanel contains two other panels, on the east and west side of it. The west side can contain your progress panel, and the east side contains another JPanel, which has a GridLayout of 1 column and 2 rows, or alternatively, a BorderLayout. From there you can add the other two JPanels to that last JPanel which represent whatever that right hand side of the picture shows.
The overall parent JPanel could also be a GridLayout with 2 columns and 1 row, but a BorderLayout might look better as you might find one side of the application might not need as much space and might limit how much space it takes up. Perhaps the east panel should be a BorderLayout too as your image might not take up that much space on the north side giving the components on the south side the rest of the container's available space.
I am trying to make a Rock, Paper, Scissors game and I have added 3 buttons to the frame, however when I launch the program two of the buttons don't appear until you hover over them, anyone have any idea why?
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.FlowLayout;
import javax.swing.JOptionPane;
public class RPSFrame extends JFrame {
public static void main(String [] args){
new RPSFrame();
}
public RPSFrame(){
JFrame Frame1 = new JFrame();
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Rock, Paper or Scissors game");
this.setLocationRelativeTo(null);
ClickListener cl1 = new ClickListener();
ClickListener cl2 = new ClickListener();
ClickListener cl3 = new ClickListener();
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Result:");
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 25));
this.add(panel1);
this.setVisible(false);
JPanel panel2 = new JPanel();
JButton Rock = new JButton("Rock");
Rock.addActionListener(cl1);
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel2.add(Rock);
this.add(panel2);
this.setVisible(true);
JPanel panel3 = new JPanel();
JButton Paper = new JButton("Paper");
Paper.addActionListener(cl2);
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
panel3.add(Paper);
this.add(panel3);
this.setVisible(true);
JPanel panel4 = new JPanel();
JButton Scissors = new JButton("Scissors");
Scissors.addActionListener(cl3);
panel4.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel4.add(Scissors);
this.add(panel4);
this.setVisible(true);
}
private class ClickListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == "Rock"){
int AI = new Random().nextInt(3) + 1;
JOptionPane.showMessageDialog(null, "I have been clicked!");
}
}
}
}
The setVisible(true) statement should be invoked AFTER all the components have been added to the frame.
You currently have two setVisible(...) statements, so you need to get rid of the first one.
Edit:
Took a second look at the code. You have multiple setVisible(...) statements. Get rid of them all except for the last one.
Don't create separate panels for each button. Instead you create one panel (called buttonPanel) for all the buttons. In your case you might use a horizontal BoxLayout. Add a button to the panel, then add glue then add a button, then add glue and then add your final button. Then add this buttonPanel to the NORTH of the frame. ie. this.add(buttonPanel, BorderLayout.NORTH). Read the section from the Swing tutorial on How to Use Box Layout for more information on how the layout works and on what glue is.
The problem is that JFrame has a default BorderLayout. When you just add(component) without specifying a BorderLayout.[POSITION] e.g add(panel, BorderLayout.SOUTH), then the component will get added to the CENTER. The problem with that is each POSITION can only have one component. So the only component you see id the last one you add.
Now I don't know after specifying the positions, if you will get your desired result. A BorderLayout may not be the right fit. But just to see a change, you can set the layout to GridLayout(0, 1) and you will see the component.
this.setLayout(new GridLayout(0, 1));
If this is not the result you want, then you should look over Laying out Components within a Container to learn the different layouts available to you.
Also as I pointed out in my comment
if(e.getSource() == "Rock"){
with the above, you are trying to compare an object (ultimately a button) with a String. Instead you will want to compare the actionCommand
String command = e.getActionCommand();
if("Rock".equals(command)){
I have an applet and I want to add a jbutton. The problem is the button is too big, I already used the setSize() method but still it doesn't work. Perhaps the setting of setSize could might be wrong.
could someone got an idea about this problem?
Thanks...
private JButton newGame = new JButton("New Game");
private JButton players = new JButton("Players");
private JButton quit = new JButton("Quit");
public void init()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
mainPanel = new JPanel();
getContentPane().add(mainPanel);
setVisible(true);
setSize(400, 400);
content.add(newGame);
content.add(players);
content.add(quit);
}
Please check the layout manager you are using for your container (panel, frame, applet..). This plays a major role in defining the size & position of components (like JButton).
See also in the Java Tutorial for more details:
Using Layout Managers.
The Laying Out Components Within a Container lesson.
Add JPanel into your applet and then add JButton to it
The tree code conception is
JApplet (GridLayout) <- JPanel (FlowLayout) <- JButton (setSize(new Dimension(x,y)))
Good Luck
I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).
How can I place my JLabel in JLayeredPane right below the JTextField?
Here is some code I have, and the current result shown in the screenshot below:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
layeredPane.setPreferredSize(field.getPreferredSize());
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
panel.add(layeredPane, BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Second try:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
lbl.setBounds(field.getBounds().x, field.getBounds().y,
field.getBounds().width, field.getBounds().height);
JPanel popPanel = new JPanel(new BorderLayout());
popPanel.add(lbl, BorderLayout.NORTH);
popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
popPanel.setPreferredSize(field.getPreferredSize());
JFrame frame = new JFrame();
JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Add the layeredPane to the "CENTER", not the SOUTH.
However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.
If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.
First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.
If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.
You have to make sure this is done every time the components are resized.
why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job