I want to create new tabs in tabbedpane when new option is clicked. But new tab should include the panel with a text area on same position as in the first tab which I created through drag and drop in netbeans. I create one tab and want new instance of that tab as other tabs how can I do that?
I am creating a notepad application and I want to add the functionality of new files in the form of tabs in tabbedpane. I have created one tab through drag and drop in netbeans. but I don't know how to use this instance in new tab when new option is clicked.
//This is the code to create new panel but it is not working
int i= 1;
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)
{
JPanel jp = new JPanel();
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
// i cantains text area.
jTabbedPane1.addTab("untitled"+i,jp);
i++;
}
I want the application to create new tabs as tabs created in netbeans or dev etc.
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
You can't share components. Swing components can only have a single parent.
So you need to create a new instance of JPanel and a new Instance of JTextArea.
JTextArea textArea = new JTextArea(5, 20);
JPanel panel = new JPanel( new BorderLaout() );
panel.add( textArea );
jTabbedPane1.addTab("untitled"+i, panel);
Now the text area will fill the space available in the tabbed pane.
You don't event need the JPanel if all you want is a text area on the tab. Just add the new text area to the tabbed pane.
JPanel jp1 = new JPanel();
JTextArea ta1 = new JTextArea();
ta1.setBounds(10, 10,100 , 100);
jp1.add(ta1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jp1);
jp1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE));
jTabbedPane1.addTab("untitled",jp1);
//this is the right answer to my question this is the way to pass a text area with a panel in the tabbed pane
Related
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 got a JScrollPane that is called incomeScroll and on this Scroll Pane is a JPanel that is called inscrollPanel.
If the user clicks on the plus label
then a new jpanel should be added to inscrollPanel. I called revalidate and repaint on the inscrollPanel and on the incomeScroll Panel, but it did nothing. I designed the window with NetBeans UI Designer. And I tried different layouts such as BoxLayout and FlowLayout. The JPanels should be vertical aligned. You can see two jpanels in each jscrollpane and my question is how can I add another jpanel to inscrollPanel which is the child of incomeScroll?
Here is the code I put in the mouselistener when you click on the plus label so far:
`javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize( new Dimension(359, 149));
panel.setBackground( new java.awt.Color( 119, 255, 144));
inscrollPanel.add(panel);
inscrollPanel.revalidate();
inscrollPanel.repaint();
incomeScroll.revalidate();`
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.
(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 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);