Multiple documents with the same JTable - java

I am developing a Java Application that uses a JTable for capturing user input. The user will have to print data captured in the table. I would want the user to open multiple documents to work on. This I have implemented it this way: I have my JFrame as my main window; on to this JFrame, I have added JTabbedPane so that the user can switch between File, Settings and Tools.Upon clicking new File, the user is taken to a JTabbedPane(place at the center of the JFrame) with a JTable for input. I would want that the next time the user click new File, a new JPanel would be added to the JTabbledPane; but this new JPanel should also contain the JTable for input.This behavior should continue everytime the user creates a new File. This is shown in the images that I have uploaded.(Please forgive for poor drawing).
I have achied this by this code:
public final class QuotPane {
//components to be used
JFrame frame;
JTabbedPane tabbedPane,tablePane;
JPanel quotPane, topPane, pane1, pane2, pane3,tablePanel;
JSeparator sep;
JButton newFile;
QuotPane() {
this.createQuotPane();
this.createGUI();
ButtonActionListener lits= new ButtonActionListener();
newFile.addActionListener(lits);
}
public void createGUI() {
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Create Quot", quotPane);
frame = new JFrame("Qout Interface");
frame.setSize(new Dimension(700, 650));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//adding the tabbed pane to the frmae
frame.add(tabbedPane, BorderLayout.NORTH);
}
public void createQuotPane() {
quotPane = new JPanel();
quotPane.setLayout(new BorderLayout());
//creating the top pane
topPane = new JPanel();
topPane.setPreferredSize(new Dimension(650, 145));
topPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.lightGray, Color.lightGray, Color.white, Color.orange));
topPane.setLayout(new MigLayout());
//add the top pane on the quot panel
quotPane.add(topPane, BorderLayout.NORTH);
//adding the panes on the top pane
this.createPanels();
topPane.add(pane1);
topPane.add(pane2);
topPane.add(pane3);
}
//a method to create panes for options
public void createPanels() {
pane1 = new JPanel();
pane1.setPreferredSize(new Dimension(200, 140));
pane1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
//lets set the icons then
pane1.setLayout(new MigLayout());
Icon fileIcon = new ImageIcon(this.getClass().getResource("/images/fil.jpg"));
newFile = new JButton(fileIcon);
pane1.add(new JLabel("New File"), "wrap");
pane1.add(newFile,"width 20!");
pane2 = new JPanel();
pane2.setPreferredSize(new Dimension(200, 140));
pane2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
pane3 = new JPanel();
pane3.setPreferredSize(new Dimension(200, 140));
pane3.setMaximumSize(new Dimension(300, 140));
pane3.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
}
public static void main(String[] args) {
QuotPane qp = new QuotPane();
}
public void createTablePane(){
tablePanel= new JPanel();
}
class ButtonActionListener implements ActionListener{
protected int count=0;
#Override
public void actionPerformed(ActionEvent e) {
if(count==0){
if(e.getSource().equals(QuotPane.this.newFile)){
tablePane=new JTabbedPane();
QuotPane.this.createTablePane();
tablePane.addTab("New Qout", tablePanel);
frame.add(tablePane,BorderLayout.CENTER);
count ++;
}
}else if(count>0)
{
tablePane.add("New Quote",new JPanel());
}}}}
The challenge I am facing here is that I cannot add my JTable to every panel created at run time.I have tried this: tablePane.add("New Quote",myCreatedBeforePanleWithTable) but it is overriding the previous tab.
Here are images of things that I want.I read about JDeskTopPane and JInternalFrame, but can't figure out how to make it work the way I want.
How do I make it work the way I want as shown in the image?

Unfortunately, a Swing Component can only have a single parent. If you add Component A to JPanel X and then to JPanel Y, it will end up in Y.
You will need to have three separate JTables, all sharing a single TableModel. This should be fairly straightforward to implement the basic functionality. It might get tricky if you allow sorting and column reordering and you want that to affect all 3 tables.

Related

Trying to add ScrollPane in Jpanel with null layout inside BorderLayout

