(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.
Related
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()
I am trying to make a UI to view recipes from a cookbook stored on the computer. Part of this tab is a JScrollPanel storing a JTextArea that displays the available recipes. All called functions work as intended (e.g. allRecipes() returns a string of the available recipes properly); however, the scroll pane itself does not appear. It is added to the frame, as I can see by a small grey block where the pane would be, but it is not filled as it should be. The code is as follows:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
We don't know what allRecipes() does, but I would guess it sets the text of the text area.
Instead you should define your text area with the rows/columns you wish. Something like:
JTextArea recipes = new JTextArea(5, 30);
then in the constructor you would add the text:
recipes.setText( allRecipes() );
You should NOT be trying to set the preferred size of the scroll pane. The preferred size will automatically be determined from the preferred size of the text area which is calculated based on the rows/columns provided in the constructor.
//scroll.setPreferredSize(new Dimension(10,15));
Also, the preferred size of a component is specified in pixels, to the above makes no sense.
pane2.add(scroll, BorderLayout.CENTER);
The default layout manager for a JPanel is the FlowLayout. So you can't just use a BorderLayout constraint when adding the component.
I have written a java gui code for many options available on it. the gui is also set visible true but it doesn't show until I pick its border and drag them to resize the gui window. After manually resizing it, it shows everything. Also, the textlabels and the textfields and buttons are not in new lines, they are placed one after one. Please tell me whats wrong with that: here is a part of code:
public static void initGUI(){
JFrame fr = new JFrame();
Container cont = fr.getContentPane();
cont.setLayout( new FlowLayout( ) );
FlowLayout layout = new FlowLayout();
cont.setLayout(layout);
frame.setSize(200,300) ;
frame.setVisible(true) ;
JTextField tName = new JTextField(30);
JTextField tCNIC = new JTextField(15);
JLabel nameLabel = new JLabel("Name:");
JLabel cnicLabel = new JLabel("CNIC #:");
cont.add(nameLabel);
cont.add(tName);
cont.add(cnicLabel);
cont.add(tCNIC);
JButton Cancel = new JButton ("Canel" );
JButton OK = new JButton ("OK" );
savebtn.setPreferredSize(new Dimension(150, 50));
retbtn.setPreferredSize(new Dimension(150, 50));
cont.add(savebtn);
cont.add(retbtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
frame.setVisible(true) ;
The above statement should be invoked AFTER all the components have been added to the frame. So it should be the last statement in your method.
Also, you should be invoking:
frame.pack();
instead of setSize(), before making the frame visible so all the components are displayed at their preferred size.
frame.setVisible(true);
This Statement should be invoked in the last of adding other components to the Frame.
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.
I am trying to display JLabels on top of a JProgressBar so that I can have fancy formatted text on a JProgressBar.
Here is the constructor for my component, which extends JPanel:
public InfoDisplay() {
//setLayout(new GridLayout(0, 1));
setLayout(new OverlayLayout(this));
lblPlayer = new JLabel();
lblPlayer.setName("Owner");
lblUnit = new JLabel();
lblUnit.setName("Unit");
lblCoords = new JLabel();
lblCoords.setName("Coordinates");
lblResources = new JLabel();
lblResources.setName("Resouces");
prgProgress = new JProgressBar();
prgProgress.setMaximum(4);
prgProgress.setStringPainted(true);
fmtDefault = "<html><font color='gray'>%s:</font> %s</html>";
JPanel pnlInfo = new JPanel();
pnlInfo.setLayout(new FlowLayout(FlowLayout.LEADING));
pnlInfo.setOpaque(false);
pnlInfo.add(lblPlayer);
pnlInfo.add(lblUnit);
pnlInfo.add(lblCoords);
pnlInfo.add(lblResources);
add(pnlInfo);
add(prgProgress);
displayInfo(Collections.EMPTY_LIST);
setProgress("Upkeep", 2);
}
I have tried using OverlayLayout, which achieves this goal when the form is created. However, when the JProgressBar updates, it covers up the other panel which holds the text fields.
I have also tried using a JLayeredPane, but I would have to write a custom layout manager for that to work and I was hoping to avoid doing all of the resizing code by hand for such a simple thing.
Does anyone have any other solutions or ideas?
An easier approach is to just add the panel to the progress bar then you don't have to worry about invoking repaint. The progress bar will automatically repaint its child component:
See the Swing tutorial on How to Use Progress Bars. I added the following code to the ProgressBarDemo:
progressBar.setLayout( new BorderLayout() );
JPanel child = new JPanel( new BorderLayout() );
child.setOpaque(false);
child.add( new JLabel("WEST"), BorderLayout.WEST );
child.add( new JLabel("EAST"), BorderLayout.EAST );
progressBar.add(child);