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.
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.
(using netbeans)
So for my project I need to add a JscrollPane so that the user can see all of the JTextArea output, a piechart and the two buttons I have added. This is the code I have implementing the JscrollPane. However it is causing the program to no longer produce an output screen. My question is do I need to add the JscrollPane to the JPanel or to the JFrame and if so what am I doing wrong (tried to include as much of the code as I thought was relevant)
P.S Should I change from Borderlayout to a Boxlayout? Would that make a difference in terms of adding a jscroll?
JFrame frame1 = new JFrame("Portfolio Results");
frame1.setSize(800,800);
// frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// output screen declartions
frame1.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
frame1.add(panel1,BorderLayout.PAGE_START);
panel1.setLayout(new BorderLayout());
JTextArea area1 = new JTextArea();
area1.setPreferredSize(new Dimension(600,600));
panel1.add(area1,BorderLayout.PAGE_START);
JScrollPane scp1 = new JScrollPane(frame1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame1.add(scp1);
//code for Pie chart and two button
DefaultPieDataset piedata = new DefaultPieDataset();
piedata.setValue("test", new Integer (100));
JFreeChart chart = ChartFactory.createPieChart("test", piedata, true, true, true);
PiePlot p = (PiePlot)chart.getPlot();
ChartPanel testpan = new ChartPanel(chart);
panel1.add(testpan,BorderLayout.CENTER);
JButton button= new JButton("SAVE");
// button.setPreferredSize(new Dimension(80,20));
// Listener listener = new Listener();
// button.addActionListener(this);
panel1.add(button,BorderLayout.PAGE_END);
JButton pbutton=new JButton("Print");
panel1.add(pbutton,BorderLayout.SOUTH);
You should init the JScrollPane with the object you want to scroll through.
In your example, it seems the JTextArea is the object you want, so:
JScrollPane scp1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
From the Oracle docs:
JScrollPane(Component view)
Creates a JScrollPane that displays the contents of the specified component, where both horizontal and
vertical scrollbars appear whenever the component's contents are
larger than the view.
Also, see this Oracle example.
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?
I have been searching around for an easy way to implement a JScrollPlane. I am trying to add it to a JPanel, and it will contain a dynamic number of JPanels (which will be filled with other stuff).
Here is my (failing miserably) attempt to make said JScrollPane:
final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
info.setLayout(new GridLayout(0,1));
info.setLocation(10,78);
info.setSize(420,490);
infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
The primary problem you're having is the fact that the default layout manager's layout is set to FlowLayout, which means that the JScrollPane will want to use it's preferred size to be layout with, which may not fill the entire panel.
Instead, use a BorderLayout
final JPanel info = new JPanel(new BorderLayout()); // <-- Change me :D
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// These are bad ideas, setLocation and setSize won't work, as the panel should be
// under the control of a layout manager
//info.setLocation(10,78);
//info.setSize(420,490);
//infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
In this example, a series of nested panels are added to a panel having BoxLayout. That panel is used to create a JScrollPane which is then added to a JFrame.
public class BoxTest extends JPanel {
...
JScrollPane jsp = new JScrollPane(this,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
...
JFrame f = new JFrame();
f.add(jsp); // BorderLayout.CENTER, by default
How can I get the scroller around my JList component in the code given below? It doesn't seem to work properly :(
public class JButtonO extends JFrame{
String[] values = {"henry", "Michael","Uche","John","Ullan","Nelly",
"Ime","Lekan","Austine","jussi","Ossi","Imam","Empo","Austine","Becky",
"Scholar","Ruth", "Anny"};
public JButtonO()
{
super("the button");
this.setSize(400,200);
JPanel panel = new JPanel();
JLabel label = new JLabel("Output Items:");
label.setAlignmentX(1);
label.setAlignmentY(1);
JList conList = new JList(values);
conList.setVisibleRowCount(3);
JScrollPane scroller = new JScrollPane(conList);
panel.add(label);
panel.add(scroller);
panel.add(conList);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(panel);
this.setVisible(true);
}
Adding the JScrollPane scroller that includes the JList conList to the JPanel panel is enough.
The mistake is that you are adding the JList a second time.
JScrollPane scroller = new JScrollPane(conList);
panel.add(label);
panel.add(scroller);
panel.add(conList); // <---THIS LINE SHOULD BE DELETED...
Look, I may not answering what you need, because I don´t remember to much of swing layout. I don´t work with it a long time ago...
But removing setting a layout (I remember) on your JPanel it works with this code:
public JButtonO() {
super("the button");
this.setSize(400, 200);
// Create a panel with a borderlayout
JPanel jpanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Output Items:");
label.setAlignmentX(1);
label.setAlignmentY(1);
// Add Label to top of layout
jpanel.add(label, BorderLayout.NORTH);
JList conList = new JList(values);
conList.setVisibleRowCount(3);
JScrollPane scroller = new JScrollPane(conList);
//AddScroll to center
jpanel.add(scroller);
//Add Panel to JFrame
this.add(jpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
I think the problems is the default layoutmaneger of JPanel. Because of how it works your scroll was not "srink" enough to create scrolls...
Hope it helps, even without too much explanation...
ACTUALLY: After I post the answer I saw your mistake. Now I can explain what is wrong. You already added your JList inside your JScrollPane here:
JScrollPane scroller = new JScrollPane(conList);
But after that you put it inside the JPanel:
panel.add(conList);
this changes where yout JList will be displayed, and let the JScroll empty again. Without components it will be displayed with size 0x0 and will not be draw (even being there).
Now I think I helped =D
The JScrollPane has settings called the scrollbar policies which say when the scrollbars are to be displayed. You can set them using JScrolPane(Component,int,int) constructor, or by calling setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy(). The default policies are "as needed", meaning the scrollbar is only displayed if the component is too large to display whole. So if your list fits inside the window, the scrolbars will not be visible, but will become visible when you e.g. make the window smaller using the mouse. You can change one or both policies to "always" using corresponding constants in order to make the scrollbar(s) always visible if that's what you need.