I am trying to add a scrollbar in jpanel with null layout.
I want to create a form. This should should display few buttons at the bottom at all times.Any content inside form should maintain it's size and ratio even if the parent container is resized.
Here is what I've come with. I have a panel with borderlayout and added buttons at the south of border. Then created another jpanel to contain form that is added at the center of parent jpanel. Since I want form to maintain it's ratio I went with null layout for inner panel. But I want it to display scrollbar when content is not fully visible. enter image description here
Now adding inner jpanel into scrollpane and adding scrollpanel into parent panel (.add(scrollpane, BorderLayout.CENTER)) doesn't give desired format.
Is there any thing that I can do to get desired format?
Here is code Sample:
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(new Dimension(1000, 700));
Container c = jFrame.getContentPane();
c.setLayout(new BorderLayout());
bottomPanel(c);
centerPanel(c); //scrollbar should go in this panel
jFrame.setVisible(true);
}
private static void centerPanel(Container c) {
JPanel centerPanel = new JPanel();
centerPanel.setLayout(null);
JButton button = new JButton("This jObject should not resize when window resizes and also should maintain relative position.");
button.setBounds(new Rectangle(10, 10, 600, 50));
JButton button1 = new JButton("Just like it works in this code. Just Add ScrollPane to centerPanel That is in green backround");
button1.setBounds(new Rectangle(10, 70, 600, 50));
JButton button2 = new JButton("For clearity");
button2.setBounds(new Rectangle(10, 130, 600, 50));
centerPanel.add(button);
centerPanel.add(button1);
centerPanel.add(button2);
centerPanel.setBackground(Color.GREEN);
c.add(centerPanel, BorderLayout.CENTER);
}
private static void bottomPanel(Container c) {
JPanel bottomPanel = new JPanel(); //Buttons that goes at the bottom of screen will go in here
JPanel bottomInnerPanel = new JPanel();
bottomInnerPanel.setLayout(new BorderLayout());
bottomPanel.setLayout(new GridLayout());
bottomInnerPanel.add(new JButton("Add"), BorderLayout.WEST);
bottomInnerPanel.add(new JButton("Search"), BorderLayout.EAST);
bottomPanel.add(bottomInnerPanel);
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
c.add(bottomPanel, BorderLayout.SOUTH);
}

I can't get my JLabels to show. I'm not sure if I placed them in the wrong position in the BorderLayout. Help! My code is below

This is the main class where JLabels and lists are initialized:
public dutchessHousing() {
super("Plan options");
contents = getContentPane();
contents.setLayout(new BorderLayout());
selections = new JLabel (suiteList[0]);
suitesScrollPane = new JScrollPane(suites);
meals = new JList<String>(mealList);
housingOptions = new JLabel();
mealOptions = new JLabel();
total = new JLabel();
contents.add(suitesScrollPane, BorderLayout.EAST);
contents.add(selections, BorderLayout.CENTER);
contents.add(meals, BorderLayout.SOUTH);
contents.add(total, BorderLayout.WEST);
setSize(750, 700);
addComponent();
showCalculations();
setVisible(true);
}
Method to show calculations in frame. housingOptions is supposed to show on the top of the frame, before the JList.
public static void showCalculations() {
housingOptions = new JLabel("Choose one of the following housing options: ", JLabel.CENTER);
mealOptions = new JLabel("Choose one of the following meal plan options: ", JLabel.CENTER);
total = new JLabel();
housingOptions.setLayout(new BorderLayout());
contents.add(housingOptions);
contents.add(mealOptions);
contents.add(total);
}
Main method:
public static void main(String[] args) {
dutchessHousing frame = new dutchessHousing();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(selections);
frame.add(suitesScrollPane);
frame.add(meals);
//frame.add(housingOptions);
frame.add(mealOptions);
frame.add(total);
showCalculations();
frame.setVisible(true);
}
}
contents.add(suitesScrollPane, BorderLayout.EAST);
contents.add(selections, BorderLayout.CENTER);
contents.add(meals, BorderLayout.SOUTH);
contents.add(total, BorderLayout.WEST);
setSize(750, 700);
addComponent();
showCalculations();
Above you add components to different areas of the BorderLayout which is reasonable.
But then when you invoke the showCalculations() method you use:
contents.add(housingOptions);
contents.add(mealOptions);
contents.add(total);
Which will replace all the components added in the "CENTER" with the "total" component since only the last component added to any region of the BorderLayout will be visible.
So you first need to add all the labels to a panel. Then add the panel to the "BorderLayout.CENTER"

