I am trying to create components and implement them into my JFrame from different classes within my program.
I have created a JTabbedPane, and each tab represents a class. All of the components for each tab are placed in their respective tabs.
//creates the JTabbedPane, and the panels. object creation.
//panelx corisponds to the tab number as well. tabbs are counted from left to right.
tabpane1 = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JLabel searchlabel1 = new JLabel("hey");
JLabel searchlabel2 = new JLabel("hi");
panel1.add(searchlabel1);
panel1.add(searchlabel2);
//SearchFlight searchflightComp = new SearchFlight();
tabpane1.addTab("Search Flight", panel1);
tabpane1.addTab("Select Flight", panel2);
tabpane1.addTab("Flight Price", new JLabel("This is tab 1ffff"));
tabpane1.addTab("Book Ticket", new JLabel("This is tab 1fff"));
tabpane1.addTab("Book Ticket", new JLabel("This is tab fs1"));
tabpane1.addTab("Payment", new JLabel("This is tabgf 1"));
tabpane1.addTab("Booking Summary", new JLabel("This is tabgf 1"));
//added the JTabbedPane to JFrame.
frame.getContentPane().add(tabpane1);
this works. I am only really working with the first tab right now to get the feel for how it works ect. But I dont even know how to begin. Would I create the a panel in the other class and then return it? or extend the JFrame?
thanks guys!
I assume you're refering to this commented line:
//SearchFlight searchflightComp = new SearchFlight();
You could either make SearchFlight a subclass of JPanel or better a controller which creates a JPanel for that tab and return it, e.g.
SearchFlight searchflightComp = new SearchFlight();
tabpane1.addTab( searchflightComp.getName(), searchflightComp.buildPanel() );
As a general tip you should read up on the MVC pattern.
This might help you to some extent: The MVC pattern and SWING
maybe you could extend from JComponent? And do it like they do here?
Related
I'm trying to create a panel that has information on it.
I've tried using the setBorderLayout to add the labels with the information to certain areas of the panel, but I know this doesn't work as it just overwrites what I already had before.
Here is an example from my code:
final JFrame fr = new JFrame("ATCGUI");
fr.setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
JPanel tab1 = new JPanel();
tab1.setLayout(new BorderLayout());
tabbedPane.addTab("Airport", tab1);
JLabel labelAirportName = new JLabel(airportInfo[0]);
tab1.add(labelAirportName, BorderLayout.NORTH);
JLabel labelAirportCodeStatic = new JLabel("Code:");
JLabel labelAirportLocationStatic = new JLabel("Location:");
JLabel labelAirportCoordinatesStatic = new JLabel("Coordinates:");
JLabel labelAirportAltitudeStatic = new JLabel("Altitude:");
JLabel labelAirportTimezoneStatic = new JLabel("Timezone:");
JLabel labelAirportICAOStatic = new JLabel("ICAO:");
tab1.add(labelAirportCodeStatic, BorderLayout.WEST);
tab1.add(labelAirportLocationStatic, BorderLayout.WEST);
tab1.add(labelAirportCoordinatesStatic, BorderLayout.WEST);
tab1.add(labelAirportAltitudeStatic, BorderLayout.WEST);
tab1.add(labelAirportTimezoneStatic, BorderLayout.WEST);
tab1.add(labelAirportICAOStatic, BorderLayout.WEST);
This ends up creating something like this:
When I am aiming for something that looks more like this:
I created this with the SwingUI designer but due to compatibility I am switching over to just Swing
Following the Oracle Swing tutorial linked by maloomeister https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I was able to create the layout I wanted by mixing BorderLayout and GridLayout.
I have two JCheckBox's and one JEditorPane. I am looking for an output as under
But my current code is somewhat messy for which I am not able to
private void createContents()
{
JEditorPane license;
JCheckBox confirmBox;
JCheckBox declineBox;
license = new JEditorPane("text/html", "");
license.setText (buildEulaText());
license.setEditable(false);
confirmBox = new JCheckBox("I accept.", false);
declineBox = new JCheckBox("I decline.", false);
add(license, BorderLayout.CENTER);
add(confirmBox, BorderLayout.SOUTH);
add(declineBox, BorderLayout.NORTH); //I know this is wrong
}
A simple solution is to compose the layouts using a new JPanel with a FlowLayout.
add(license, BorderLayout.CENTER);
JPanel boxes = new JPanel(new FlowLayout());
// FlowLayout is the JPanel default layout manager, so
// boxes = new JPanel(); works too :)
boxes.add(confirmBox);
boxes.add(declineBox);
add(boxes, BorderLayout.SOUTH);
But you can also take a look at the GridBagLayout.
Create a new JPanel to hold all the checkboxes, then add this to your panel/frame.
JPanel checkBoxesPane = new Panel();
checkBoxesPane.add( confirmBox );
checkBoxesPane.add( declineBox );
add( checkBoxes, BorderLayout.SOUTH );
First of all I would highly recommend to do it in javafx rather than in Java swing. It is the later Technology and much better according to my opinion.
If you still want to do it in Java swing, here is the code:
JPanel panel = new Panel();
panel.add(confirmBox);
panel.add(declineBox);
add(panel, BorderLayout.SOUTH);
I didn't test the code, but it should work with this code.
I'm kind of new to the whole "how to arrange your components" thing in JAVA and I couldn't figure out how to realise the following JFrame (I can't post images so I just put the link)
I tried to be as precise as possible about what I already did.
I would like your advice about how to arrange the green part.
Thanks!
EDIT: as some people rightfully said, I didn't put the code of what I did. Here it is:
public Frame(){
this.setTitle("Small application");
this.setSize(445, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
//Title
JLabel title = new JLabel("Welcome to this application");
title.setHorizontalAlignment(JLabel.CENTER);
title.setPreferredSize(new Dimension(200,50));
title.setFont(new Font("Courrier",Font.BOLD,20));
container.add(title, BorderLayout.NORTH);
//Center part
JPanel centerPart = new JPanel();
JLabel cell1 = new JLabel("Enter all measurements:");
cell1.setPreferredSize(new Dimension(150,20));
JLabel cell2 = new JLabel("Please, select the files...");
cell2.setPreferredSize(new Dimension(150,20));
cell2.setBackground(Color.white);
cell2.setBorder(BorderFactory.createLineBorder(Color.black));
cell2.setOpaque(true);
JButton cell3 = new JButton("Browse");
cell3.setPreferredSize(new Dimension(100,20));
centerPart.add(cell1);
centerPart.add(cell2);
centerPart.add(cell3);
container.add(centerPart, BorderLayout.CENTER);
/*
* I need your help here :)
* I can't figure out how to put the image and the text next to it
*/
//Bottom part
JPanel bottom = new JPanel();
JButton graph = new JButton("Graph");
JButton exit = new JButton("Exit");
bottom.add(graph);
bottom.add(exit);
container.add(bottom, BorderLayout.SOUTH);
this.setContentPane(container);
}
For most practical cases, you use multiple, nested containers, with a LayoutManager suited to the layout within each container.
Each LayoutManager does one specific job, in practice you often want differnt regions of a UI layouted in different ways. So for each region use a separate Container (e.g. JPanel) and set a LayoutManager that suits your layout requirements.
The big hurdle for beginners seems to be to get the point that LayoutManagers can (and often must) be used with nested containers.
Try using a JPanel
Create the JPanel
Place your JPanel into your JFrame
Position the labels, button, textfield onto the newly created JPanel.
It should do the trick. and it is pretty basic. You should be able to do the code on your own!
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'm adding a two JPanel objects (panels contain tables) to a JSplitPane, for some reason the table is not completely visible. I wanted to add a screen shot for reference but stack overflow wouldn't allow me to do so :(. Anyway, the code related to this is as follows: Kindly suggest some solution.
frame.setTitle(PAGE2);
final String[] stream_column_names = { "IP Address", "Port", "IP Address", "Port", "Transport" };
final String[] packet_column_names = { "#", "Direction", "Preview" };
final JScrollPane pane1 = new JScrollPane();
final JScrollPane pane2 = new JScrollPane();
final JLabel label1 = new JLabel();
final JLabel label2 = new JLabel();
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, streamPanel, packetPanel);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(350);
streamPanel = new JPanel();
streamPanel.setLayout(new BorderLayout());
pane1.setLayout(new ScrollPaneLayout());
label1.setText("STREAM:");
label1.setVerticalAlignment(SwingConstants.CENTER);
label1.setHorizontalAlignment(SwingConstants.LEFT);
streamInfoTable = new JTable();
pane1.setViewportView(streamInfoTable);
streamInfoTable.setModel(streamTableModel);
streamTableModel.setColumnIdentifiers(stream_column_names);
streamPanel.add(label1, BorderLayout.NORTH);
streamPanel.add(pane1);
streamPanel.getPreferredSize();
splitPane.add(streamPanel);
packetPanel = new JPanel();
packetPanel.setLayout(new BorderLayout());
pane2.setLayout(new ScrollPaneLayout());
label2.setText("PACKET:");
label2.setVerticalAlignment(SwingConstants.CENTER);
label2.setHorizontalAlignment(SwingConstants.LEFT);
packetInfoTable = new JTable();
pane2.setViewportView(packetInfoTable);
packetInfoTable.setModel(packetTableModel);
packetInfoTable.setRowSelectionAllowed(false);
packetInfoTable.setColumnSelectionAllowed(false);
packetTableModel.setColumnIdentifiers(packet_column_names);
packetPanel.add(label2, BorderLayout.NORTH);
packetPanel.add(pane2);
packetPanel.getPreferredSize();
splitPane.add(packetPanel);
centerPanel.add(splitPane);
frame.add(centerPanel, BorderLayout.CENTER);
frame.setVisible(true);
jDesktopPane.add(frame);
Your centerPanel JPanel looks to be using its default layout which for JPanel is FlowLayout, a layout that does not resize the contents it displays and thus you risk possibly not showing the entire JScrollPane that holds your JTable.
Consider either giving your centerPanel a BorderLayout, or if it just holds the JSplitPane and nothing else, then getting rid of it all together and simply adding the JSplitPane to your frame's BorderLayout.CENTER position. Don't forget to call pack() on your JFrame before displaying it.
And again, if still stuck, then distill your problem down to an sscce and posting the latest code here.
Edit: not sure why you're giving your JScrollPane a layout as it's own default layout should work just fine.