swing BoxLayout not working

I have read many subjects here but I can't make my window with the layout I want.
I simply want all my graphic object to be in a row style like in the first picture here : http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html
I've tried GridLayout but it still make my first button giant and then, as I add textfields, it's getting smaller and smaller?!
Here is my code without all the imports:
public class TestScrollPane extends JFrame implements ActionListener{
Dimension dim = new Dimension(200 , 50);
JButton button;
JPanel panel = new JPanel();
JScrollPane scrollpane = new JScrollPane(panel);
public TestScrollPane(){
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.add(scrollpane);
//panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
button = new JButton("click me");
button.setPreferredSize(dim);
panel.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button ){
JTextField txt = new JTextField(); // we add a new button
txt.setPreferredSize(dim);
panel.add(txt);
SwingUtilities.updateComponentTreeUI(this); // refresh jframe
}
}
public static void main(String[] args){
TestScrollPane test = new TestScrollPane();
}
}
I just want to have one button per row.
A BoxLayout will respect the minimum/maximum sizes of a component.
For some reason the maximum height of a text field is unlimited so the text field gets all the space available.
So you can do something like:
JTextField txt = new JTextField(10); // we add a new button
//txt.setPreferredSize(dim); // don't hardcode a preferrd size of a component.
txt.setMaximumSize(txt.getPreferredSize());
Also:
//SwingUtilities.updateComponentTreeUI(this); // refresh jframe
Don't use the above method. That is used for a LAF change.
Instead when you add/remove components from a visible GUI you should use:
panel.revalidate();
panel.repaint();

(Java) There are white borders around all my buttons

In java, My buttons are surrounded by white, thick borders that only appear when I have a image in a jlabel for the background.
Example:
The problem here is that white borders go around all of my buttons, making it look terrible. The intended use was for the buttons to just go over the image, like this image:
Here is my code:
public class Gui extends JFrame {
private JTextField TextField;
private JButton Hi, Bye, Exit;
private JPanel Panel, Panel1, Panel2;
private JLabel label;
public Gui() {
super("My Program");
TextField = new JTextField("");
TextField.setEditable(false);
Hi = new JButton("Hi");
Bye = new JButton("Bye");
Exit = new JButton("Exit");
Actions a = new Actions();
Hi.addActionListener(a);
Bye.addActionListener(a);
Exit.addActionListener(a);
Dimension tfd = new Dimension(780, 25);
Dimension bd = new Dimension (75, 25);
Dimension lpd = new Dimension (800, 600);
TextField.setPreferredSize(tfd);
Hi.setPreferredSize(bd);
Bye.setPreferredSize(bd);
Exit.setPreferredSize(tfd);
ImageIcon image = new ImageIcon("C:/Users/Dakota/Desktop/Coding/Coding/img/Background.png");
label = new JLabel(image);
Panel = new JPanel();
this.setContentPane(label);
this.add(Panel);
this.setLayout(new FlowLayout());
Panel.add(TextField);
Panel1 = new JPanel();
this.add(Panel1);
Panel1.add(Hi);
Panel1.add(Bye);
Panel2 = new JPanel();
this.add(Panel2);
Panel2.add(Exit);
}
Your problem appears that you're adding JPanels to your GUI and not changing the opaque property. This property is by default true, meaning that the JPanel will paint a background that visually covers up anything below it, but if you set it to false by calling myPanel.setOpaque(false);, then any components below the JPanel will show through. This is true for most any Swing component, although is not quite straight-forward for component components such as JScrollPanes, and JTextComponents.

JButtons only appear on JFrame if in BorderLayout.CENTER, not SOUTH or NORTH

So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works

Categories